Menu

Binary Shifts

Arithmetic Shifts

An arithmetic shift moves a binary pattern left or right. It is the shift you use for signed integers, such as two's complement numbers, because a right shift keeps the sign of the number the same. The bit that carries the sign is the leftmost bit, the most significant bit (MSB).

Shifting right

An arithmetic right shift moves every bit one place to the right and copies the sign bit into each vacant position on the left. Because the sign bit is copied rather than replaced, the sign is preserved: a negative number stays negative and a positive number stays positive.

Each place shifted right halves the value, so n places divide it by 2^n, discarding any remainder. For example, take −46 as the 8-bit pattern 1101 0010. Shifting right one place copies the 1 from the MSB into the gap, giving 1110 1001, which is −23 — exactly half of −46, still negative.

Shifting left

An arithmetic left shift moves every bit to the left and fills the vacant positions on the right with zeros. This makes it identical to a logical left shift, because a logical left shift also fills with zeros; there is nothing to preserve at the right-hand end.

Each place shifted left doubles the value, so n places multiply it by 2^n. For instance, 1101 0010 (−46) shifted left one place becomes 1010 0100, which is −92.

Why the fill bit matters

Right shiftBits filling the leftSign after shiftingCorrect for a signed integer?
Logical0Set to 0No
ArithmeticA copy of the sign bitKeptYes

A logical right shift on a negative two's complement number forces the sign bit to 0, so the answer is wrong for a signed integer: 1101 0010 (−46) shifted logically right one place gives 0110 1001, which is 105. The only difference between the two right shifts is that single fill bit.

1101 0010 (-46) shifted right: an arithmetic shift copies the sign bit into the gap to give 1110 1001 (-23), while a logical shift puts in a 0 and gives 0110 1001 (105).

Practise this