How to Count number of 1's in binary representation Number Of bits in Binary form trick STL (c++): __builtin_popcount ( number ); //it will return number of 1's Other Method: while(n>0) { n = n & (n-1); count++; } Another Method: while(n) { count+= n & 1; n = n >> 1; }