"C Language" Original Complement Complement and Bit Operation
Although various literatures can be found, summing up your own system can deepen your understanding of this knowledge.
This article is based on Baidu Encyclopedia's introduction to source code, complement, complement and bit operations, and draws on Zhang Ziqiu and Liquor related articles.
table of Contents:
First, the number of machines and truth values
Second, the basic concepts of source code, complement and complement
Third, why use the original code, complement and complement?
Fourth, the original code, complement and reverse
Five, data overflow test
Sixth, the operation of bit operations
Seven, simple application of bit operation
First, the number of machines and truth values
Computer number is the binary representation of a number in a computer
The number of machines has two characteristics: one is the digitization of symbols, and the other is that the size of the number is limited by the word length of the machine
For example: +6 in decimal, computer word length is 8 bits, converted to binary is 00000110, if it is -6, it is 10000110
Here 00000110 and 10000110 are the number of machines
Because the first bit is the sign bit (positive numbers are 0, negative numbers are 1, 0 min +0 and -0), so:
① The range of 8-bit binary numbers is: [1111 1111, 0111 1111]
② The formal value of the machine number is not equal to the real value.
For the sake of distinction, the true value corresponding to the number of machines with a signed bit is called the true value of the machine number.
For example: 0000 0001 truth value = +000 0001 = +1, 1000 0001 truth value = –000 0001 = –1
Second, the basic concepts of source code, complement and complement
For a number, the computer uses a certain encoding to store it. Original code, reverse code, complement code is the encoding method used by the machine to store a specific number
[+1] = [00000001] original = [00000001] reverse = [00000001] complement
[-1] = [10000001] original = [11111110] reverse = [11111111] complement
Original code
The original code is the absolute value of the sign bit plus the true value, that is, the first bit represents the sign, and the remaining bits represent the value.
2. Inverse code
The inverse of a positive number is itself
The negative code of the negative number is based on its original code, the sign bit is unchanged, and the remaining bits are inverted.
3. Complement
The complement of a positive number is itself
The complement of a negative number is +1 on the basis of the complement
It can be seen that the original two's complement of the positive number is itself, and the complement of the negative number cannot be seen intuitively, and it needs to be converted to the original code to calculate its value.
Third, why use the original code, complement and complement?
For computers, adding and subtracting multipliers is already the most basic operation. It must be designed as simple as possible, and letting the computer recognize the "sign bit" obviously makes the computer's basic circuit design very complicated. So people began to explore the method of pseudo-participating in symbols, and only retaining the addition
If you use the original code to calculate the decimal subtraction: 1-1 = 0, the result is incorrect:
1-1 = 1 + (-1) = [00000001] original + [10000001] original = [10000010] original = -2
When using the complement code, the true part of the result is correct:
1-1 = 1 + (-1) = [0000 0001] original + [1000 0001] original = [0000 0001] inverse + [1111 1110] inverse = [1111 1111] inverse = [1000 0000] original = -0
The problem of the inverse code is 0. In the inverse code, there will be [0000 0000] original = +0 and [1000 0000] original =-0. The two codes represent 0, so a complement [1000 0000] to solve this problem appears ] Complement (in 8-bit binary machine numbers, the complement can also represent a minimum of -128 = [1000 0000] complement):
1-1 = 1 + (-1) = [0000 0001] original + [1000 0001] original = [0000 0001] supplement + [1111 1111] supplement = [0000 0000] supplement = [0000 0000] original
Fourth, the original code, complement and reverse
I recommend Zhang Ziqiu's blog about the original two's complement code. It also talks about the concept of congruence and negative modulo, so I won't copy it myself. .
x mod y is equal to x minus y times the lower bound of the quotient of x and y
The inverse of a number is actually the congruence of this number with a film, and this film is not our binary, but the maximum value that can be represented
Due to the special case of 0, there is no way to represent 128, so the range of the two's complement value is [-128, 127]
Five, data overflow test
Sixth, the operation of bit operations
1. & bitwise with "AND"
Function: When the corresponding two binary digits are 1, the result is 1, otherwise it is 0.
Example: 9 & 5 = 1001 & 0101 = 0001, that is, 9 & 5 = 1
* Law: and 1 & in binary remain in place, and 0 & is 0
2. | Bitwise or "OR"
Function: The corresponding two binary bits are only 1 if the result is 1, otherwise 0
Example: 9 | 5 = 1001 | 0101 = 1101, which is 9 | 5 = 13
3. ^ Bitwise XOR, `` XOR, EOR ''
Function: The corresponding two binary digits are different. It is 1, otherwise it is 0.
Example: 9 ^ 5 = 1001 ^ 0101 = 1100, that is, 9 ^ 5 = 12
*law:
XOR of the same integer is 0, eg 5 ^ 5 = 0
Different integers are different or the result has nothing to do with the order, for example: 5 ^ 6 ^ 7 = 5 ^ 7 ^ 6
XOR of any number and 0 does not change, for example: x ^ 0 = x
To sum up, x ^ y ^ x = x ^ x ^ y = 0 ^ y = y
4. ~ Invert `` NOR '' bit by bit
Function: Invert each bit of the integer, and the sign is also inverted. "Invert: 0 is inverted to 1, 1 is inverted to 0."
Example: ~ 9 = -10 (because negative numbers are stored in two's complement)
~ 9 = ~ [00001001] Original = [11110110] Complement = [11110101] Inverse = [10001010] Original = -10
5. << shift left (shl)
Format: integer << shift left
Example: x << n
In essence: x * 2n
Operation: shift the binary bits of x to the left by n units, discard the high-order bits, and add 0 to the low-order bits
6. >> shift right (shr)
Format: Integer >> Shift right
Example: x >> n
In essence: x / 2n
Operation: shift the binary bits of x to the right by n units, discard the lower bits, and keep the sign bit unchanged
Note: The sign bit also moves. Right shift does not change the sign of the integer. Finally, the sign bit must be adjusted to the original value.
Positive number The sign bit is 0, and the most significant bit is 0.
Negative sign is 1, most significant bit is complemented by 1
Seven, simple application of bit operation
1. (& bitwise and "AND") parity judgment
Modulo judgment: a% 2? Printf ("odd \ n"): printf ("even \ n");
And one judgment: a & 1? Printf ("odd \ n"): printf ("even \ n");
2. (^ bitwise XOR, EOR) value conversion
With third-party variables: temp = a; a = b; b = temp;
Without extra space, mathematical method: a = b-a; b = b-a; a = b + a;
Without extra space, bitwise operations: a = a ^ b; b = a ^ b; a = a ^ b;
3. (<< left shift and >> right shift) optimize multiplication and division efficiency
the value of a shl b is equal to a multiplied by 2 to the power of b
a shr b such as binary search, heap insertion, etc.