Advertisement
jasonpogi1669

Count the 1-Bits of a number using C++

Sep 21st, 2021
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.20 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     int n;
  7.     cin >> n;
  8.     int one_bits = 0;
  9.     while (n > 0) {
  10.         one_bits += (n & 1);
  11.         n >>= 1;
  12.     }
  13.     cout << one_bits << '\n';
  14.     return 0;
  15. }
  16.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement