-
Notifications
You must be signed in to change notification settings - Fork 0
/
monbits.c
69 lines (63 loc) · 1.34 KB
/
monbits.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
int count1bits(int *val, int sbit, int ebit)
{
int mask, count = 0;
mask = 1 << (sbit-1);
while (sbit < ebit)
{
if (mask & *val) count++;
mask = mask << 1;
sbit++;
}
return count;
}
int testbit(int *add, int bit)
{
return ((1<<(bit-1)) & *add)?1:0;
}
int setbit(int *add, int bit)
{
int mask, ret;
mask = 1 << (bit-1);
ret = *add & mask;
*add = *add | mask;
return ret?1:0;
}
int clearbit(int *add, int bit)
{
int mask, ret;
mask = 1 << (bit-1);
ret = *add & mask;
if (ret) *add = *add - mask;
return ret?1:0;
}
int getbits(int *add, int sbit, int nbit)
{
int mask = 0, val = 0, loop;
mask = 1 << (sbit-1);
for (loop=0;loop<nbit;loop++)
{
val = val * 2;
if (*add & mask) val++;
mask = mask * 2;
}
return val;
}
int setbits(int *add, int sbit, int nbit, int new_mask)
{
int mask = 0, val = 0, loop;
mask = 1 << (sbit-1); /* mask for the lowest bit */
for (loop=0;loop<nbit;loop++)
{
/* double our value, then add 1 if it this bit is set in the # passed */
val = val * 2;
if (*add & mask) val++;
/* set bit in address if set in mask.. else clear it if it isn't clear */
if (new_mask & 1)
*add = *add | mask;
else
if (*add & mask) *add = *add - mask;
new_mask >> 1; /* next bit */
mask = mask * 2; /* mask the next bit */
}
return val;
}