Menu

Binary Shifts

Binary Shifts

A binary shift moves the bits in a binary pattern a set number of places to the left or right. The pattern keeps its fixed width, so any bits that move past either end are discarded rather than reappearing at the other side.

For example, shifting the 8-bit pattern 0011 1010 left by two places discards the two leftmost bits (00). Zeros fill the two right-hand vacancies, giving 1110 1000. The original value 58 has been multiplied by 4 to give 232.

A left shift by two places on 0011 1010 (58): the two leftmost 0s are dropped, two 0s come in on the right, giving 1110 1000 (232).

Vacant positions are filled according to the type of shift. A logical shift fills every vacant position with a 0. An arithmetic right shift fills the left-hand vacancies with a copy of the sign bit (the most significant bit), so the sign of a signed integer is kept. A left shift, whether logical or arithmetic, fills the right-hand vacancies with zeros.

Shifts are a quick way to multiply or divide by powers of two. Shifting left multiplies: one place doubles the value, two places multiplies by four, three places by eight, and so on. Shifting right divides: one place halves, two places divides by four, three places by eight. The multiplier or divisor is 2 raised to the number of places — two places means 2² = 4, not 2.

8-bit patternShiftResultEffect on value
0000 1100 (12)Left 20011 0000 (48)×4
0000 1100 (12)Right 10000 0110 (6)÷2

Practise this