-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path191.cpp
More file actions
19 lines (18 loc) · 696 Bytes
/
Copy path191.cpp
File metadata and controls
19 lines (18 loc) · 696 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*******************************************************************************
> File Name: 191.cpp
> Author: sillyplus
> Mail: oi_boy@sina.cn
> Created Time: Mon Mar 7 10:25:08 2016
*******************************************************************************/
class Solution {
public:
int hammingWeight(uint32_t n) {
int ret = n;
ret = (ret & 0x55555555) + ((ret >> 1) & 0x55555555);
ret = (ret & 0x33333333) + ((ret >> 2) & 0x33333333);
ret = (ret & 0x0F0F0F0F) + ((ret >> 4) & 0x0F0F0F0F);
ret = (ret & 0x00FF00FF) + ((ret >> 8) & 0x00FF00FF);
ret = (ret & 0x0000FFFF) + ((ret >> 16) & 0x0000FFFF);
return ret;
}
};