📜  按位计数总设置位 - C++ 代码示例

📅  最后修改于: 2022-03-11 14:44:45.931000             🧑  作者: Mango

代码示例2
//WAP to find setbits (total 1's in binary ex. n= 5 => 101 => 2 setbits
int count{}, num{};
  cin >> num;

  while (num > 0) {
    count = count + (num & 1); // num&1 => it gives either 0 or 1
    num = num >> 1;    // bitwise rightshift 
  }

     cout << count; //count is our total setbits