Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // macros for the actual printing
- #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')
- // input
- int b = 1234123;
- binary_print(b, sizeof(b));
- // relevant source
- void binary_print(int data, size_t data_len)
- {
- size_t i;
- unsigned int byte_shift = data_len * 8;
- /*
- while (1) {
- printf(BYTE_TO_BINARY_PATTERN" ", BYTE_TO_BINARY(*data>>byte_shift));
- byte_shift = byte_shift-8;
- if (byte_shift == 0)
- break;
- }
- */
- for (i = 0; i < data_len; i++) {
- printf(BYTE_TO_BINARY_PATTERN" ", BYTE_TO_BINARY(data>>byte_shift));
- byte_shift = byte_shift-8;
- }
- printf("\n");
- }
- // output
- >>> ./a.out
- 11001011 00000000 00010010 11010100
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement