### Count no of 0's in the binary number.

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
#include <iostream>
#include<bitset>
using namespace std;

int main()
{
int num,count=0,Zero,count1 =0;
cout<<"Enter the number:";
cin>>num;
string binary;
binary = bitset<4>(num).to_string();
cout<<"The binary conversion of " << num << " is " << binary << endl;
while(num>0)
{
num = num & (num-1);
count++;
}
cout<<"The no of 1's in the binary number " << binary << " is " << count << endl;
}
closed account (18hRX9L8)
Isn't the number of zeroes the total number of digits minus the number of ones?
I tried doing that but, it shows the result as zero. I did
int count1;
count1 = sizeof(binary)-sizeof(count);
cout<< count1;
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
 ``1234567891011121314151617181920212223242526`` ``````#include #include #include #include int main() { unsigned int num = 101 ; std::cin >> num ; constexpr std::size_t MAX_BITS = std::numeric_limits::digits ; const std::bitset bits(num) ; std::cout << "The no of 1's in " << bits << " is " << bits.count() << '\n' ; std::cout << "The no of 0's in " << bits << " is " << bits.size() - bits.count() << '\n' ; std::string str = bits.to_string() ; auto pos = str.find('1') ; if( pos != std::string::npos ) { str = str.substr(pos) ; std::cout << "The no of 0's in " << str << " is " << std::count( str.begin(), str.end(), '0' ) << '\n' ; } }``````

http://liveworkspace.org/code/1kzF0Q\$0
 Isn't the number of zeroes the total number of digits minus the number of ones?

Depends on whether leading zeroes count.

 ``12345678910111213141516171819202122232425262728293031323334`` ``````#include #include void doBinaryJunk(unsigned n) { unsigned mask = 1 << (std::numeric_limits::digits-1) ; unsigned count[2] = {0}; // skip leading 0's: while ( mask && !(mask & n) ) mask >>= 1 ; while ( mask ) { unsigned digit = (mask & n) != 0 ; ++count[digit] ; std::cout << digit; mask >>= 1 ; } std::cout << "\n\t0: " << count[0] << "\n\t1: " << count[1] << '\n' ; } int main() { unsigned num ; std::cout << "Enter num: " ; std::cin >> num ; doBinaryJunk(num) ; }``````
@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??
Last edited on
Another similar example

 ``12345678910111213141516171819202122232425`` ``````#include #include int main() { std::vector bits; int x = 256; std::cout << "Output order is Little-Endian" << std::endl; while(x > 0) { bits.push_back(x & 1); std::cout << (x & 1); x >>= 1; } std::cout << std::endl; std::cout << "Output order is big-Endian" << std::endl; for( int i = (bits.size() - 1); i >= 0; --i) std::cout << bits[i]; std::cout << std::endl; return 0; }``````