/* binary counting exampple -counts bits set to 1 in an 8 bit number */ /* acc -o bitcount bitcount.c on SUNS */ /* c89 -o bitcount bitcount.c on DECS */ include <stdio.h unsigned char bitcount(unsigned char); /* prototype */ main() { unsigned char i8,count; int i; printf("Enter number (0 - 255 decimal)\n); scanf(%d,&i); if (( i < 0 ) || (i 255)) { printf("Error:Number out of range = %d\n, i); exit(1); } i8 = (unsigned char) i; count = bitcount(i8); printf(\n\nNumber of bits set to 1 in %d = %d\n,i,count); } unsigned char bitcount(unsigned char x) { unsigned char count; for (count = 0; x!=0; x=1) if ( x & 01 ) ++count; return count; }
Bhopal news
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100