The concept of moving right is opposite to moving left, that is, moving a few digits to the right, and the operator is > >;
Shift right and shift left handle sign bits in different ways. For signed integers, such as int type, shifting to the right will keep the sign bit unchanged, for example:
inti = 0x80000000
i = i & gt& gt 1; The value of //i will not become 0x40000000, but 0xc00000000.
2. Move to the left, multiply by 2, and move to the left by n bits multiplied by the n power of 2;
Let's talk about moving left first. Shifting to the left means shifting all the bits of a number to the left by several bits and using the
i = i & lt& lt2; //Shift the value in I by 2 bits to the left.
Moving to the left means that the highest bit is discarded and the lowest bit is filled with 0.
Extended data
After the sign bit is shifted to the right, the positive number complements 0 and the negative number complements 1, which is the arithmetic right shift in assembly language. Similarly, when the number of digits moved exceeds the length of the type, the remainder will be taken and the remaining digits will be moved.
Negative number10100110 >; 5 (assuming the word length is 8 bits), you get11111.
In C, left shift is logical/arithmetic left shift (both are the same), and right shift is arithmetic right shift, which will keep the sign bit unchanged. In practical application, we can use left/right shift to do fast multiplication/Divison according to the situation, which is much more efficient than loop.