Created
December 8, 2019 20:19
-
-
Save twopoint718/2d711aa492fcab378791303eaa9a55e6 to your computer and use it in GitHub Desktop.
Bitfields in C
This file contains hidden or 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 { | |
uint8_t foo :4; | |
uint8_t :3; | |
uint8_t bar :1; | |
} foobar; | |
int main(int argc, char** argv) { | |
foobar f = { | |
.foo = 0xf, | |
.bar = 0x1 | |
}; | |
printf("foo:\t%d\nbar:\t%d", f.foo, f.bar); | |
/* prints: | |
* | |
* foo: 15 | |
* bar: 1 | |
*/ | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment