-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbit_utils.hpp
More file actions
37 lines (31 loc) · 783 Bytes
/
bit_utils.hpp
File metadata and controls
37 lines (31 loc) · 783 Bytes
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
#include <stdio.h>
template <class T>
char* as_bytes(T& t) {
return *((char**)&t);
}
/// Print object binary representation (bits)
template <class T>
void bit_print(
const T& t,
const size_t base = 2,
const size_t bytes_per_row = 4) {
// TODO print T's name
char* c = *((char**)&t);
for (size_t byte = 0; byte < sizeof(T); ++byte) {
for (size_t bit = 1 << 7; bit; bit >>= 1) {
if (bit & c[byte]) {
putchar('1');
} else {
putchar('0');
}
if (bit == 1 << 4) {
putchar('\'');
}
}
if ((byte + 1) % bytes_per_row == 0) {
putchar('\n');
} else {
putchar(' ');
}
}
}