Binary and data representation - EdexcelBinary shifts and negative numbers

All data in a computer is represented in binary, whether it is numbers, text, images or sound. The computer software processes the data according to its content.

Part ofComputer SciencePrinciples of computer science

Binary shifts and negative numbers

There are two different types of binary shift which work differently for negative numbers. The two types are:

  • arithmetic
  • logical

Arithmetic shift left

To multiply a negative number, an arithmetic binary shift moves all the digits in the binary number along to the left and fills the gaps after the shift with 0:

  • to multiply by two, all digits shift one place to the left
  • to multiply by four, all digits shift two places to the left
  • to multiply by eight, all digits shift three places to the left
  • and so on

Example: 00011100 (denary -128 + 64 + 16 + 8 = -40) × 2

1286432168421
11011000
1281
641
320
161
81
40
20
10

Result: shifting one place to the left gives 00111000 (-128 + 32 + 16 = denary -80)

1286432168421
10110000
1281
640
321
161
80
40
20
10

Arithmetic shift right

To divide a negative number, an arithmetic binary shift moves all the digits in the binary number along to the right and fills the gaps after the shift with the previous MSB value:

  • to divide by two, all digits shift one place to the right
  • to divide by four, all digits shift two places to the right
  • to divide by eight, all digits shift three places to the right
  • and so on

Example: 10001100 (denary -116) ÷ 2

-1286432168421
10001100
-1281
640
320
160
81
41
20
10
-1286432168421
11000110
-1281
641
320
160
80
41
21
10

Result: (1 × -128) + (1 × 64) + (1 × 4) + (1 × 2) = denary -58

Two’s complement is therefore better for arithmetic shifts left since the sign is always retained.

Logical shifts

In logical shifts - left or right - a 0 is always copied in. This means that a logical shift is ideal for unsigned binary numbers.

Example: 10011000 (denary 152) ÷ 2

1286432168421
10011000
1281
640
320
161
81
40
20
10
1286432168421
01001100
1280
641
320
160
81
41
20
10

Result: (1 × 64) + (1 × 8) + (1 × 4) = denary 76