Last active
October 22, 2018 19:53
-
-
Save VehpuS/d7b7bacb743f7883029e7f8a37ecca07 to your computer and use it in GitHub Desktop.
reading packed struct bits created by VehpuS - https://repl.it/@VehpuS/reading-packed-struct-bits
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdint.h> | |
typedef struct __attribute__((__packed__)) data_block_t { | |
uint8_t a; | |
uint8_t b : 1; | |
uint8_t c : 1; | |
uint8_t d : 1; | |
uint8_t e : 1; | |
uint8_t f : 1; | |
uint8_t g : 1; | |
uint8_t h : 1; | |
uint8_t i : 1; | |
uint16_t j; | |
} data_block; | |
#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(void) { | |
data_block data = { | |
.a = 0, | |
.b = 1, | |
.d = 1, | |
.j = 3, | |
}; | |
printf(BYTE_TO_BINARY_PATTERN ":", BYTE_TO_BINARY(((uint8_t*) &data)[2])); | |
printf(BYTE_TO_BINARY_PATTERN ":", BYTE_TO_BINARY(((uint8_t*) &data)[1])); | |
printf(BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(((uint8_t*) &data)[0])); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment