当前位置:网站首页>C Primer Plus Chapter 15 (bit operation)

C Primer Plus Chapter 15 (bit operation)

2022-07-05 06:15:00 His Last Bow

1. Binary system 、 Bits and bytes

  • With 2 The number represented by the base is called Binary number (binary number)
1		  1		 	0		  1
1 * 2^3 + 1 * 2^2 + 0 * 2^1 + 1 * 2^0 = 13

1.1 Binary integer

  • C Language use byte (byte) Represents the required size of the storage system character set

    • 1 byte = 8 position
  • Yes 1 byte (8 position ) In different ways Bit combination (bit pattern)

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

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

1.2 Signed integers

  • Sign quantity (sign-magnitude) notation :1 position ** Higher order potential (high-order bit)** Store symbols , be left over 7 Bits represent the number itself

    • 1111 1111 ~ 0111 1111 = -127 ~ 127
    • There are two 0 :
      • 0000 0000 = +0
      • 1000 0000 = -0
  • Binary complement (two’s-complement) Method : Take the opposite , Add 1

    • 1111 1111 ~ 0111 1111 = -128 ~ 127
  • Binary inverse (one’s-complement) Method : Take the opposite

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

1.3 Binary floating point numbers

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

1.3.1 Binary decimals

  • With 2 Power of as 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 multiple 1/2 The sum of the powers of

1.3.2 floating-point number

  • Several bits store binary fractions , Other bits store index

2. Other hexadecimal numbers

  • Octal and hexadecimal numeration systems are usually used

2.1 octal

  • octal (octal) It refers to octal numeration system
  • With 8 Represent numbers for the base
4		  5			1
4 * 8^2 + 5 * 8^1 + 1 * 8^0 = 297
  • Each octal digit corresponds to 3 Binary bits

    • Binary equivalent to octal

      Octal digit Equivalent binary
      0000
      1001
      2010
      3011
      4100
      5101
      6110
      7111

2.2 Hexadecimal

  • Hexadecimal (hexadecimal or hex) It refers to the hexadecimal numeration system
  • With 16 Represent numbers for the base
A			3			F
10 * 16^2 + 3 * 16^1 + 15 * 16^0 = 2623
  • Each hexadecimal digit corresponds to 4 Binary bits

    • Decimal system 、 Hexadecimal and equivalent binary digits

      Decimal system Hexadecimal Equivalent binary
      000000
      110001
      220010
      330011
      440100
      550101
      660110
      770111
      881000
      991001
      10A1010
      11B1011
      12C1100
      13D1101
      14E1110
      15F1111

3. C Bitwise operators

3.1 Bitwise logical operators

  • Bitwise (bitwise) operation : Operations are carried out for each bit , It doesn't affect the left and right bits

3.1.1 Binary inversion or bitwise inversion :~

  • hold 1 Turn into 0 , hold 0 Turn into 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 :&

  • A fellow 1 Only then 1
char c1 = 0b10010011;
char c2 = 0b00111101;
printf("%d\n", c1 & c2);
// 1001 0011
// 0011 1101
// &
// 0001 0001

3.1.3 Press bit or :|

  • Yes 1 Then for 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 Operators

3.7.1 Move left :<<

  • Move the object to the left of the operator to the left , The number of bits specified by the right object
  • 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 truncation 
// 0010 1000

3.7.2 Move right :>>

  • Move the object to the left of the operator to the right , The number of bits specified by the right object
  • 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 Operators

  • in the light of 2 The power of provides fast multiplication and division
    • number << n :number multiply 2 Of n The next power
    • number >> n : If number non-negative , Then use number Divide 2 Of n The next power

4. Bit fields

  • Bit fields (bit field) It's a signed int or unsigned int A set of adjacent bits in a type variable
  • Build through a structure declaration
struct {
    
    int a : 1;
    int b : 2;
    int c : 3;
} abc;
// abc  Include for  3  individual  1  Bit field 

abc.a = 0;
abc.c = 1;
//  Because the number of digits in the field is  1 , So it can only be assigned  0  or  1
  • If the total number of digits declared exceeds int (unsigned int Empathy ) Number of digits , Will use the next one int Storage location

    • When this happens , first int Will leave an unnamed “ hole ”
    • You can use unnamed field widths “ fill ” Unnamed “ hole ”
    • Use a width of 0 The unnamed field of causes the next field to be stored in the next int in
    struct {
          
        int a : 1;
        int   : 2;
        int b : 2;
        int   : 0;
        int c : 3;
    } abc;
    // a  and  b  There is a  2  Bit gap 
    // c  Will be stored in the next  int z
    
原网站

版权声明
本文为[His Last Bow]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140619046324.html