LA

Logical Binary Shifts

Logical Binary Shifts

A binary shift moves all of the bits in a given binary number either to the left or the right by a given number of places.  All of the empty spaces are then filled with zeros.

A shift of one place to the left will have the following effect:

You can move “bits” either to the right or the left

  • If you move to the left then the effects can be:

    • Any over flow to the left is ignored

    • Any gaps on the right are infilled by zero

  • If you move to the right then the effects can be:

    • Any overflow to the right is ignored

    • Any gaps on the left are infilled by zero

In coding the bit shift operators are: << to move to the left and >> to move to the right.

 

Each of these will be followed by a number so that >>1 means to move one place to the right and << 2 means move to places to the left.

 

Complete the questions below - all answers in binary:

  1. 0010 >> 1 = 0001

  2. 0101  << 1 = 1010

  3. 0101 0101>>1 = 0010 1010

  4. 1101 0000 << 1 = 1010 0000

  5. 0010 0101 << 2 = 1001 0100

  6. 1111 0001 >> 2 = 0011 1100

  7. 0101 0001 << 3 = 1000 1000

  8. 1110 0111 >> 2 = 0011 1011


Effects of logical shifts - multiplication and division by powers of 2 

If a binary number is shifted to the left this is equivalent to multiplying the number by 2 for each shift to the left. Two shifts would therefore multiply a number by 4.  

E.g. 

0

0

0

0

1

1

1

1


Two places to the left we get the binary number:

0

0

1

1

1

1

0

0


If a binary number is shifted to the right, this is equivalent to dividing the number by 2 for each shift to the right. 

0

1

1

1

0

0

0

0


Three places to the right we get the binary number:

0

0

0

0

1

1

1

0


Effects of logical shifts -  using odd numbers

When moving the decimal number 17 one place left, it becomes 170 and has therefore been multiplied by its base of 10.

An issue with precision occurs where odd numbers are divided since a byte cannot represent fractional numbers.  Consider the following shift of three places to the right.

1

0

1

0

0

1

0

1



0

0

0

1

0

1

0

0


The original binary value was equal to decimal 165.  A right shift should divide this by 8 (or divide 2, three times).  165/8 = 20.625.  However, the resulting binary converted to decimal is 20.



Questions

  1. Complete a 2-place shift to the right on the binary number 11010110        [1]

00110101

  1. Explain the effect of performing a right shift or two places on the binary number 11010110                                                                                        [2]

The number gets divided by 4  the original number was 214 / 4 = 53

  1. Explain the effect of performing a left shift of 1 place on the binary number 11010110                                                                                                     [2]

10101100 timesing by 2 the number is 214 x 2 = 428

  1. Convert the binary number 1100 1011 into denary.                                     [1]

203

  1. Complete a 2-place shift to the right on the binary number 11001011        [1]

0011 0010

  1. Explain the effect of performing a 2-place shift to the right on the binary number 11001011.                                                                                       [2]

0011 0010 

Divided by 4 the original number is 203 / 4 = 50.75 however we lose precision 0000 1100 = 50 instead of 50.75