Hello, this is my first post :).
Anyway, i am writing a program that accepts a decimal number from the user and convert it to binary numbers. After the conversion, i should count the number of 1's and 0's in the said binary number. I got upto converting and counting 1's using Brian Kernighan’s Algorithm. But, i can't seem to get it to count the number of 0's. Any help will be appreciated :D
using namespace std;
int num,count=0,Zero,count1 =0;
cout<<"Enter the number:";
binary = bitset<4>(num).to_string();
cout<<"The binary conversion of " << num << " is " << binary << endl;
num = num & (num-1);
cout<<"The no of 1's in the binary number " << binary << " is " << count << endl;
I tried doing that but, it shows the result as zero. I did
count1 = sizeof(binary)-sizeof(count);
but it shows count1 as zero.
When i tried finding the size manually, it showed both binary and count as 4=>4-4=0.
Must be because of the brian algo
@cire:Your code works, but is a bit out of my depth(i'm a newbie in coding). So, if possible, can you post a line by line explanation. I'll try to understand as much as i can myself. Thanks.
edit:Can anyone tell me how to get 0's im the program i posted??