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) ;
}
| |
This code outputs number of 1s and 0s but how to find position of 1s and 0s??
Last edited on