-
Notifications
You must be signed in to change notification settings - Fork 5
/
print_binary.c
44 lines (37 loc) · 1.14 KB
/
print_binary.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
#include<stdio.h>
#define BYTE_TO_BINARY_PATTERN "%c%c%c%c%c%c%c%c"
#define BYTE_TO_BINARY(byte) \
(byte & 0x80 ? '1' : '0'), \
(byte & 0x40 ? '1' : '0'), \
(byte & 0x20 ? '1' : '0'), \
(byte & 0x10 ? '1' : '0'), \
(byte & 0x08 ? '1' : '0'), \
(byte & 0x04 ? '1' : '0'), \
(byte & 0x02 ? '1' : '0'), \
(byte & 0x01 ? '1' : '0')
int main() {
int i = 1;
float f = 1;
// sizes of these types won't be the same on every system
size_t int_size = sizeof(int);
size_t flt_size = sizeof(float);
// interpret the integer and float as arrays of bytes
char *int_ptr = (char *) &i;
char *flt_ptr = (char *) &f;
// loop through bytes and print all 8 bits individually
// assume little endian (loop in reverse to print with LSB on the right)
for (int b = int_size - 1; b >= 0; --b) {
char byte = *(int_ptr + b);
printf(BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(byte));
printf(" ");
}
printf("\n");
// same thing for float
for (int b = flt_size - 1; b >= 0; --b) {
char byte = *(flt_ptr + b);
printf(BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(byte));
printf(" ");
}
printf("\n");
return 0;
}