Advertisement
Guest User

gcc union bit field example

a guest
Oct 30th, 2011
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. // gcc -Wall -o union union.c
  2.  
  3. #include <inttypes.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6.  
  7. typedef struct test
  8. {
  9. union
  10. {
  11. uint8_t reg8bit; // overall register
  12. struct
  13. {
  14. uint8_t bit05:6; // bits 0-5
  15. uint8_t bit68:3; // bits 6-8 .. ooops, too large
  16. } regs;
  17. } REG0;
  18.  
  19. union
  20. {
  21. uint8_t reg8bit; // overall register
  22. struct
  23. {
  24. uint8_t bit05:6; // bits 0-5
  25. uint8_t bit68:3; // bits 6-8 .. ooops, too large
  26. } regs;
  27. } REG1;
  28. } test_t;
  29.  
  30.  
  31. int main(void)
  32. {
  33. test_t * t = (test_t*)malloc(20);
  34. printf("sizeof(test_t): %d\n", sizeof(test_t));
  35.  
  36. printf("t: 0x%p\n", t);
  37. printf("reg0: 0x%p\n", &t->REG0);
  38. printf("reg1: 0x%p\n", &t->REG1);
  39. printf("sizeof(t): %d\n", sizeof(t));
  40.  
  41.  
  42. return 0;
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement