Created
October 22, 2012 16:46
-
-
Save whyrusleeping/3932483 to your computer and use it in GitHub Desktop.
Bit Fields and Unions
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> | |
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