c primer plus----第15章 位操作
#include <stdio.h> #define YES 1 #define NO 0 #define SOLID 0 #define DOTTED 1 //有点的 #define DASHED 2 //虚线 #define BULE 4 #define GREEN 2 #define RED 1 #define BLACK 0 #define YELLOW (RED | GREEN) #define MAGENTA (RED | BULE) #define CYAN (GREEN | BLUE) #define WHITE (RED | GREEN | BULE) #define OPAQUE 0x1 #define FILL_BLUE 0x8 //#define FILL_BLUE 1<<3 #define FILL_GREEN 0x4 //#define FILL_GREEN 1<<2 #define FILL_RED 0x2 #define FILL_MASK 0xE #define BORDER 0x100 #define BORDER_BLUE 0x800 //#define BORDER_BLUE 1<<11 #define BORDER_GREEN 0x400 #define BORDER_RED 0x200 //define BORDER_RED 0010 0000 0000 #define BORDER_MASK 0xE00 #define B_SOLID 0 #define B_DOTTED 0x1000 //#define B_DOTTED 0001 0000 0000 0000 #define B_DASHED 0x2000 #define STYLE_MASK 0x3000 //#define STYLE_MASK 0011 0000 0000 0000 const char * colors[8] = { "black", "red", "green", "yellow", "blue", "magenta", "cyan", "white" }; struct box_props { unsigned int opaque : 1;//1 unsigned int fill_color : 3;//011 unsigned int : 4;//0000 unsigned int show_border : 1;//1 unsigned int border_color : 3;//010 unsigned int border_style : 2;//010 unsigned int : 2;//00 // 00010010100000111 //00000000000000000010010100000111 }; union Views { struct box_props st_view; unsigned int ui_view; }; void show_settings(const struct box_props * pb); void show_settings1(unsigned short us); char * itobs(int n, char * ps); //0-9对应0-9;A-F对应10-15 //0 1 2 3 4 5 6 7 8 9 A B C D E F int main(void) { union Views box = {YES, YELLOW, YES, GREEN, DASHED}; char bin_str[8 * sizeof(unsigned int) + 1]; printf("Original box settings:\n"); show_settings(&box.st_view); printf("\nBox settings using unsigned int view:\n"); show_settings1(box.ui_view); //00000000000000000010010100000111 printf("bits are %s\n", itobs(box.ui_view, bin_str)); box.ui_view &= ~FILL_MASK; //把代表填充颜色的位清0 //~1110 0001 ////00000000000000000010010100000001 printf("bits are %s\n", itobs(box.ui_view, bin_str)); box.ui_view |= (FILL_BLUE | FILL_GREEN);//重置填充色 1000 | 100 = 1100 //00000000000000000010010100001101 printf("bits are %s\n", itobs(box.ui_view, bin_str)); box.ui_view ^= OPAQUE;//转置指示是否透明的位 //00000000000000000010010100001101 //00000000000000000000000000000001 //00000000000000000010010100001100 printf("bits are %s\n", itobs(box.ui_view, bin_str)); box.ui_view |= BORDER_RED;//错误的方法 //00000000000000000000001000000000 //00000000000000000010011100001100 box.ui_view &= ~STYLE_MASK;//清除样式位 //00000000000000000011000000000000 //11111111111111111100111111111111 //00000000000000000010011100001100 //00000000000000000000011100001100 printf("bits are %s\n", itobs(box.ui_view, bin_str)); box.ui_view |= B_DOTTED;//把样式设置为点 //00000000000000000000011100001100 //00000000000000000001000000000000 //00000000000000000001011100001100 printf("\nModified box settings:\n"); show_settings(&box.st_view); printf("\nBox settings using unsigned int view:\n"); show_settings1(box.ui_view); printf("bits are %s\n", itobs(box.ui_view, bin_str)); return 0; } char * itobs(int n, char * ps) { int i; static int size = 8 * sizeof(unsigned int); for (i = size - 1; i >= 0; i--, n >>= 1) //31 --->0 ps[i] = (01 & n) + '0'; ps[size] = '\0'; return ps; } void show_settings1(unsigned short us) { //00000000000000000010010100000111 // 111 printf("Box is %s.\n", us & OPAQUE == OPAQUE ? "opaque" : "transparent"); printf("The fill color is %s.\n", colors[(us >> 1) & 07]); printf("Border %s.\n", us & BORDER == BORDER ? "shown" : "not shown"); printf("The border style is "); switch (us & STYLE_MASK) { case B_SOLID: printf("solid.\n"); break; case B_DOTTED: printf("dotted.\n"); break; case B_DASHED: printf("dashed.\n"); break; default: printf("unknow type.\n"); break; } printf("The border color is %s.\n",colors[(us >> 9) & 07]); } void show_settings(const struct box_props * pb) { printf("Box is %s.\n", pb->opaque == YES ? "opaque" : "transparent"); printf("The fill color is %s.\n", colors[pb->fill_color]); printf("Border %s.\n", pb->show_border == YES ? "shown" : "not shown"); printf("The border color is %s.\n", colors[pb->border_color]); printf("The border style is "); switch (pb->border_style) { case SOLID: printf("solid.\n"); break; case DOTTED: printf("dotted.\n"); break; case DASHED: printf("dashed.\n"); break; default: printf("unknow type.\n"); break; } }