第 15 章 位操作(dualview)

时间:2024-07-29 18:36:44
 /*-----------------------------------
dualview.c -- 位字段和按位运算符
-----------------------------------*/ #include <stdio.h>
#include <limits.h> //边框线样式
#define SOLID 0
#define DOTTED 1
#define DASHED 2 //三原色
#define BLUE 4
#define GREEN 2
#define RED 1 //混合颜色
#define BLACK (RED & GREEN & BLUE)
#define YELLOW (RED | GREEN)
#define MAGENTA (RED | BLUE)
#define CYAN (GREEN | BLUE)
#define WHITE (RED | BLUE | GREEN) //按位方法中用到的符号常量
#define OPAQUE 0x1
#define FILL_BLUE 0x8
#define FILL_GREEN 0x4
#define FILL_RED 0x2
#define FILL_MASK 0xE //掩码
#define BORDER 0x100
#define BORDER_BLUE 0x800
#define BORDER_GREEN 0x400
#define BORDER_RED 0x200
#define BORDER_MASK 0xE00
#define B_SOLID 0
#define B_DOTTED 0x1000
#define B_DASHED 0x2000
#define STYLE_MASK 0x3000 struct box_props
{
unsigned int opaque : ;
unsigned int fill_color : ;
unsigned int : ;
unsigned int show_border : ;
unsigned int border_color : ;
unsigned int border_style : ;
unsigned int : ;
}; //把数据看作结构或 unsigned short 类型的变量
union Views
{
struct box_props st_view;
unsigned short us_view;
}; const char *color[] =
{
"black", "red", "green", "yellow", "blue", "magenta", "cyan", "white"
}; void show_settings(const struct box_props *pb);
void show_setting1(unsigned short);
char* itobs(int n, char *ps);
void show_bstr(const char *str); int main()
{
printf("size of struct box_props is %d bit\n", CHAR_BIT * sizeof(struct box_props));
printf("size of unsigned short is %d bit\n", CHAR_BIT * sizeof(unsigned short)); //创建 Vies 联合,并初始化 initialize struct box view
union Views box = {{true, YELLOW, true, GREEN, DASHED}};
char bin_str[ * sizeof(unsigned int) + ]; printf("\nOriginal box settings:\n");
show_settings(&box.st_view);
printf("\nBox settings using unsigned int view:\n");
show_setting1(box.us_view);
//printf("bits are %s\n", itobs(box.us_view, bin_str));
printf("bits are:\n");
show_bstr(itobs(box.us_view, bin_str)); box.us_view &= ~FILL_MASK; //把表示填充色的位清零
box.us_view |= CYAN; //重置填充色
box.us_view ^= OPAQUE; //切换透明位
box.us_view &= ~BORDER_MASK; //边框颜色位清零
box.us_view |= BORDER_RED;
box.us_view &= ~STYLE_MASK; //样式位清零
box.us_view |= B_DOTTED; //边框样式设置为点
printf("\nModified box settings:\n");
show_settings(&box.st_view);
printf("\nBox settings using unsigned int view:\n");
show_setting1(box.us_view);
//printf("bits are %s\n", itobs(box.us_view, bin_str));
printf("bits are:\n");
show_bstr(itobs(box.us_view, bin_str)); return ;
} void show_settings(const struct box_props *pb)
{
printf("Box is %s.\n", pb->opaque ? "opaque" : "transparent");
printf("The fill color is %s.\n", color[pb->fill_color]);
printf("Border %s.\n", pb->show_border ? "shown" : "not shown");
printf("The border color is %s.\n", color[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("unknown type.\n");
break;
}
} void show_setting1(unsigned short us)
{
printf("Box is %s.\n", (us & OPAQUE) ? "opaque" : "transparent");
printf("The fill color is %s.\n", color[(us >> ) & 0x07]);
printf("Border %s.\n", (us & BORDER) == BORDER ? "shown" : "not shown");
printf("The border color is %s.\n", color[us >> & 0x07]); 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("unknown style");
break;
}
} char* itobs(int n, char *ps)
{
const static int size = CHAR_BIT * sizeof(int); for (int i(size - ); i >= ; --i, n >>= )
ps[i] = ( & n) + ''; ps[size] = '\0'; return ps;
} //以 4 位为一组,显示二进制字符串
void show_bstr(const char *str)
{
int i = ; while (str[i]) //str[i] 不是空字符
{
putchar(str[i]); if (++i % == && str[i])
putchar(' ');
} putchar('\n');
}

dualview.c

第 15 章 位操作(dualview)