Answers for "how to count set bit in c++"

C++
1

count number of set bits C++

int hammingWeight(uint32_t n) {
    int count = 0;
    
    while (n) {
        n &= (n - 1);
        count++;
    }
    
    return count;
}
Posted by: Guest on September-28-2021
0

count bit 1 c++

returns the number of set bits in a integer.
cout<< __builtin_popcount (11); //1011
//Ouput: 3
Posted by: Guest on May-10-2021

Browse Popular Code Answers by Language