Skip to content

Instantly share code, notes, and snippets.

@twopoint718
Created December 8, 2019 20:19
Show Gist options
  • Save twopoint718/2d711aa492fcab378791303eaa9a55e6 to your computer and use it in GitHub Desktop.
Save twopoint718/2d711aa492fcab378791303eaa9a55e6 to your computer and use it in GitHub Desktop.
Bitfields in C
#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