Skip to content

Instantly share code, notes, and snippets.

@bartman
Created September 15, 2011 14:38
Show Gist options
  • Save bartman/1219405 to your computer and use it in GitHub Desktop.
Save bartman/1219405 to your computer and use it in GitHub Desktop.
gcc bitfields
#include <stdio.h>
#include <stdint.h>
#define STRINGIFY_ARG(contents) #contents
int main(void)
{
union {
uint32_t word;
struct {
uint32_t b0:1;
uint32_t b1:1;
uint32_t b2:1;
uint32_t b3_4:2;
uint32_t b5_7:3;
uint32_t b8_31:24;
};
} x;
#define BIT(_b_) \
x.word = 0; \
x._b_ = -1; \
printf("" STRINGIFY_ARG(_b_) " -> %08x\n", x.word);
BIT(b0);
BIT(b1);
BIT(b2);
BIT(b3_4);
BIT(b5_7);
BIT(b8_31);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment