AlexandruT

Contorizarea numărului de biţi pe 1 dintr-un argument întreg

Oct 10th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.24 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int bitCount(int x)
  5. {
  6.     int sol = 0;
  7.     for(int i = 0; i < 32; i++)
  8.         if(x & (1 << i)) sol++;
  9.     return sol;
  10. }
  11.  
  12. int main()
  13. {
  14.     int n;
  15.     cin >> n;
  16.     cout << bitCount(n);
  17. }
Advertisement
Add Comment
Please, Sign In to add comment