here are left and right movements.
Let's talk about moving left first. Moving left means moving all the digits of a number to the left by several digits, and using <; < Operator. For example:
int i = 1;
i = i < < 2; //shift the value in I to the left by 2 digits
, that is, the binary of 1 is ...1 (where the number of zeros in front of 1 is related to the number of bits in int, and there are 31 zeros in gcc for 32-bit machines), and after shifting 2 digits to the left, it becomes ...1, which is 4 in decimal, so moving 1 digit to the left is equivalent to multiplying 2. Because shift to the left may lead to sign change, the reason is explained below.)
One problem that needs attention is the leftmost sign bit of int type and the shift out. As we know, int is a signed integer, and the leftmost bit is a sign bit, that is, is positive and 1 is negative, so there will be overflow when shifting, for example:
int i = x4; //4 in hexadecimal, 1 in binary ...
I = I <; < 1;
then, I will become x8 after shifting one bit to the left, that is, 1... in binary, the sign bit will be set to 1, and all other bits will be , which becomes the minimum value that can be represented by the int type. The value of 32-bit int is -2147483648, which overflows. What will happen if I is shifted one bit to the left? In C language, the processing method of discarding the most significant bit is adopted. After discarding 1, the value of I becomes .
A special case in left shift is that when the number of digits shifted to the left exceeds the maximum number of digits of the numerical type, the compiler will use the number of digits shifted to the left to remove the maximum number of digits of the modular type, and then shift it according to the remainder, such as:
int i = 1, j = x8; //let int be 32 bits
I = I <; < 33; // 33% 32 = 1 shifts one place to the left, and I becomes 2
j = j <; < 33; // 33% 32 = 1 shifts one bit to the left, j becomes , and the highest bit is discarded
When compiling this program with gcc, the compiler will give a warning, saying that the number of bits is shifted to the left >; = type length. So in fact, i,j moves one bit, that is, the remainder after 33%32. This rule is under gcc, and it is not clear whether other compilers are the same.
In short, shifting to the left means discarding the highest bit, adding to the lowest bit
and then moving to the right. If you understand the reason of shifting to the left, then moving to the right is easier to understand.
Moving to the right. > .
shift right handles the sign bit differently from shift left. For signed integers, such as int type, shift right will keep the sign bit unchanged, for example:
int i = x8;
i = i > > 1; The value of //i will not become x4, but xc
. That is to say, after the sign bit moves to the right, the positive number will be supplemented by , and the negative number will be supplemented by 1, that is, the arithmetic in assembly language will move to the right. Similarly, when the number of digits moved exceeds the length of the type, the remainder will be taken, and then the remainder bits will be moved.
The negative number is 1111 > > 5 (assuming the word length is 8 bits), the result is 1111111
In short, in C, the left shift is a logical/arithmetic left shift (both are exactly the same), and the right shift is an arithmetic right shift, which will keep the sign bit unchanged. In practical application, we can use the left/right shift to do fast multiplication/Divison according to the situation, which will be much more efficient than the loop.