-
Notifications
You must be signed in to change notification settings - Fork 0
/
count_set_bits.c
79 lines (67 loc) · 1.69 KB
/
count_set_bits.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int count_set_bits(int number)
{
int count = 0;
int mask = 1;
int i;
clock_t start, end;
struct timespec time1, time2;
// start = clock();
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
for (i=0; i<sizeof(int)*8; i++)
{
count += (number>>i) & mask;
}
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
//end = clock();
//double time_taken = (double)(end - start);
struct timespec time_taken = diff(time1, time2);
printf("Ticks taken: %f\n",time_taken);
return count;
}
int cnt_set_bits(int number)
{
int count=0;
clock_t start, end;
struct timespec time1, time2;
start = clock();
while(number)
{
count++;
number &= number-1;
}
end = clock();
double time_taken = (double)(end - start);
printf("Ticks taken: %f\n",time_taken);
return count;
}
struct timespec diff(struct timespec start, struct timespec end)
{
struct timespec temp;
if ((end.tv_nsec-start.tv_nsec)<0) {
temp.tv_sec = end.tv_sec-start.tv_sec-1;
temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
} else {
temp.tv_sec = end.tv_sec-start.tv_sec;
temp.tv_nsec = end.tv_nsec-start.tv_nsec;
}
return temp;
}
int main(int argc, char **argv)
{
int count=0;
int number;
if (argc<2)
{
printf("Please supply the desired number!\n");
return -1;
}
number=atoi(argv[1]);
count=count_set_bits(number);
printf("number of set bits in: %d is: %d\n",number, count);
count=cnt_set_bits(number);
printf("number of set bits in: %d is: %d\n",number, count);
return 0;
}