Created
June 8, 2021 03:25
-
-
Save neosarchizo/fdabd87b3e48a6accae3c4afd530c7af to your computer and use it in GitHub Desktop.
C++ Bit fields example
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 union | |
{ | |
uint8_t command; | |
struct | |
{ | |
uint8_t a:1; | |
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; | |
} bits; | |
} tsc2046_command; | |
int main() | |
{ | |
tsc2046_command touch_get_y; | |
touch_get_y.command = 0b11010010; | |
printf("a: %d\r\n", touch_get_y.bits.a); | |
printf("b: %d\r\n", touch_get_y.bits.b); | |
printf("c: %d\r\n", touch_get_y.bits.c); | |
printf("d: %d\r\n", touch_get_y.bits.d); | |
printf("e: %d\r\n", touch_get_y.bits.e); | |
printf("f: %d\r\n", touch_get_y.bits.f); | |
printf("g: %d\r\n", touch_get_y.bits.g); | |
printf("h: %d\r\n", touch_get_y.bits.h); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment