-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bits.c
67 lines (55 loc) · 1.39 KB
/
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
/* Bitwise operations on input
* with binary and hex output
* July 3, 2022 */
#include <stdio.h>
void bits(size_t const size, void const * const ptr) {
unsigned char *b = (unsigned char *)ptr;
unsigned char byte;
int j;
for (int i = size-1; i >= 0; i--) {
for (j = 7; j >= 0; j--) {
byte = (b[i] >> j) & 1;
printf("%u%c", byte, (((j % 4) == 0) ? ' ' : 1));
}
}
printf("\t\t%x\t%x", *(b + 1), *b);
putchar('\n');
}
void operation(unsigned short op, const char *str) {
printf("\n%s\t%d\n= ", str, op);
bits(sizeof(op), &op);
}
int main(void) {
unsigned short a, b, c;
/* Set the variable c each time we call operation()
* to avoid stack smashing */
printf("Size of short: %ld\n", sizeof(short));
printf("Binary\t\t\t\t\t\tHex\n");
printf("Enter one number (a): ");
scanf("%hd", &a);
bits(sizeof(a), &a);
printf("Enter another number (b): ");
scanf("%hd", &b);
bits(sizeof(b), &b);
c = a & b; // a and b
operation(c, "a & b");
c = a | b; // a or b
operation(c, "a | b");
c = a ^ b;
operation(c, "a ^ b");
c = ~a; // negative a
operation(c, "~a\t");
c = ~b; // negative b
operation(c, "~b\t");
printf("\nShift a left 2 bits, then right 2\n");
c = a << 2;
operation(c, "a << 2");
c = a >> 2;
operation(c, "a >> 2");
printf("\nShift b left 2 bits, then right 2\n");
c = b << 2;
operation(c, "b << 2");
c = b >> 2;
operation(c, "b >> 2");
return 0;
}