Let's say move left, move left to shift all bits of a number to the left, and in C with the << operator. For example:
int i = 1;
i = i << 2; Move the value in I to the left 2 bits
In other words, 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 there is 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 digits left shifts exceeds the maximum number of digits of that numeric type, the compiler uses the number of bits left to modulo the maximum number of digits 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 Left shift 1 bit, I becomes 2
j = J << 33; 33% 32 = 1 Left shift 1 bit, J becomes 0, highest bit discarded
When you compile this program with GCC, the compiler gives a warning that says left shift number >= type length. So actually the i,j move is 1 bits, which is the remainder of the 33%32. Under GCC It is the rule that other compilers are not yet clear.
Anyway, left is: Discard the highest bit, 0 the lowest bit
Again right shift, 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 >>
Right shifts the handling of symbol bits and left shifts, for signed integers, such as the int type, the right shift preserves the sign bit, for example:
int i = 0x80000000;
i = i >> 1; The value of I will not become 0x40000000, but will become 0xc0000000
That is, the symbol bit to move to the right, the positive word of 0, minus 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 a word length of 8 bits), you get 11111101.
In summary, in C, the left shift is the logical/arithmetic left shift (the two are identical), the right shift is the arithmetic right shift, and the symbol bit remains unchanged. In practice, you can use left/right shifts to do fast multiply/divide operations, which is much more efficient than cycling.
From http://hi.baidu.com/todaygoodhujun/blog/item/b8c10dd15ae4dfd3572c8417.html
Left and right shift operations in C language