C Primer Plus Chapter 15 (bit operation)

1. Binary, bit and byte

  • Numbers based on 2 are called binary number s
1		  1		 	0		  1
1 * 2^3 + 1 * 2^2 + 0 * 2^1 + 1 * 2^0 = 13

1.1 binary integer

  • C language uses byte s to represent the required size of the character set of the storage system

    • 1 byte = 8 bits
  • Interpret bit pattern s in different ways for 1 byte (8 bits)

    • unsigned char: 0000 0000 ~ 1111 1111 = 0 ~ 255

    • signed char : 1000 0000 ~ 0111 1111 = -128 ~ 127

1.2 signed integer

  • Sign magnitude representation: 1 bit * * high order bit) * * stores symbols, and the remaining 7 bits represent the number itself

    • 1111 1111 ~ 0111 1111 = -127 ~ 127
    • There are two zeros:
      • 0000 0000 = +0
      • 1000 0000 = -0
  • two's-complement method: take the inverse and add 1

    • 1111 1111 ~ 0111 1111 = -128 ~ 127
  • Inverse binary (one's) method

    • 1000 0000 ~ 0111 1111 = -127 ~ 127
    • There are two zeros:
      • 0000 0000 = +0
      • 1111 1111 = -0

1.3 binary floating point number

  • Floating point numbers are stored in two parts
    • Binary decimal
    • Binary index

1.3.1 binary decimal

  • Take the power of 2 as the denominator
1	  0		1
1/2 + 0/4 + 1/8 = 0.625
  • Many fractions cannot be accurately represented by binary decimals
    • It can only accurately represent the sum of multiple 1 / 2 powers

1.3.2 floating point representation

  • Several bits store binary fractions, while others store exponents

2. Other hexadecimal numbers

  • Octal and hexadecimal numeration systems are usually used

2.1 octal

  • Octal refers to the octal numeration system
  • Numbers based on 8
4		  5			1
4 * 8^2 + 5 * 8^1 + 1 * 8^0 = 297
  • Each octal bit corresponds to three binary bits

    • Binary equivalent to octal

      Octal digitEquivalent binary bit
      0000
      1001
      2010
      3011
      4100
      5101
      6110
      7111

2.2 hex

  • Hexadecimal (hex decimal or hex) refers to the hexadecimal numeration system
  • Numbers are represented on the basis of 16
A			3			F
10 * 16^2 + 3 * 16^1 + 15 * 16^0 = 2623
  • Each hexadecimal bit corresponds to 4 binary bits

    • Decimal, hexadecimal, and binary equivalents

      decimal systemhexadecimalEquivalent binary
      000000
      110001
      220010
      330011
      440100
      550101
      660110
      770111
      881000
      991001
      10A1010
      11B1011
      12C1100
      13D1101
      14E1110
      15F1111

3. C bitwise operator

3.1 bitwise logical operators

  • bitwise operation: the operation is carried out for each bit and does not affect the bits on the left and right sides

3.1.1 binary inversion or bit inversion:~

  • Change 1 to 0, change 0 to 1
  • The operator does not change the original value
char c = 0b10011010;
~c;
printf("%d\n%d\n", c, ~c);
// ~(1001 1010) = -102
//   0110 0101  = 101

3.1.2 bitwise AND:&

  • The same is 1
char c1 = 0b10010011;
char c2 = 0b00111101;
printf("%d\n", c1 & c2);
// 1001 0011
// 0011 1101
// &
// 0001 0001

3.1.3 by bit or:|

  • If there is 1, it is 1
char c1 = 0b10010011;
char c2 = 0b00111101;
printf("%d\n", c1 | c2);
// 1001 0011
// 0011 1101
// |
// 1011 1111

3.1.4 bitwise XOR:^

  • The difference is 1
char c1 = 0b10010011;
char c2 = 0b00111101;
printf("%d\n", c1 ^ c2);
// 1001 0011
// 0011 1101
// ^
// 1010 1110

3.7 shift operator

3.7.1 move left:<<

  • Moves the object to the left of the operator to the left and the number of bits specified by the object to the right
  • The operator does not change the original value
char c = 0b10001010;
c << 2;
printf("%d\n%d\n", c, c << 2);
// 00 1000 1010
// 10 0010 1000
//    High level truncation
//    0010 1000

3.7.2 shift right: > >

  • Moves the object to the left of the operator to the right, and the number of bits specified by the object to the right
  • The operator does not change the original value
char c = 0b10001010;
c >> 2;
printf("%d\n%d\n", c, c >> 2);
// 1000 1010
// 1110 0010 10
// Low truncation
// 1110 0010

3.7.3 usage: shift operator

  • Provides fast multiplication and division for powers of 2
    • Number < < n: number is multiplied by 2 to the nth power
    • Number > > n: if number is nonnegative, divide number by the nth power of 2

4. Bit field

  • A bit field is a set of adjacent bits in a signed int or unsigned int type variable
  • Build through a structure declaration
struct {
    int a : 1;
    int b : 2;
    int c : 3;
} abc;
// abc is a field containing three 1 bits

abc.a = 0;
abc.c = 1;
// Since the number of digits in the field is 1, it can only be assigned 0 or 1
  • If the total number of bits declared exceeds the number of bits of int (unsigned int is the same), the storage location of the next int will be used

    • When this happens, an unnamed "hole" is left in the first int
    • Unnamed "holes" can be "filled" with unnamed field widths
    • Use an unnamed field with a width of 0 to store the next field in the next int
    struct {
        int a : 1;
        int   : 2;
        int b : 2;
        int   : 0;
        int c : 3;
    } abc;
    // There is a 2-digit gap between a and b
    // c will be stored in the next int z
    

Keywords: C

Added by amy_ewe on Sun, 13 Feb 2022 18:08:01 +0200