1
0

dualview.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* dualview.c -- bit fields and bitwise operators */
  2. #include <stdio.h>
  3. #include <stdbool.h>
  4. #include <limits.h>
  5. /* BIT-FIELD CONSTANTS */
  6. /* line styles */
  7. #define SOLID 0
  8. #define DOTTED 1
  9. #define DASHED 2
  10. /* primary colors */
  11. #define BLUE 4
  12. #define GREEN 2
  13. #define RED 1
  14. /* mixed colors */
  15. #define BLACK 0
  16. #define YELLOW (RED | GREEN)
  17. #define MAGENTA (RED | BLUE)
  18. #define CYAN (GREEN | BLUE)
  19. #define WHITE (RED | GREEN | BLUE)
  20. /* BITWISE CONSTANTS */
  21. #define OPAQUE 0x1
  22. #define FILL_BLUE 0x8
  23. #define FILL_GREEN 0x4
  24. #define FILL_RED 0x2
  25. #define FILL_MASK 0xE
  26. #define BORDER 0x100
  27. #define BORDER_BLUE 0x800
  28. #define BORDER_GREEN 0x400
  29. #define BORDER_RED 0x200
  30. #define BORDER_MASK 0xE00
  31. #define B_SOLID 0
  32. #define B_DOTTED 0x1000
  33. #define B_DASHED 0x2000
  34. #define STYLE_MASK 0x3000
  35. const char * colors[8] = {"black", "red", "green", "yellow",
  36. "blue", "magenta", "cyan", "white"};
  37. struct box_props {
  38. bool opaque : 1;
  39. unsigned int fill_color : 3;
  40. unsigned int : 4;
  41. bool show_border : 1;
  42. unsigned int border_color : 3;
  43. unsigned int border_style : 2;
  44. unsigned int : 2;
  45. };
  46. union Views /* look at data as struct or as unsigned short */
  47. {
  48. struct box_props st_view;
  49. unsigned short us_view;
  50. };
  51. void show_settings(const struct box_props * pb);
  52. void show_settings1(unsigned short);
  53. char * itobs(int n, char * ps);
  54. int main(void)
  55. {
  56. /* create Views object, initialize struct box view */
  57. union Views box = {{true, YELLOW , true, GREEN, DASHED}};
  58. char bin_str[8 * sizeof(unsigned int) + 1];
  59. printf("Original box settings:\n");
  60. show_settings(&box.st_view);
  61. printf("\nBox settings using unsigned int view:\n");
  62. show_settings1(box.us_view);
  63. printf("bits are %s\n",
  64. itobs(box.us_view,bin_str));
  65. box.us_view &= ~FILL_MASK; /* clear fill bits */
  66. box.us_view |= (FILL_BLUE | FILL_GREEN); /* reset fill */
  67. box.us_view ^= OPAQUE; /* toggle opacity */
  68. box.us_view |= BORDER_RED; /* wrong approach */
  69. box.us_view &= ~STYLE_MASK; /* clear style bits */
  70. box.us_view |= B_DOTTED; /* set style to dotted */
  71. printf("\nModified box settings:\n");
  72. show_settings(&box.st_view);
  73. printf("\nBox settings using unsigned int view:\n");
  74. show_settings1(box.us_view);
  75. printf("bits are %s\n",
  76. itobs(box.us_view,bin_str));
  77. return 0;
  78. }
  79. void show_settings(const struct box_props * pb)
  80. {
  81. printf("Box is %s.\n",
  82. pb->opaque == true ? "opaque": "transparent");
  83. printf("The fill color is %s.\n", colors[pb->fill_color]);
  84. printf("Border %s.\n",
  85. pb->show_border == true ? "shown" : "not shown");
  86. printf("The border color is %s.\n", colors[pb->border_color]);
  87. printf ("The border style is ");
  88. switch(pb->border_style)
  89. {
  90. case SOLID : printf("solid.\n"); break;
  91. case DOTTED : printf("dotted.\n"); break;
  92. case DASHED : printf("dashed.\n"); break;
  93. default : printf("unknown type.\n");
  94. }
  95. }
  96. void show_settings1(unsigned short us)
  97. {
  98. printf("box is %s.\n",
  99. (us & OPAQUE) == OPAQUE? "opaque": "transparent");
  100. printf("The fill color is %s.\n",
  101. colors[(us >> 1) & 07]);
  102. printf("Border %s.\n",
  103. (us & BORDER) == BORDER? "shown" : "not shown");
  104. printf ("The border style is ");
  105. switch(us & STYLE_MASK)
  106. {
  107. case B_SOLID : printf("solid.\n"); break;
  108. case B_DOTTED : printf("dotted.\n"); break;
  109. case B_DASHED : printf("dashed.\n"); break;
  110. default : printf("unknown type.\n");
  111. }
  112. printf("The border color is %s.\n",
  113. colors[(us >> 9) & 07]);
  114. }
  115. char * itobs(int n, char * ps)
  116. {
  117. int i;
  118. const static int size = CHAR_BIT * sizeof(int);
  119. for (i = size - 1; i >= 0; i--, n >>= 1)
  120. ps[i] = (01 & n) + '0';
  121. ps[size] = '\0';
  122. return ps;
  123. }