Our common shift operations are << and >>, which are left-and right -shifted respectively.
There are two kinds of arithmetic shift and logical shift in C language.
The difference between >> and >>> in JS
--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- -------------
Arithmetic shift: It's the shift of the symbol. {C language is defined directly as char m = 3}
Logical shift: That is, the shift of the non-distinguishable symbol {unsigned char m = 3} in the C language
--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- -------------
Okay, let's see what the difference is between the arithmetic shift and the logical shift:
For example:
(1) For unsigned number 3来 said, the x<<1 to the left one, the leftmost displacement off, the rightmost move in the bit to fill 0. Become
00000110, so the result is 6;x>>1 to the right one bit, because it is unsigned number, so the logical move right, the rightmost one to move away,
The leftmost shift comes in with a bit of 0, which becomes 00000001, so the result is 1.
(2) for the signed number 3来 said, the x<<1 to the left one, the leftmost displacement off, the rightmost move in the bit to fill 0. Become
00000110, so the result is 6;x>>1 to the right one bit, because it is a signed number, may occur logical right SHIFT, may also occur arithmetic right shift
, this, the C standard does not specifically specify whether to use the logical right SHIFT or arithmetic right shift. But most machines use arithmetic to move right, and become
00000001, so the result is still 1. But please note that this is just to say that most machines are like this and you dare to guarantee yourself
No special circumstances.
(3) for the signed number-3来 said, the x<<1 to the left one, the leftmost displacement off, the rightmost move in the bit complement 0. Become
11111010, the result is-6. Move to the right one bit, because it is a signed number, the logical right shift may occur, or arithmetic right shifts may occur.
Most machines use arithmetic to move right, turning to 11111110, and the result is-2.
Summary: Always shift and fill 0 when left. The unsigned number in the right shift is shift and complement 0, which is called the logical right shift;
In most cases, the number of symbols is the shift and complement the leftmost bit (that is, the maximum number of digits), a few to make up, now called arithmetic right shift .
--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- -------------
Add
1. The logical right shift in assembly language (SHR) is to move you to the right of the specified number of digits, and then to the left of the 0, arithmetic right shift (SAR) is to move you to the right of the specified number of digits, and then on the left with the original symbol bit to fill. &NBSP
2. The advanced language Right Shift operator (>>) is to move the binary of a number to the right several bits, the low move out of the partial discard, and the left complement 0. &NBSP
3. The advanced language is the same as the logical right SHIFT function in assembly language, but differs from the arithmetic right shift