-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbits.c
More file actions
67 lines (55 loc) · 1.33 KB
/
bits.c
File metadata and controls
67 lines (55 loc) · 1.33 KB
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
https://powcoder.com
代写代考加微信 powcoder
Assignment Project Exam Help
Add WeChat powcoder
// bits.c ... functions on bit-strings
// part of Multi-attribute Linear-hashed Files
// Bit-strings are 32-bit unsigned quantities
// Last modified by John Shepherd, July 2019
#include <assert.h>
#include "bits.h"
// check if the bit at position is 1
int bitIsSet(Bits val, int position)
{
assert(0 <= position && position <= 31);
Bits mask = (1 << position);
return ((val & mask) != 0);
}
// set the bit at position to 1
Bits setBit(Bits val, int position)
{
assert(0 <= position && position <= 31);
Bits mask = (1 << position);
return (val | mask);
}
// set the bit at position to 0
Bits unsetBit(Bits val, int position)
{
assert(0 <= position && position <= 31);
Bits mask = (~(1 << position));
return (val & mask);
}
// extract just lower-order n bits
Bits getLower(Bits b, int n)
{
assert(1 <= n && n <= 32);
int i; Bits mask = 0;
for (i = 0; i < n; i++) mask |= (1<<i);
return b&mask;
}
// convert 32-bit unsigned quantity to string
// place in a user-supplied buffer of length > 36
void bitsString(Bits val, char *buf)
{
int i,j; char ch;
Bits bit = 0x80000000;
i = j = 0;
while (bit != 0) {
ch = ((val & bit) != 0) ? '1' : '0';
buf[i++] = ch;
j++;
if (j%8 == 0) buf[i++] = ' ';
bit = bit >> 1;
}
buf[--i] = '\0';
}