Menu

Memory & Storage

Binary Shifts

Contents

  1. Binary Shifts
  2. Shift Effects on Value
  3. Shift Overflow and Precision

A binary shift takes each bit in an 8-bit binary value and moves it a given number of positions, either leftwards or rightwards. The value stays 8 bits wide, so any bit pushed past either end is discarded.

When the bits move, gaps appear at one end. Every gap is filled with a 0 — never with a 1, and never with a bit that fell off the other end.

Left shift

To carry out a left shift of n places:

  1. Move every bit n places to the left.
  2. Throw away any bits that pass the left-hand end.
  3. Fill the n gaps on the right with 0s.

For example, left shift 00010110 by 3 places. Each bit moves three columns to the left, the three bits at the left-hand end (000) pass out of the 8-bit value, and three 0s drop into the right-hand end. The result is 10110000.

Right shift

To carry out a right shift of n places:

  1. Move every bit n places to the right.
  2. Throw away any bits that pass the right-hand end.
  3. Fill the n gaps on the left with 0s.

For example, right shift 01101000 by 3 places. The three bits at the right-hand end (000) drop out, the remaining bits 01101 slide three columns to the right to become 00001101, and the three new left-hand positions are filled with 0s.

0011 0100 (52) shifted logically left one place gives 0110 1000 (104) and right one place gives 0001 1010 (26), with 0s filling the gaps and the dropped bit marked.

Describing a shift

A shift is only fully described when you give both the direction and the number of places. A "shift of three" could mean three places left or three places right, and those two answers are completely different, so the direction matters just as much as the count.

Binary shifts in this topic are carried out on positive whole numbers only. Every gap is filled with a 0, so you never treat the leftmost bit any differently from the rest.

Shifting every bit in a binary number sideways changes the number's value in a very predictable way: each place moved to the left doubles the value, and each place moved to the right halves it. The multiplier or divisor is a power of 2, set by the number of places you move.

Places movedLeft shiftRight shift
1×2÷2
2×4÷4
3×8÷8

Why a shift changes the value

In an 8-bit number the columns are worth 128, 64, 32, 16, 8, 4, 2 and 1, so every column is worth twice the column immediately to its right. Move all the bits one column left and each bit now sits in a column worth double, which doubles the whole number. Move them one column right and each bit drops into a column worth half as much, so the number halves.

00001101 (13) shifted left one place gives 00011010 (26); shifted right one place gives 00000110 (6), with the dropped 1 marked. Place values 128 to 1 sit under each row.

Worked example: multiplying by 8

Take 00010101, which is 16 + 4 + 1 = 21. To multiply by 8, shift it left 3 places. The bits land in 10101000, which is 128 + 32 + 8 = 168, and 21 × 8 = 168, so the shift has done the multiplication. Going the other way, 01011000 is 88; shift it right 3 places and you get 00001011, which is 11, and 88 ÷ 8 = 11.

This is why a binary shift is a quick way to multiply or divide by a power of two: one shift of the right number of places does the whole calculation, instead of working it out the long way.

A binary shift keeps the result to 8 bits, so any bit pushed past either end is dropped. Whether those bits are 0s or 1s decides whether the remaining 8 bits hold the true value.

Left shifts and overflow

A left shift is correct only when every bit lost off the left-hand end is a 0, because a 0 adds no value. If a 1 is lost, the true result is too big to store in 8 bits: this is overflow, and the 8 bits left behind are not the real value. That is not a computer fault, just a result too big for 8 bits.

Shifting 01101010 (106) two places left drops a 0 and a 1, leaving 10101000 (168). The true answer is 424 (106 × 4), too big for 8 bits, so 168 is wrong. By contrast, 00110110 (54) shifted one place left drops a 0 and gives 01101100 (108), exactly 54 × 2.

Right shifts and precision

A right shift is exact only when every bit lost off the right-hand end is a 0. If a 1 falls off the right, part of the value goes with it, so the result is rounded down and precision is lost. Shifting 10100000 (160) four places right drops only 0s and gives 00001010 (10), exactly 160 ÷ 16. Shifting 10110111 (183) two places right drops 11 and gives 00101101 (45); the exact answer, 45.75, has been rounded down.

Showing the effect in denary

Convert the original and the shifted pattern to denary and compare them: if the new value is not the old one multiplied or divided by 2, 4 or 8, then 1s were lost.

StartShiftResultBits lostEffect
00110110 (54)1 left01101100 (108)0Correct, ×2
01101010 (106)2 left10101000 (168)0 and 1Overflow (true 424)
10100000 (160)4 right00001010 (10)0000Exact, ÷16
10110111 (183)2 right00101101 (45)11Rounded down

The first row loses nothing; the second is out by 256, and the last by less than 1.