一个经典的位运算(忘记从哪搬过来的了,貌似不是自己写的)

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