-
Notifications
You must be signed in to change notification settings - Fork 0
/
bit.c
66 lines (51 loc) · 1.13 KB
/
bit.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
#include <stdio.h>
int bitAnd(int x, int y) {
int var1 = ~x;
int var2 = ~y;
int var3 = var1 | var2;
int var4 = ~var3;
return var4;
}
int bitXor(int x, int y) {
int var1 = ~x;
int var2 = ~y;
int var3 = x & y;
int var4 = var1 & var2;
int var5 = ~var3 & ~var4;
return var5;
}
int isEqual(int x, int y) {
int var1 = x ^ y;
int var2 = !var1;
return var2;
}
int evenBits(void) {
int val1 = 0x55;
int val2 = (val1 << 8) + val1;
int val3 = (val2 << 16) + val2;
return val3;
}
int bitMask(int highbit, int lowbit) {
int val1 = (2<<highbit) + ~0;
int val2 = (1<<lowbit) + ~0;
return val1 & ~val2;
}
int bitParity(int x) {
int var1 = x;
var1 ^= (var1 >> 16);
var1 ^= (var1 >> 8);
var1 ^= (var1 >> 4);
var1 ^= (var1 >> 2);
var1 ^= (var1 >> 1);
return var1&1;
}
int main() {
printf("bitAnd(6, 5) = %i \n", bitAnd(6, 5));
printf("bitXor(4, 5) = %i \n", bitXor(4, 5));
printf("isEqual(5, 5) = %i \n", isEqual(5, 5));
printf("isEqual(4, 5) = %i \n", isEqual(4, 5));
printf("evenBits = 0x%X \n", evenBits());
printf("bitMask(5,3) = 0x%X \n", bitMask(5, 3));
printf("bitParity(5) = %i \n", bitParity(1));
return 0;
}