Skip to content

Instantly share code, notes, and snippets.

@whyrusleeping
Created October 22, 2012 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whyrusleeping/3932483 to your computer and use it in GitHub Desktop.
Save whyrusleeping/3932483 to your computer and use it in GitHub Desktop.
Bit Fields and Unions
#include <stdio.h>
typedef struct a
{
unsigned short q : 4;
unsigned short w : 2;
unsigned short e : 4;
unsigned short r : 6;
}A;
typedef struct b
{
unsigned short q : 4;
unsigned short w : 3;
unsigned short e : 5;
unsigned short r : 4;
}B;
typedef union test
{
B second;
A first;
}testU;
int main()
{
testU n;
n.second.e = 25;
n.second.q = 11;
n.second.w = 5;
n.second.r = 13;
printf("%d %d %d %d\n",n.first.q,n.first.w,n.first.e,n.first.r);
printf("%d %d %d %d\n",n.second.q,n.second.w,n.second.e,n.second.r);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment