Fundamentals of data representation - AQABinary addition and binary shift

All data is represented as binary digits, whether it is numbers, text, images or sound. Calculations are also made in binary.

Part ofComputer ScienceComputational thinking and problem solving

Binary addition and binary shift

When two numbers are added together in , we take the first number, add the second number to it, and get an answer. For example, 1 + 2 = 3.

When we add two numbers together the process is different.

There are four rules that need to be followed when adding two binary numbers. These are:

  • 0 + 0 = 0
  • 1 + 0 = 1
  • 1 + 1 = 10 (binary for decimal 2)
  • 1 + 1 + 1 = 11 (binary for decimal 3)

Example - adding 01 + 10

1 + 0 = 1

0 + 1 = 1

Binary addition: 0 1 plus 1 0 equals 1 1

Result in binary - 11 (which is decimal 3)

Example - adding 01 + 101

1 + 1 = 0, carry 1

1 + 0 + 0 = 1

0 + 1 = 1

Binary addition: 0 1 plus 1 0 1 equals 1 1 0

Result in binary - 110 (which is decimal 6)

Example - adding 01010011 + 01110110

1 + 0 = 1

1 + 1 = 0, carry 1

1 + 0 + 1 = 0, carry 1

1 + 0 + 0 = 1

1 + 1 = 0, carry 1

1 + 0 + 1 = 0, carry 1

1 + 1 + 1 = 1, carry 1

1 + 0 + 0 = 1

Binary addition: 0 1 0 1 0 0 1 1 plus 0 1 1 1 0 1 1 0 equals 1 1 0 0 1 0 0 1

Result in binary - 11001001 (which is decimal 201). You can check your answers by converting each binary number into decimal and checking your addition. In this example, 01010011 is 83 in decimal and 01110110 is 118 in decimal. So, 83 + 118 is 201.

Adding 3 binary numbers

If we need to add 3 binary numbers together, the same addition rules apply.

Example – adding 00010001 + 01010101 + 00000001

1 + 1 + 1 = 1, carry 1

0 + 0 + 0 + 1 = 1

0 + 1 + 0 = 1

0 + 0 + 0 = 0

1 + 1 + 0 = 0, carry 1

0 + 0 + 0 = 0

0 + 1 + 0 = 1

0 + 0 + 0 = 0

Overflow

Overflow occurs when the result of a calculation requires more bits - place values - than are in the available range.

For example, when using eight bits, the largest number that can be recorded is 11111111 (decimal 255). When adding together two eight-bit numbers, a situation may occur when the result requires more than eight bits to hold it. For example, adding the binary numbers 11111110 (decimal 254) and 00000010 (decimal 2) would give:

Binary addition: 1 1 1 1 1 1 1 0 plus 0 0 0 0 0 0 1 0 equals 0 0 0 0 0 0 0 0

The result is actually 10000000 (decimal 256), which requires nine bits. However, as only eight bits are available to hold the number, the result would be 00000000 (decimal 0).

As you can see, overflow can have serious consequences for the validity of calculations.

Binary shifts

Binary numbers are multiplied and divided through a process called shifting.

Multiplication

To multiply a number, a binary shift moves all the 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 - 1100 (decimal 12) × 2

1286432168421
1100
128
64
32
16
81
41
20
10

Result - shifting one place to the left gives 11000 (decimal 24)

1286432168421
11000
128
64
32
161
81
40
20
10

Example - 10110 (decimal 22) × 4

1286432168421
10110
128
64
32
161
80
41
21
10

Result - shifting two places to the left gives 1011000 (decimal 88)

1286432168421
1011000
128
641
320
161
81
40
20
10

Multiplying and dividing binary numbers using binary shifts