views:

53

answers:

3

what is the difference between logical right shift and arithmetic right shift?

+3  A: 

Arithmatic right shift duplicates the sign-bit. Logical shift ignores the sign bit and the left-most bit becomes 0.

Example

8-bit right-shift to keep things simple.

Example 1

11100101 Original bits
11110010 Arithmetic right-shift
01110010 Logical right-shift

Example 2

00011101 Original bits
00001110 Arithmetic right-shift
00001110 Logical right-shift

Note that both operations are the same when the sign bit is 0.

Andrew Cooper
can you explain through some example
Shweta
@Shweta: Examples added. Please accept the answer if you like it.
Andrew Cooper
A: 

One (arithmetic shift) preserves sign (by filling in the vacated left-most bit position with a duplicate of what was previously there); the other does not (logical shift) and instead always fills in with zeroes.

Amber
+2  A: 

LSR shifts the bits to the right and fills the MSb with 0. ASR shifts the bits to the right and fills the MSb with whatever was there before the shift, which preserves the "divide by 2" mechanism for signed numbers.

Ignacio Vazquez-Abrams