Skip to content

Instantly share code, notes, and snippets.

@walkingtospace
Created April 1, 2015 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walkingtospace/bc863d29f1f0924afca8 to your computer and use it in GitHub Desktop.
Save walkingtospace/bc863d29f1f0924afca8 to your computer and use it in GitHub Desktop.
number-of-1-bits
https://leetcode.com/problems/number-of-1-bits/
class Solution {
public:
int hammingWeight(uint32_t n) {
int size = 32; //uint32
int numOne = 1;
int cnt = 0;
for(int i=0; i<size ; ++i) {
if(n>>i & numOne) cnt++;
}
return cnt;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment