-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.cpp
More file actions
38 lines (35 loc) · 818 Bytes
/
program.cpp
File metadata and controls
38 lines (35 loc) · 818 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**************************************************************
* Author : Robin Liu
* Date : 2015-05-01
* Detail : https://leetcode.com/submissions/detail/26697057/
**************************************************************/
#include "../include/pre.h"
#include <cstdint>
#include <vector>
using std::vector;
#define LENGTH_OF_INT32 sizeof(int) * 8
int hammingWeight(uint32_t n)
{
int count = 0;
for (int i = 0; i != LENGTH_OF_INT32; i++)
{
if (n % 2 == 1)
count += 1;
n = n >> 1;
}
return count;
}
#undef LENGTH_OF_INT32
int Mymain()
{
vector<int> testData = {1, 2, 3, 23, 45, 67};
for (int i : testData)
{
cout << "NUM :" << i << endl;
cout << "BIN :";
PrintToBinary<uint32_t>(i);
cout << "WET :" << hammingWeight(i) << endl << "=====" << endl;
}
system("pause");
return 0;
}