Menu

Binary Shifts

Binary Shifts

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.

Practise this