A-Level Computer Science / Unit 1: Representing Information

1.1.3 Binary Arithmetic and Overflow

πŸ”’ Lesson slides are available to signed-in users. Sign in

1.1.3 Binary Arithmetic and Overflow

Binary arithmetic is performed using a fixed number of bits. This matters because a result that is mathematically correct may still be impossible to store in the space available. In this section, you will work with unsigned and signed integers, use two's complement, perform binary addition and subtraction, and recognise overflow.

By the end of this section, you should be able to:

  • determine the range of an unsigned or two's complement integer;
  • convert between denary values and fixed-width two's complement;
  • perform binary addition and subtraction;
  • explain how subtraction can be carried out using two's complement;
  • identify overflow in unsigned and signed calculations.

Exam focus

Always identify the representation and the number of bits before calculating. The same bit pattern can represent different values when interpreted as unsigned binary or two's complement.

Fixed-Width Integer Storage

A computer usually allocates a fixed number of bits to each integer. The available bit width determines the range of values that can be represented.

Bit width: the number of bits reserved for storing a value.

Unsigned range

An unsigned integer represents zero or a positive whole number. With n bits, its range is:

0 to 2n βˆ’ 1

Bit width Minimum Maximum
4 bits 0 15
8 bits 0 255
12 bits 0 4095

Two's complement range

With n bits, a two's complement integer has the range:

βˆ’2nβˆ’1 to 2nβˆ’1 βˆ’ 1

Bit width Minimum Maximum
4 bits βˆ’8 7
8 bits βˆ’128 127
12 bits βˆ’2048 2047

Common mistake

Do not use the unsigned range for a signed value. An 8-bit pattern can represent 0 to 255 when unsigned, but βˆ’128 to 127 when interpreted as two's complement.

Representing Negative Integers

Two's complement is a signed binary representation that allows positive and negative values to be processed using the same addition circuitry.

One's complement: invert every bit.
Two's complement: invert every bit and then add 1.

Example: represent βˆ’43 using 8 bits

Step Bit pattern
Write +43 using 8 bits 00101011
Invert every bit 11010100
Add 1 11010101

Therefore, 11010101 represents βˆ’43 in 8-bit two's complement.

Reading a negative two's complement value

If the most significant bit is 1, the value is negative. You can invert the bits and add 1 to find its positive magnitude, then attach a minus sign.

Alternatively, use a negative place value for the most significant bit. For an 8-bit value, the place values are:

βˆ’128, 64, 32, 16, 8, 4, 2, 1

Common mistake

Keep the required number of bits throughout the conversion. Removing leading zeros changes the bit width and may change the meaning of the result.

Interactive: Animated Two's Complement

Use this tool to follow a conversion one stage at a time. Select the bit width first, then choose the conversion direction.

Faster Very slow

Result: --

Press start to see the conversion process.

Binary Addition

Add from right to left. Record one result bit in each column and carry to the next column when the total is 2 or 3.

Column total Write Carry
0 0 0
1 1 0
2 0 1
3 1 1

Worked example

  010110
+ 001101
--------
  100011

The result is 100011β‚‚, which is 35₁₀.

Exam tip

Write each carry above the next column. This makes the method visible and reduces errors caused by losing a carry.

Interactive: Animated Binary Addition

This tool works from the least significant bit to the most significant bit and shows how carries affect each column.

Faster Very slow

Press start to see the addition process.

Overflow

Overflow occurs when a result falls outside the range available for the chosen bit width and representation.

Overflow: a result cannot be represented correctly using the allocated number of bits.

Unsigned overflow

In 4-bit unsigned binary, the largest value is 15. Therefore:

  1101   (13)
+ 0101   (5)
------
1 0010

The mathematical result is 18, but 18 requires five bits. Storing only the lower four bits would give an incorrect value.

Signed overflow

For two's complement addition, overflow occurs when two values with the same sign produce a result with the opposite sign.

Operands Result sign Overflow?
positive + positive negative Yes
negative + negative positive Yes
different signs either sign No signed overflow

Common mistake

A carry out of the most significant bit is not, by itself, a reliable test for signed overflow. Check the signs of the operands and the stored result.

Binary Subtraction

Direct binary subtraction uses borrowing. When 0 must subtract 1, borrow from the next non-zero column to the left.

Worked example using borrowing

  101101
- 011011
--------
  010010

The result is 010010β‚‚, which is 18₁₀.

Subtraction using two's complement

A computer can calculate A βˆ’ B by adding A to the two's complement representation of B. This allows one addition circuit to support both operations.

Why two's complement is preferred

A computer's ALU only needs to implement addition. By representing negative numbers in two's complement, subtraction (A βˆ’ B) is carried out as A + (βˆ’B), using the same adder circuit that performs addition. This avoids needing separate borrowing logic in hardware.

Common mistake

In A βˆ’ B, form the two's complement of B only.

Interactive: Animated Binary Subtraction

Compare direct borrowing with the two's complement method.

Faster Very slow

Press start to see the subtraction process.

Practice and Review

Try these questions

  1. State the range of an 8-bit unsigned integer.
  2. State the range of an 8-bit two's complement integer.
  3. Represent βˆ’43 using 8-bit two's complement.
  4. Convert 11101010 from 8-bit two's complement to denary.
  5. Add 00110110 and 00011101.
  6. Explain whether 01110110 + 00110101 causes signed overflow in 8 bits.
  7. Calculate 101101 βˆ’ 011011 using binary subtraction.
  8. Explain why subtraction can be implemented using addition hardware.

Final checklist

  • Confirm the representation.
  • Keep the stated bit width.
  • Show carries or borrowing.
  • Check the permitted range.
  • Include an overflow explanation when required.