示例:位字段与位运算

作者:追风剑情 发布于:2020-4-13 10:47 分类:C

示例:位字段与位运算

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. //提供CHAR_BIT的定义,CHAR_BIT表示每字节的位数
  6. #include <limits.h>
  7. //C99定义了bool、true、false
  8. #include <stdbool.h>
  9.  
  10. /* 线的样式 */
  11. #define SOLID 0 //实线
  12. #define DOTTED 1 //点线
  13. #define DASHED 2 //虚线
  14.  
  15. /* 三原色 */
  16. #define BLUE 4
  17. #define GREEN 2
  18. #define RED 1
  19. /* 混合色 */
  20. #define BLACK 0
  21. #define YELLOW (RED | GREEN)
  22. #define MAGENTA (RED | BLUE)
  23. #define CYAN (GREEN | BLUE)
  24. #define WHITE (RED | GREEN | BLUE)
  25.  
  26. /* 按位方法中用到的符号常量 (也可以将下面的常量声明为枚举) */
  27. #define OPAQUE 0x1
  28. #define FILL_BLUE 0x8
  29. #define FILL_GREEN 0x4
  30. #define FILL_RED 0x2
  31. #define FILL_MASK 0xE
  32. #define BORDER 0x100
  33. #define BORDER_BLUE 0x800
  34. #define BORDER_GREEN 0x400
  35. #define BORDER_RED 0x200
  36. #define BORDER_MASK 0xE00
  37. #define B_SOLID 0
  38. #define B_DOTTED 0x1000
  39. #define B_DASHED 0x2000
  40. #define STYLE_MASK 0x3000
  41.  
  42. const char * colors[8] = {"black", "red", "green", "yellow","blue", "magenta", "cyan", "white"};
  43.  
  44. //声明一个描述方框属性的结构体
  45. //注意:不同的机器,存储位字段的顺序不同。
  46. struct box_props {
  47. bool opaque : 1; //是否透明; 或者 unsigned int (C99以前)
  48. unsigned int fill_color : 3;//填充色
  49. unsigned int : 4;
  50. bool show_border : 1;//是否显示边框; 或者 unsigned int (C99以前)
  51. unsigned int border_color : 3; //边框颜色
  52. unsigned int border_style : 2; //边框样式
  53. unsigned int : 2;
  54. };
  55.  
  56. /* 把数据看作结构或unsigned short类型的变量 */
  57. union Views
  58. {
  59. struct box_props st_view;
  60. unsigned short us_view;
  61. };
  62.  
  63. void show_settings(const struct box_props * pb);
  64. void show_settings1(unsigned short);
  65. char * itobs(int n, char * ps);
  66.  
  67. int main(int argc, char* argv[])
  68. {
  69. /* 创建Views联合,并初始化initialize struct box view */
  70. union Views box = { {true, YELLOW, true, GREEN, DASHED} };
  71. char bin_str[8 * sizeof(unsigned int) + 1];
  72.  
  73. printf("Original box settings:\n");
  74. show_settings(&box.st_view);
  75. printf("\nBox settings using unsigned int view:\n");
  76. show_settings1(box.us_view);
  77.  
  78. printf("bits are %s\n",
  79. itobs(box.us_view, bin_str));
  80. box.us_view &= ~FILL_MASK; /* 把表示填充色的位清0 */
  81. box.us_view |= (FILL_BLUE | FILL_GREEN); /* 重置填充色 */
  82. box.us_view ^= OPAQUE; /* 切换是否透明的位 */
  83. box.us_view |= BORDER_RED; /* 错误的方法 */
  84. box.us_view &= ~STYLE_MASK; /* 把样式位清0 */
  85. box.us_view |= B_DOTTED; /* 把样式设置为点 */
  86. printf("\nModified box settings:\n");
  87. show_settings(&box.st_view);
  88. printf("\nBox settings using unsigned int view:\n");
  89. show_settings1(box.us_view);
  90. printf("bits are %s\n",
  91. itobs(box.us_view, bin_str));
  92.  
  93. system("pause");
  94. return 0;
  95. }
  96.  
  97. void show_settings(const struct box_props * pb)
  98. {
  99. printf("Box is %s.\n",
  100. pb->opaque == true ? "opaque" : "transparent");
  101. printf("The fill color is %s.\n", colors[pb->fill_color]);
  102. printf("Border %s.\n",
  103. pb->show_border == true ? "shown" : "not shown");
  104. printf("The border color is %s.\n", colors[pb->border_color]);
  105. printf("The border style is ");
  106. switch (pb->border_style)
  107. {
  108. case SOLID: printf("solid.\n"); break;
  109. case DOTTED: printf("dotted.\n"); break;
  110. case DASHED: printf("dashed.\n"); break;
  111. default: printf("unknown type.\n");
  112. }
  113. }
  114.  
  115. void show_settings1(unsigned short us)
  116. {
  117. printf("Box is %s.\n",
  118. (us & OPAQUE) == OPAQUE ? "opaque" : "transparent");
  119. printf("The fill color is %s.\n",
  120. colors[(us >> 1) & 07]);
  121. printf("Border %s.\n",
  122. (us & BORDER) == BORDER ? "shown" : "not shown");
  123. printf("The border style is ");
  124. switch (us & STYLE_MASK)
  125. {
  126. case B_SOLID: printf("solid.\n"); break;
  127. case B_DOTTED: printf("dotted.\n"); break;
  128. case B_DASHED: printf("dashed.\n"); break;
  129. default: printf("unknown type.\n");
  130. }
  131. printf("The border color is %s.\n",
  132. colors[(us >> 9) & 07]);
  133. }
  134.  
  135. char * itobs(int n, char * ps)
  136. {
  137. int i;
  138. const static int size = CHAR_BIT * sizeof(int);
  139. for (i = size - 1; i >= 0; i--, n >>= 1)
  140. //01代表八进制1
  141. ps[i] = (01 & n) + '0';//1 + '0' 表示 '1'
  142. ps[size] = '\0';
  143. return ps;
  144. }

运行测试

1111.png

标签: C语言

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号