LOGBOOK

HELP

Quiz Entry - updated: 2026.07.14

What's the difference between SAR (arithmetic right shift) and SHR (logical right shift)?

SAR fills the vacated upper bits with copies of the sign bit (preserving sign), while SHR fills them with zeros — so SAR is for signed values and SHR for unsigned.

Right-shifting -8 with sar (sign-fill) versus shr (zero-fill).

* Right-shift differs in the vacated top bit: sar copies the sign bit (correct for signed), shr shifts in a 0 (correct for unsigned). *

Right-shifting by one is a fast divide-by-two, but "what fills the top?" depends on whether the number is signed. A negative number must stay negative, so SAR copies the sign bit; an unsigned number's top bits are genuinely zero, so SHR fills with zeros.

Watch what happens to the 8-bit value −8 (11111000):

Original:  11111000  (-8)
sar $1:    11111100  (-4)   ← sign bit preserved, correct for signed
shr $1:    01111100  (124)  ← zero-filled, WRONG if it was meant to be signed
Shift Fills with Use for
sar sign bit signed division by powers of 2
shr 0 unsigned division, bit manipulation

Reverse-engineering tip: seeing sar tells you the compiler believed the value was signed; shr implies unsigned. That single instruction choice can reveal the original C type.

Go deeper:

From Quiz: REVE1 / The Processor Interface | Updated: Jul 14, 2026