争渡

Be my self

那些不明白
还是不明白
我还是不会
为了谁失去自由自在
我想你明白
我想你明白
  menu
55 文章
0 浏览
1 当前访客
ღゝ◡╹)ノ❤️

《C Primer Plus》手记三

  

C Primer Plus 手记一

C Primer Plus 手记二

13.位操作

 1编号
 20 0 0 0  0 0 0 0
 3^ ^ ^ ^  ^ ^ ^ ^
 47 6 5 4  3 2 1 0 
 5
 67为最高位,0为最低位'
 77-0 每个位置为1分别代表为
 8128 64 32 16 8 4 2 1
 9所以一个bytes最大为 128+64+32+16+8+4+2+1 = 255
10最小为 0000000 = 0
11
12一个 byte 8位
13[] [] [] [] [] [] [] [] 
1411111111 = 255
1500000000 = 0
16所以 单字节 取值范围为 0-255
1高阶位存储符号
21000 0001 = -1	1111 1111 = -127	1000 0000 = -0
30000 0001 =  1	0111 1111 = +127	0000 0000 = +0
1正负0的问题可以用二进制补码来解决
2正数的反码和补码都与原码相同。   
3负数的反码为对该数的原码除符号位外各位取反。   
4负数的补码为对该数的原码除符号位外各位取反,然后在最后一位加1

  二进制运算符

 1// 取反
 2~(10011010) -> 01100101
 3unsigned char a = 2(0000 0010)
 4~a = 253 = 255-2 = (1111 1101)
 5
 6// & 1对1 = 1 1对0 0对0 得 0
 7(10010011) & (00111101) = 00010001 = 15
 8
 9// |  有1为1
10(10010011) | (00111101) = 10111111
11
12// ^ 相应位的只有一位为1 则为1
13(1001 0011) ^ (0011 1101) = 1010 1110

  打开位

  实际上的用法,比如打开扬声器需要在其他位不变的情况下打开 1 号
sound = sound | MASK
MASK 可以为 1000 0000 这样可以保证其他位不变,1 号位为 1

  关闭位
关闭 1 号位可以
sound = sound & ~MASK sound = sound & 0111 1111 这样 1 号位无论如何都会是 0,其他位不变
二进制的每一位可能在底层驱动都是一个开关操作。所以在底层用的较多一些。

  切换位
打开已经关闭的位 或者 关闭已经打开的位
sound = sound ^ MASK
·0000 1111 ^ 1011 0110 = 1011 1001

  检查位的值
if( (sound & MASK) == MASK) puts("WOW!");

  移位运算
1000 1010 <<2 = 0010 1000

  三原色解析

 1#define BYTE_MASK 0XFF
 2
 3int main(){
 4  // R           G           B
 5  // 0010 1010 / 0001 0110 / 0010 1111
 6  unsigned long color = 0X002A162f;
 7  unsigned char blue,green,red;
 8  // 0010 1010 / 0001 0110 / 0010 1111
 9  red = color & BYTE_MASK;
10  // 0001 0110 / 0010 1111
11  green = color >> 8 & BYTE_MASK;
12  // 0010 1111
13  blue = color >> 16 & BYTE_MASK;
14  return 0;
15}

  数值转二进制字符串

 1#include <limits.h>
 2#include <stdio.h>
 3
 4char *itobs(int, char *);
 5void show_bstr(const char *);
 6
 7int main(void)
 8{
 9  char bin_str[CHAR_BIT * sizeof(int) + 1];
10  int number;
11  puts("Enter integers and see them in binary.");
12  puts("Non-numeric input terminates program.");
13  while (scanf("%d", &number) == 1)
14  {
15    itobs(number, bin_str);
16    printf("%d is ", number);
17    show_bstr(bin_str);
18    putchar('\n');
19  }
20  puts("Bye!");
21  return 0;
22}
23
24char *itobs(int n, char *ps)
25{
26  int i;
27  const static int size = CHAR_BIT * sizeof(int);
28
29  for (i = size - 1; i >= 0; i--, n >>= 1)
30  {
31    // i=31; ps[31] = 0001 & 0101 = 0001 +'0' = '1'; n = 0101 =5 ; n>>=1 = 0010 = 2
32    // i=30; ps[30] = 0001 & 0010 = 0000 +'0' = '0'; n = 0010 =2 ; n>>=1 = 0001 = 1
33    // i=29; ps[29] = 0001 & 0001 = 0001 +'0' = '1'; n = 0001 =1 ; n>>=1 = 0000 = 0
34    ps[i] = (01 & n) + '0';
35    printf("i = %d; ps[i] = %c; n = %d; n>>1 = %d;\n", i, ps[i], n, n >> 1);
36  }
37  ps[size] = '\0';
38  return ps;
39}
40
41void show_bstr(const char *str)
42{
43  int i = 0;
44  while (str[i])
45  {
46    putchar(str[i]);
47    if (++i % 4 == 0 && str[i])
48    {
49      putchar(' ');
50    }
51  }
52}
53

  后四位取反

 1#include <limits.h>
 2#include <stdio.h>
 3
 4char *itobs(int n, char *ps);
 5void show_bstr(const char *str);
 6int invert_end(int num, int bits);
 7
 8int main(void) {
 9  char bin_str[CHAR_BIT * sizeof(int) + 1];
10  int number;
11  puts("Enter integers and see them in binary.");
12  puts("Non-numeric input terminates program.");
13  while (scanf("%d", &number) == 1) {
14    itobs(number, bin_str);
15    printf("%d is \n", number);
16    show_bstr(bin_str);
17    putchar('\n');
18    number = invert_end(number, CHAR_BIT * sizeof(int));
19    printf("Inverting the last 4 bits gives\n");
20    show_bstr(itobs(number, bin_str));
21    puts("\n");
22  }
23  puts("Bye!");
24  return 0;
25}
26
27char *itobs(int n, char *ps) {
28  int i;
29  const static int size = CHAR_BIT * sizeof(int);
30  for (i = size - 1; i >= 0; i--, n >>= 1) {
31    ps[i] = (01 & n) + '0';
32  }
33  ps[size] = '\0';
34  return ps;
35}
36
37void show_bstr(const char *str) {
38  int i = 0;
39  while (str[i]) {
40    putchar(str[i]);
41    if (++i % 4 == 0 && str[i]) {
42      putchar(' ');
43    }
44  }
45}
46
47int invert_end(int num, int bits) {
48  int mask = 0;
49  int bitval = 1;
50  while (bits-- > 0) {
51    mask |= bitval;
52    bitval <<= 1;
53  }
54
55  return num ^ mask;
56}

  三原色

 1#include <stdbool.h>
 2#include <stdio.h>
 3
 4#define SOLID 0
 5#define DOTTED 1
 6#define DASHED 2
 7// 三原色
 8#define BLUE 4
 9#define GREEN 2
10#define RED 1
11#define BLACK 0
12// 混合色
13// 0001 | 0010 = 0011 = 3
14#define YELLOW (RED | GREEN)
15// 0001 | 0100 = 0101 = 5
16#define MAGENTA (RED | BLUE)
17// 0010 | 0100 = 0110 = 6
18#define CYAN (GREEN | BLUE)
19// 0001 | 0010 | 0100 = 0111 = 7
20#define WHITE (RED | GREEN | BLUE)
21
22const char *colors[8] = {"black", "red", "green", "yellow",
23                         "blue", "magenta", "cyan", "white"};
24// :number 位数,用位的结构体
25struct box_props
26{
27  bool opaque : 1;
28  unsigned int fill_color : 3;
29  unsigned int : 4;
30  bool show_border : 1;
31  unsigned int border_color : 3;
32  unsigned int border_style : 2;
33  unsigned int : 2;
34};
35
36void show_settings(const struct box_props *pb);
37
38int main(void)
39{
40  /* 创建并初始化 box_props 结构 */
41  struct box_props box = {true, YELLOW, true, GREEN, DASHED};
42  printf("Original box settings:\n");
43  show_settings(&box);
44  box.opaque = false;
45  box.fill_color = WHITE;
46  box.border_color = MAGENTA;
47  box.border_style = SOLID;
48  printf("\nModified box settings:\n");
49  show_settings(&box);
50  return 0;
51}
52
53void show_settings(const struct box_props *pb)
54{
55  printf("Box is %s.\n", pb->opaque == true ? "opaque" : "transparent");
56  printf("The fill color is %s.\n", colors[pb->fill_color]);
57}
58
  1#include <limits.h>
  2#include <stdbool.h>
  3#include <stdio.h>
  4
  5// 位字段常量
  6// 边框线样式
  7#define SOLID 0
  8#define DOTTED 1
  9#define DASHED 2
 10
 11// 三原色
 12#define BLUE 4
 13#define GREEN 2
 14#define RED 1
 15
 16// 混合颜色
 17#define BLACK 0
 18#define YELLOW (RED | GREEN)
 19#define MAGENTA (RED | BLUE)
 20#define CYAN (GREEN | BLUE)
 21#define WHITE (RED | GREEN | BLUE)
 22
 23// 按位方法中用到的符号常量
 24#define OPAQUE 0x1
 25#define FILL_BLUE 0x8
 26#define FILL_GREEN 0x4
 27#define FILL_REDD 0x4
 28#define FILL_MASK 0xE
 29#define BORDER 0x100
 30#define BORDER_BLUE 0x800
 31#define BORDER_RED 0x200
 32#define BORDER_GREEN 0x400
 33#define BORDER_MASK 0xE00
 34#define B_SOLID 0
 35#define B_DOTTED 0x1000
 36#define B_DASHED 0x2000
 37#define STYLE_MASK 0x3000
 38
 39const char *colors[8] = {"black", "red",     "green", "yellow",
 40                         "blue",  "magenta", "cyan",  "white"};
 41
 42struct box_props {
 43  bool opaque : 1;
 44  unsigned int fill_color : 3;
 45  unsigned int : 4;
 46  bool show_border : 1;
 47  unsigned int border_color : 3;
 48  unsigned int border_style : 2;
 49  unsigned int : 2;
 50};
 51
 52union Views {
 53  struct box_props st_view;
 54  unsigned short us_view;
 55};
 56
 57void show_settings(const struct box_props *pb);
 58void show_settings1(unsigned short);
 59char *itobs(int n, char *ps);
 60
 61int main(void) {
 62  //创建Views联合,并初始化initialize struct box view
 63  union Views box = {{true, YELLOW, true, GREEN, DASHED}};
 64  char bin_str[8 * sizeof(unsigned int) + 1];
 65  printf("Original box settings:\n");
 66  show_settings(&box.st_view);
 67  printf("\nBoxSettings using unsigned int view:\n");
 68  show_settings1(box.us_view);
 69  printf("bits are %s\n", itobs(box.us_view, bin_str));
 70  box.us_view &= ~FILL_MASK;
 71  box.us_view |= (FILL_BLUE | FILL_GREEN);
 72  box.us_view ^= OPAQUE;
 73  box.us_view |= BORDER_RED;
 74  box.us_view &= ~STYLE_MASK;
 75  box.us_view |= B_DOTTED;
 76  printf("\nModified box settings\n");
 77  show_settings1(box.us_view);
 78  printf("bits are %s\n", itobs(box.us_view, bin_str));
 79  return 0;
 80}
 81
 82void show_settings(const struct box_props *pb) {
 83  printf("Box is %s.\n", pb->opaque == true ? "opaque" : "transparent");
 84  printf("The fill color is %s \n", colors[pb->fill_color]);
 85  printf("Border %s.\n", pb->show_border == true ? "shown" : "not shown");
 86  printf("The border color is %s\n", colors[pb->border_color]);
 87  printf("The border style is");
 88  switch (pb->border_style) {
 89  case SOLID:
 90    printf("solid.\n");
 91    break;
 92  case DOTTED:
 93    printf("dotted.\n");
 94    break;
 95  case DASHED:
 96    printf("dashed.\n");
 97    break;
 98  default:
 99    printf("unknown type.\n");
100    break;
101  }
102}
103
104void show_settings1(unsigned short us) {
105  printf("box is %s.\n", (us & OPAQUE) == OPAQUE ? "opaque" : "transparent");
106  printf("The fill color is %s.\n", colors[(us >> 1) & 07]);
107  printf("Border %s.\n", (us & BORDER) == BORDER ? "shown" : "not shown");
108  printf("The border style is");
109  switch (us & STYLE_MASK) {
110  case B_SOLID:
111    printf("solid.\n");
112    break;
113  case B_DOTTED:
114    printf("dotted.\n");
115    break;
116  case B_DASHED:
117    printf("dashed.\n");
118    break;
119  default:
120    printf("unknow type.\n");
121  }
122  printf("The border color is %s.\n", colors[(us >> 9 & 07)]);
123}
124
125
126char *itobs(int n, char *ps) {
127  int i;
128  const static int size = CHAR_BIT * sizeof(int);
129  for (i = size - 1; i >= 0; i--, n >>= 1) {
130    ps[i] = (01 & n) + '0';
131  }
132  ps[size] = '\0';
133  return ps;
134}

END


标题:《C Primer Plus》手记三
作者:fe
地址:https://blog.eiyouhe.com/articles/2019/12/06/1575625068064.html