第 15 章 位操作(fields)

图片 1图片 2

图片 3图片 4

 1 /*------------------------------------
 2     binbit.c -- 使用位操作显示二进制
 3 ------------------------------------*/
 4 
 5 #include <stdio.h>
 6 #include <limits.h>        //提供 CHAR_BIT 的定义,CHAR_BIT 表示每字节的位数
 7 
 8 char* itobs(int, char*);
 9 void show_bstr(const char*);
10 
11 int main()
12 {
13     char bin_str[CHAR_BIT * sizeof(int) + 1];
14     int number;
15 
16     puts("Enter integers and see them in binary.");
17     puts("Non-numeric input terminates program");
18 
19     while (scanf("%d", &number) == 1)
20     {
21         itobs(number, bin_str);
22         
23         printf("%d is ", number);
24 
25         show_bstr(bin_str);
26 
27         putchar('\n');
28     }
29 
30     puts("Bye");
31 
32     return 0;
33 }
34 
35 char* itobs(int n, char *ps)
36 {
37     const static int size = CHAR_BIT * sizeof(int);
38 
39     for (int i(size - 1); i >= 0; --i, n >>= 1)
40         ps[i] = (01 & n) + '0';
41     ps[size] = '\0';
42 
43     return ps;
44 }
45 
46 //以4位为一组,显示二进制字符串
47 void show_bstr(const char *str)
48 {
49     int i = 0;
50     while (str[i])        //不是空字符
51     {
52         fputc(str[i], stdout);
53 
54         if (++i % 4 == 0 && str[i])
55             fputc(' ', stdout);
56     }
57 }
 1 /*-----------------------------------
 2     fields.c -- 定义并使用字段
 3 -----------------------------------*/
 4 
 5 #include <stdio.h>
 6 
 7 //线的样式
 8 #define SOLID    0
 9 #define DOTTED    1
10 #define DASHED    2
11 
12 //三原色
13 #define BLUE    4
14 #define GREEN    2
15 #define RED        1
16 
17 //混合色
18 #define BLACK    (BLUE & GREEN & RED)
19 #define YELLOW    (RED | GREEN)
20 #define MAGENTA    (RED | BLUE)
21 #define CYAN    (GREEN | BLUE)
22 #define WHITE    (RED | GREEN | BLUE)
23 
24 const char *colors[] =
25 {
26     "black", "red", "green", "yellow", "blue", "magenta", "cyan", "white"
27 };
28 
29 struct box_props
30 {
31     unsigned int opaque : 1;
32     unsigned int fill_color : 3;
33     unsigned int : 4;
34     unsigned int show_border : 1;
35     unsigned int border_color : 3;
36     unsigned int border_style : 2;
37     unsigned int : 2;
38 };
39 
40 void show_settings(const struct box_props *pb);
41 
42 int main()
43 {
44     //创建并初始化 box_props 结构
45     struct box_props box = {true, YELLOW, true, GREEN, DASHED};
46 
47     printf("Original box settings:\n");
48     show_settings(&box);
49 
50     box.opaque = false;
51     box.fill_color = WHITE;
52     box.border_color = MAGENTA;
53     box.border_style = SOLID;
54 
55     printf("\nModified box settings:\n");
56     show_settings(&box);
57 
58     return 0;
59 }
60 
61 void show_settings(const struct box_props *pb)
62 {
63     printf("Box is %s.\n", pb->opaque ? "opaque" : "transparent");
64 
65     printf("The fill color is %s.\n", colors[pb->fill_color]);
66 
67     printf("Border %s.\n", pb->show_border ? "shown" : "not shown");
68 
69     printf("The border color is %s.\n", colors[pb->border_color]);
70 
71     printf("The border style is ");
72     switch (pb->border_style)
73     {
74     case SOLID:
75         printf("solid.\n");
76         break;
77 
78     case DOTTED:
79         printf("dotted.\n");
80         break;
81 
82     case DASHED:
83         printf("dashed.\n");
84         break;
85 
86     default:
87         printf("unknown type.\n");
88         break;
89     }
90 }

binbit.c

fields.c

图片 5

图片 6

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图