Reference: http://blog.sina.com.cn/s/blog_944790400101bsi8.html
Http://www.feiesoft.com/asm/05-2-05.html
int 8 ; int Main () { = i<<3; Output i =+}
Excuse me: Does the above variable I use logical shift or arithmetic shift? Logical shift, simple understanding is the physical bitwise movement left and right, at both ends with 0 to supplement, do not care about the symbolic problem of numerical value. The arithmetic shift, which is also the physical bitwise movement of the left and right, is supplemented with 0 at both ends, but it must be ensured that the sign bit does not change. Arithmetic shift Instruction
The arithmetic shift instructions are: arithmetic left-shiftalgebraic and arithmetic right-shift SAR (Shiftalgebraic-R). Their instruction format is as follows: Sal/sar Reg/mem, CL/IMM, affected flag bits: CF, OF, PF, SF, and ZF (AF undefined). The function of the arithmetic shift instruction is described as follows:
(1) The arithmetic left-shift Sal puts the target operand's low to high displacement, and the empty low-fill 0;
(2) The arithmetic right-shift SAR puts the high position of the purpose operand to the low displacement, and the vacated high point is filled with the highest bit (sign bit).
Logical SHIFT Instructions
This group of instructions has the following: logical left shiftlogical and logical right-shift SHR (Shiftlogical-R). Their instruction format is as follows: Shl/shr Reg/mem, CL/IMM, affected flag bits: CF, OF, PF, SF, and ZF (AF undefined). The logical left/right SHIFT commands only have a different direction of shift, and the positions vacated after the shift are 0.
(1) Logical left-moving SHL
(2) Logical right Shift SHR
But what we are curious about is whether the "i<<3" and "i>>3" use arithmetic or logical shift? In fact single from the C language itself may not have too many breakthroughs,
Since C will eventually be compiled by the compiler into the assembly code of the target platform, the above code must be parsed in conjunction with the compiler and assembler.
In vc6.0, press F9 to set a breakpoint, then debug run, click Disassembly
You can also, directly debugging the runtime, right at the breakpoint, click Go To Disassembly, display assembly code.
The assembly code can be displayed
The following situations:
(1) When I is unsigned, move the 3-bit to the left, using a logical left shift.
unsigned int i = 8; i = i<<3;//output result i =
(2) When I is a signed shape, move the 3-bit to the left, using the logical left shift.
int i = 8;i = i<<3;//output result i = 64
Conclusion: The logical left shift is used regardless of whether there is an unsigned type, and whether the value is positive or negative.
(3) When I is unsigned, move the 3-bit to the right and use the logical right shift.
int 8 ; int Main () { = i>>3; Output Result i = 1}
(4) When I is a signed shape, move the 3-bit to the right, using the arithmetic right shift.
int 8 ; int Main () { = i>>3; Output i = 1 return0;}
(5) When negative, when put unsigned int i =-8 o'clock (under 32-bit window System), move right three-bit:
(6) When int i=-8, the right move three bit, the result how much? The result is-1
conclusion: as long as the signed number, whether the value is positive or negative, the right shift is the arithmetic right shift.
Questions:In accordance with the principle of shifting 0, why is the left shift a logical shift?
FAQ:First look at the "8" and "8" in the computer Memory values are: 0xfffffff80x8 because the computer is in the complement to save values, so whether the sign or minus, left to the sign bit does not have an impact, and the right shift is different, the number of unsigned numbers right shift does not affect the sign bit, However, when the signed number logic moves right, the high 0 will change the sign bit, so you can only use arithmetic right shift.
Summary:Only the signed number is shifted to the right by arithmetic right, otherwise the logical shift operation (logical left or logical right shift) is used in all other cases. As long as understand that the computer is in a complementary way to preserve the values, everything is clear.
Arithmetic shift and logic shift implementation analysis