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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <bitset>
#include <limits>
#include <algorithm>

int main()
{

    unsigned int num = 101 ;
    std::cin >> num ;

    constexpr std::size_t MAX_BITS = std::numeric_limits<unsigned int>::digits ;
    const std::bitset<MAX_BITS> 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <limits>

void doBinaryJunk(unsigned n)
{
    unsigned mask = 1 << (std::numeric_limits<unsigned>::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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <vector>

int main()
{
	std::vector<bool> 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;
}
Thanks for the reply guys.
Topic archived. No new replies allowed.