3

I am a beginner starting in C and am doing some exercises on codewars. The exercise requires me to take a decimal int, convert it into binary and output the number of 1s in the binary number. Below my incomplete code. I store the binary in int b and I want to output it into an array so that I can run a loop to search for the 1s and output the sum.

Thanks in advance!

#include <stddef.h>
#include <stdio.h>

//size_t countBits(unsigned value);
int countBits(int d);

int main() {
    int numD = 1234;
    int numB = countBits(numD);

    printf("The number %d converted to binary is %d \n", numD, numB);
}

int countBits(int d) {
    if (d < 2) {
        return d;
    } else {
        int b = countBits(d / 2) * 10 + d % 2; //convert decimal into binary
        int c;
        int bArray[c];
    }
chqrlie
  • 131,814
  • 10
  • 121
  • 189
nocnoc
  • 337
  • 2
  • 12
  • What you're doing is not really possible. Any number larger than 512 (nine bits) will overflow the "binary" value stored in an `int`. – Some programmer dude May 17 '20 at 23:15
  • Does this answer your question? [How to count the number of set bits in a 32-bit integer?](https://stackoverflow.com/questions/109023/how-to-count-the-number-of-set-bits-in-a-32-bit-integer) – Marco May 17 '20 at 23:17
  • You don't need to store the "converted binary" anywhere, you can test bits in the int. Or if you really have to, then using a `char` array containing `'1'` and `'0'` might be easier – M.M May 17 '20 at 23:17
  • You can easily spin through an `int` value using a loop and a bitshift. – tadman May 18 '20 at 00:13
  • @nocnoc: you can accept one of the answers by clicking on the grey checkmark below its score. – chqrlie May 19 '20 at 22:23

3 Answers3

1

Your function is almost correct:

  • you should define the argument type as unsigned to avoid problems with negative numbers
  • you should just return b in the else branch. Trying to use base 10 as an intermediary representation is useless and would fail for numbers larger than 1023.

Here is a corrected version:

int countBits(unsigned d) {
    if (d < 2) {
        return d;
    } else {
        return countBits(d / 2) + d % 2;
    }
}

There are many more efficient ways to compute the number of bits in a word.

Check Sean Eron Anderson's Bit Twiddling Hacks for classic and advanced solutions.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
0

You can make an array char as one of the replies said, for example:

#include <stdio.h>
#include <string.h>

int main(){
    int count=0;
    int n,bit;
    char binary[50];
    printf("Enter a binary: \n");
    scanf("%s",binary);
    n=strlen(binary);
    for(int i=0;i<n;i++){
        bit=binary[i]-'0';
        if (bit==1){
            count=count+1;
        }

    }
  printf("Number of 1's: %d\n",count);

return 0;
}

This should count the number of 1's of a given binary. Try something like this! edit: I know that binary[i]-'0' might be confusing, if you don't understand that..take a look at this:

Nicolas Bazan
  • 65
  • 2
  • 7
0

There are definitely 'smarter'/more compact ways to do this, but here is one way that will allow you to count bits of a bit larger numbers

#include <stdio.h>
int count_bits(int x)
{
    char c_bin[33];
    int count=0;
    int mask=1;
    for( int i =0; i < 32; i++){
        if (x & mask ){
            count=i+1;
            c_bin[31-i]='1';
        }
        else{
            c_bin[31-i]='0';
        }
        mask=mask*2;
    }
    c_bin[32]='\0';
    printf("%d has %d bits\n",x,count);
    printf("Binary x:%s\n",c_bin);

    return count;
}
int main()
{
    int c=count_bits(4);
    return 0;
}
visibleman
  • 3,175
  • 1
  • 14
  • 27