Skip to main content
ScotlandComputer ScienceSyllabus dot point

How does a computer store positive and negative whole numbers and very large or very small real numbers in binary?

Representing positive and negative integers using two's complement, and representing real numbers using floating-point with a mantissa and exponent.

An SQA Higher Computing Science answer on representing numbers in binary, covering positive and negative integers using two's complement and real numbers using floating-point representation with a mantissa and exponent.

Generated by Claude Opus 4.812 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this key area is asking
  2. Why binary, and why two's complement
  3. Working in two's complement
  4. Floating-point representation of real numbers
  5. The precision versus range trade-off
  6. Examples in context
  7. Try this

What this key area is asking

The SQA wants you to know how a computer represents numbers in binary: positive and negative integers using two's complement, and real numbers using floating-point with a mantissa and an exponent. You should be able to convert a small value to two's complement and explain the floating-point trade-off.

Why binary, and why two's complement

A computer's memory holds only two states, so all numbers are stored in binary (base 2). Positive whole numbers are straightforward binary. The challenge is negative numbers, and the standard solution is two's complement.

Two's complement is used because it lets the processor subtract by adding the negative: the same adder circuit handles both addition and subtraction, and there is only one representation of zero. This is simpler and cheaper in hardware than the alternatives.

Working in two's complement

In nn bits, two's complement represents values from βˆ’2nβˆ’1-2^{n-1} to +2nβˆ’1βˆ’1+2^{n-1}-1. In 8 bits that is βˆ’128-128 to +127+127.

For example +5=0000 0101+5 = 0000\,0101; inverting gives 1111 10101111\,1010; adding 1 gives 1111 1011=βˆ’51111\,1011 = -5. The leading 1 confirms it is negative.

Floating-point representation of real numbers

Whole numbers are not enough: programs need real numbers such as 3.14, and very large or very small magnitudes. These use floating-point representation, the binary version of scientific notation.

This is why it is called floating-point: the same bits can represent a tiny fraction or a huge number by changing the exponent, just as 6.02Γ—10236.02 \times 10^{23} and 1.6Γ—10βˆ’191.6 \times 10^{-19} share a format in decimal.

The precision versus range trade-off

The total number of bits for a floating-point number is fixed, and it is split between the mantissa and the exponent.

So a system that needs to store extremely large and small numbers favours the exponent (range); a system that needs many accurate significant figures favours the mantissa (precision). Designers choose the split for the job.

Examples in context

These representations underpin real computing. Two's complement is the integer format in virtually every processor, which is why an 8-bit signed value wraps from +127+127 to βˆ’128-128 - a real cause of bugs when a counter overflows. Floating-point (the IEEE 754 standard) is how spreadsheets, games and scientific software store decimals, and the precision limit explains why 0.1+0.20.1 + 0.2 does not give exactly 0.30.3 in many languages: 0.1 has no exact finite binary mantissa. Understanding range versus precision explains why scientific code sometimes uses "double precision" (more bits) to reduce rounding error.

Try this

Q1. State the two steps used to negate a number in two's complement. [2 marks]

  • Cue. Invert all the bits, then add 1.

Q2. State which part of a floating-point number determines its precision. [1 mark]

  • Cue. The mantissa.

Q3. For a fixed number of bits, state what increasing the exponent bits does. [1 mark]

  • Cue. It increases the range (at the cost of precision).

Exam-style practice questions

Practice questions written in the style of SQA exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

SQA Higher (style)3 marksUsing two's complement in 8 bits, show how the denary value -20 is represented. Show your working.
Show worked answer β†’

Start from positive 20, then negate using two's complement (invert the bits and add 1).

Step 1, write +20 in 8 bits: 20=16+420 = 16 + 4, so +20=0001 0100+20 = 0001\,0100.

Step 2, invert every bit: 1110 10111110\,1011.

Step 3, add 1: 1110 1011+1=1110 11001110\,1011 + 1 = 1110\,1100.

So βˆ’20=1110 1100-20 = 1110\,1100 in 8-bit two's complement. The leading bit is 1, confirming a negative value.

Markers reward the correct positive binary for 20, inverting the bits, adding 1, and the final answer 1110 1100 with the sign bit set.

SQA Higher (style)4 marksExplain how a real number is stored in floating-point representation, and describe the trade-off between using more bits for the mantissa and more bits for the exponent.
Show worked answer β†’

A real number is stored in floating-point as two parts: a mantissa, which holds the significant digits of the number, and an exponent, which says where the binary point goes (the power of 2 to scale the mantissa by). Together they represent the number as mantissa multiplied by a base raised to the exponent, much like scientific notation.

The trade-off is for a fixed total number of bits: giving more bits to the mantissa increases precision (more significant figures, so the number is stored more accurately), while giving more bits to the exponent increases range (the largest and smallest magnitudes that can be stored). Because the total is fixed, gaining range costs precision and gaining precision costs range.

Markers reward identifying the mantissa as the significant digits and the exponent as the scaling power, and the precision (mantissa) versus range (exponent) trade-off for a fixed number of bits.

Related dot points

Sources & how we know this