Shift Operator: The most important point, although the digital storage in the computer memory is in the 2-based complement form, but the use of the shift operator is the source code.
← Shift Operator: High Discard, low 0 (negative numbers are the same as positive)
Example: int a=-10,g. g=a<<4=-160 a:11010 (2) →110100000=-160 (10)
→ shift operator: The sign bit with the number of operations, when the highest position when positive is 0, negative when the symbol bit 1, the highest bit of the complement depends on the compiler, more than 1. int i = 1; i = i << 2; Move the value of I to the left 2 bits that is, 1 of the 2 binary is 000 ... 0001 (here 1 the number of front 0 is related to the number of int, 32-bit machine, GCC has 31 0), left 2-bit to 000 ... 0100, that is, 10 of the 4, so that the left shift 1 is equal to 2, then the left n is multiplied by the N-Square of 2 (the number of symbols is not fully applicable, because the left shift may lead to symbol changes, explained below reason)
One of the issues to be aware of is the position of the leftmost symbol and the shift out of the int type. We know that int is a signed shape number, the leftmost 1 bit is the sign bit, that is 0 positive 1 negative, then the shift occurs when the overflow, for example: int i = 0x40000000; 16 binary 40000000, 2 binary 01000000 ... 0000 i = i << 1; So, I move 1 bits to the left and then it becomes 0x80000000, which is 2 binary 100000 ... 0000, the sign bit is set 1, the other bits are all 0, it becomes the minimum value that int type can represent, and the value of 32-bit int is-2147483648, overflow. What happens if I move the I to the left of the 1 bit again? In C language, the processing method of discarding the highest bit is used, and after 1 is discarded, The value of I becomes 0.
A special case in the left shift is that when the number of left-shifted digits exceeds the maximum number of digits of that numeric type, the compiler uses the number of bits left to go to modulo the maximum number of bits of the type, and then shifts by the remainder, such as: int i = 1, j = 0x80000000; Set int to 32 bits i = i << 33; 33% 32 = 1 shift left 1 bits, I becomes 2 j = J << 33; 33% 32 = 1 shift left 1 bits, J becomes 0, highest bit is discarded when compiling this program with GCC the compiler gives a warning that says left shift number >= type length. So actually the i,j is moving 1 bits, which is the remainder of the 33%32. Under GCC is the rule, It's not clear that the other compilers are all the same. In short, the left shift is: Discard the highest bit, 0 of the minimum bit to move to the right, understand the truth of the left shift, then the right shift is better understood. The concept of right shift is opposite to the left shift, which is to move several bits to the right, the operator is >> The right shift is different from the processing of the symbol bit and the left shift, for signed integers, such as the int type, the right shift keeps the sign bit constant, for example: int i = 0x80000000; i = i >> 1; The value of I will not become 0x40000000, but will become 0xc0000000 that is, the sign bit to the right, after the positive word of 0, negative complement 1, that is, the arithmetic right shift in assembly language. Also, when the number of bits moved exceeds the length of the type, the remainder is taken and the remaining digits are moved. Negative 10100110 >>5 (assuming the word length is 8 bits), the resulting is 11111101 in short, in C, the left is the logical/arithmetic left shift (the two are identical), the right shift is the arithmetic right shift, will maintain the symbol bit unchanged. In practice, you can use left/right shifts to do fast multiply/divide operations as appropriate. , which is much higher than the cycle efficiency. In many system programs, it is often required to perform operations or processing at the bit level. The C language provides the function of bit arithmetic, which makes C language can also be used to write system program like assembly language.
(Refer to part of the content).