-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathllvm_cttz_in_bitops.c
More file actions
57 lines (40 loc) · 889 Bytes
/
llvm_cttz_in_bitops.c
File metadata and controls
57 lines (40 loc) · 889 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
static unsigned extractBit(unsigned short input, unsigned index)
{
assert(index < sizeof(input) * 8 );
return (0x1 & (input >> index));
}
static unsigned countOnes(unsigned short input)
{
size_t width = sizeof(input) * 8;
unsigned count = 0;
unsigned i;
for(i = 0; i< width; i++){
count += extractBit(input, i);
}
return count;
}
int main(void)
{
unsigned short x;
/* x = 0xC8; 11001000 3*/
x = 0xC0; /* 11000000 6*/
/* cttz should be 3 */
size_t width = sizeof(unsigned short) * 8;
unsigned half = width/2;
unsigned ones = 0;
unsigned i,j;
unsigned result;
for( i = 0, j = 0; j < width; i++){
j = 1<<i;
x = (x | (x<<j));
}
printf("%x\n",x);
ones = countOnes(x);
printf("ones is %d\n", ones);
result = width - ones;
printf("result = %u\n", result);
assert(result == 6);
}