-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountingBits.c
47 lines (38 loc) · 903 Bytes
/
CountingBits.c
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
39
40
41
42
43
44
45
46
47
#include <stdio.h>
void countBits(unsigned int x){
unsigned int m[5];
m[0] = 0x55555555;
m[1] = 0x33333333;
m[2] = 0x0f0f0f0f;
m[3] = 0x00ff00ff;
m[4] = 0xffffffff;
int k=1;
for(int i=0;i<5;i++){
printf("%b\n",x);
x = (x & m[i]) + ((x >> k) & m[i]);
printf("%b\n\n",m[i]);
k*=2;
}
printf("%b\n\n",x);
}
void main(){
unsigned int x;
while(1){
scanf("%u",&x);
unsigned int m[5];
m[0] = 0x55555555;
m[1] = 0x33333333;
m[2] = 0x0f0f0f0f;
m[3] = 0x00ff00ff;
m[4] = 0xffffffff;
int k=1;
for(int i=0;i<5;i++){
printf("%b\n",x);
x = (x & m[i]) + ((x >> k) & m[i]);
printf("%b\n\n",m[i]);
k*=2;
}
printf("%b\n\n",x);
}
}
// 10111100011000110111111011111111