Skip to main content

Number Systems

1. Positional Number Systems

Definition

A positional number system represents a number as a sequence of digits dn1dn2d1d0d_{n-1}d_{n-2}\ldots d_1d_0 in base bb, where the value of the number is:

N=i=0n1dibiN = \sum_{i=0}^{n-1} d_i \cdot b^i

Each digit did_i satisfies 0di<b0 \leq d_i \lt{} b. The most significant digit (MSD) is dn1d_{n-1} and the least significant digit (LSD) is d0d_0.

The bases relevant to A Level Computer Science are:

BaseNameDigits Used
10Denary0, 1, 2, ..., 9
2Binary0, 1
8Octal0, 1, 2, ..., 7
16Hex0–9, A, B, C, D, E, F

We use subscript notation to denote the base: 10112=11101011_2 = 11_{10}.

Conversion Between Bases

Base bb to Denary

Algorithm. Given digits dn1d0d_{n-1}\ldots d_0 in base bb, compute:

N=i=0n1dibiN = \sum_{i=0}^{n-1} d_i \cdot b^i

Correctness. This is exactly the definition of positional notation, so correctness is immediate.

Proof of termination. The sum has exactly nn terms, so the algorithm terminates after nn iterations.

Example: Convert 1A3161A3_{16} to denary

1×162+10×161+3×160=256+160+3=419101 \times 16^2 + 10 \times 16^1 + 3 \times 16^0 = 256 + 160 + 3 = 419_{10}

Denary to Base bb (Repeated Division)

Algorithm. To convert denary NN to base bb:

  1. Set i=0i = 0
  2. While N>0N \gt{} 0: digit di=Nmodbd_i = N \bmod b; set N=N/bN = \lfloor N / b \rfloor; ii+1i \leftarrow i + 1
  3. The result is di1di2d0d_{i-1}d_{i-2}\ldots d_0 (digits collected in reverse order)

Correctness proof. We prove by induction on the number of division steps that the algorithm produces the correct base-bb representation.

Base case. After the first step, d0=Nmodbd_0 = N \bmod b and N=N/bN' = \lfloor N / b \rfloor. We have N=d0+bNN = d_0 + b \cdot N', so d0d_0 is indeed the coefficient of b0b^0.

Inductive step. Assume after kk steps we have N=d0+d1b++dk1bk1+bkNkN = d_0 + d_1 b + \cdots + d_{k-1}b^{k-1} + b^k \cdot N_k where Nk=N/bkN_k = \lfloor N / b^k \rfloor. The next step computes dk=Nkmodbd_k = N_k \bmod b and Nk+1=Nk/bN_{k+1} = \lfloor N_k / b \rfloor, giving Nk=dk+bNk+1N_k = d_k + b \cdot N_{k+1}. Substituting:

N=i=0k1dibi+bk(dk+bNk+1)=i=0kdibi+bk+1Nk+1N = \sum_{i=0}^{k-1} d_i b^i + b^k(d_k + b \cdot N_{k+1}) = \sum_{i=0}^{k} d_i b^i + b^{k+1} N_{k+1}

This maintains the invariant. When Nk=0N_k = 0, the representation is complete.

Example: Convert 15610156_{10} to binary
StepNNNmod2N \bmod 2N/2\lfloor N/2 \rfloor
1156078
278039
339119
41919
5914
6402
7201
8110

Reading bottom to top: 15610=100111002156_{10} = 10011100_2

Binary to Hexadecimal (and vice versa)

Since 16=2416 = 2^4, each hex digit corresponds to exactly 4 binary digits. Group binary digits from right to left in groups of 4, then convert each group.

Example: Convert 10111011011210111011011_2 to hex

Group: 101 1101 1011101\ 1101\ 1011

Pad left group: 0101 1101 10110101\ 1101\ 1011

Convert: 5 D B=5DB165\ \mathrm{D}\ \mathrm{B} = 5DB_{16}

Hexadecimal to Binary

Replace each hex digit with its 4-bit binary equivalent.

Example: Convert 3F7163F7_{16} to binary

3=00113 = 0011, F=1111F = 1111, 7=01117 = 0111

Result: 0011111101112=111111101112001111110111_2 = 11111110111_2

Octal Conversions

Since 8=238 = 2^3, each octal digit maps to exactly 3 binary digits. Convert by grouping in 3s (or multiplying/dividing by 8).


2. Binary Arithmetic

Binary Addition

We add bitwise from right to left, with carries:

AABBCinC_{in}SumCoutC_{out}
00000
00110
01010
01101
10010
10101
11001
11111

Proposition (Carry propagation). The carry into bit position ii depends only on bits 00 through i1i-1.

Proof. The carry CiC_i is defined recursively: C0=0C_0 = 0 and Ci+1=(AiBi)+(AiCi)+(BiCi)C_{i+1} = (A_i \cdot B_i) + (A_i \cdot C_i) + (B_i \cdot C_i). By structural induction on ii, CiC_i is a Boolean function of {A0,B0,,Ai1,Bi1}\{A_0, B_0, \ldots, A_{i-1}, B_{i-1}\} only. \square

Example: Add 10112+011021011_2 + 0110_2
Carry: 1 1 1 0 0
1 0 1 1
+ 0 1 1 0
---------
1 0 0 0 1

10112=11101011_2 = 11_{10}, 01102=6100110_2 = 6_{10}, 100012=171010001_2 = 17_{10}. Correct. ✓

Binary Subtraction

Binary subtraction can be performed using two's complement (see below).


3. Two's Complement Representation

Motivation

We need a way to represent both positive and negative integers in binary, using a fixed number of bits.

Definition

For an nn-bit two's complement representation, the range of representable integers is:

[2n1, 2n11][-2^{n-1},\ 2^{n-1} - 1]

The representation of a non-negative integer xx is simply its standard nn-bit binary representation. The representation of a negative integer x-x (where x>0x \gt{} 0) is:

TwosCompn(x)=2nx\mathrm{TwosComp}_n(-x) = 2^n - x

Derivation: Why xˉ+1\bar{x} + 1 Works

Theorem. For any nn-bit positive integer xx (1x2n111 \leq x \leq 2^{n-1} - 1), the two's complement representation of x-x equals xˉ+1\bar{x} + 1 (where xˉ\bar{x} is the bitwise NOT of the nn-bit representation of xx, and +1+1 is binary addition).

Proof. The nn-bit representation of xx has bits xn1x0x_{n-1}\ldots x_0. The bitwise complement xˉ\bar{x} has bits xˉn1xˉ0\bar{x}_{n-1}\ldots\bar{x}_0, where xˉi=1xi\bar{x}_i = 1 - x_i. The value of xˉ\bar{x} as an unsigned nn-bit number is:

xˉ=i=0n1(1xi)2i=i=0n12ii=0n1xi2i=(2n1)x\bar{x} = \sum_{i=0}^{n-1}(1 - x_i) \cdot 2^i = \sum_{i=0}^{n-1} 2^i - \sum_{i=0}^{n-1} x_i \cdot 2^i = (2^n - 1) - x

Therefore:

xˉ+1=(2n1)x+1=2nx\bar{x} + 1 = (2^n - 1) - x + 1 = 2^n - x

This is exactly the definition of the two's complement of x-x. \square

Corollary. Adding xx and x-x in two's complement yields zero (modulo 2n2^n).

Proof.

x+(xˉ+1)=x+2nx=2nx + (\bar{x} + 1) = x + 2^n - x = 2^n

In nn bits, 2n2^n is represented as 00000\ldots0 with a carry out of bit position n1n-1, which is discarded. Hence the result is 00. \square

Two's Complement Addition and Overflow

When adding two nn-bit two's complement numbers, the result is correct (modulo 2n2^n) if and only if no overflow occurs.

Overflow detection rules:

ConditionOverflow?
Positive + Positive = NegativeYes
Negative + Negative = PositiveYes
Positive + NegativeNever
Same signs produce same sign resultNo

Formally: overflow occurs if and only if the carry into the MSB differs from the carry out of the MSB.

Example: Add 5-5 and 33 in 4-bit two's complement

5=10112-5 = 1011_2 (since 245=11=101122^4 - 5 = 11 = 1011_2) 3=001123 = 0011_2

Carry: 1 1 1 1 0
1 0 1 1
+ 0 0 1 1
---------
1 1 1 0

111021110_2 in two's complement: 2-2. Let us verify: 5+3=2-5 + 3 = -2. ✓

No overflow (negative + positive never overflows).

Example: Overflow — Add 66 and 55 in 4-bit two's complement

6=011026 = 0110_2, 5=010125 = 0101_2

Carry: 0 1 1 0 0
0 1 1 0
+ 0 1 0 1
---------
1 0 1 1

Result: 10112=51011_2 = -5 in two's complement. But 6+5=116 + 5 = 11, which is outside the range [8,7][-8, 7] for 4 bits. Overflow detected: positive + positive yielded negative. ✓

info

info

  • AQA: Requires two's complement for 8-bit and 16-bit numbers.
  • CIE: Requires two's complement for 8-bit numbers specifically.
  • OCR: Requires understanding of sign and magnitude as well as two's complement.

4. Fixed-Point Binary Representation

Definition

A fixed-point binary number uses a specified number of bits for the integer part and a specified number of bits for the fractional part.

For an nn-bit number with mm integer bits and ff fractional bits (n=m+fn = m + f):

N=i=0m1bi2i+j=1fbm+j12jN = \sum_{i=0}^{m-1} b_i \cdot 2^i + \sum_{j=1}^{f} b_{m+j-1} \cdot 2^{-j}

where b0b_0 is the LSB of the integer part and bm+f1b_{m+f-1} is the MSB.

Range and Precision

For an unsigned fixed-point number with mm integer bits and ff fractional bits:

  • Range: [0, 2m2f][0,\ 2^m - 2^{-f}]
  • Precision (resolution): 2f2^{-f}

For signed (two's complement) with mm integer bits and ff fractional bits:

  • Range: [2m1, 2m12f][-2^{m-1},\ 2^{m-1} - 2^{-f}]
Example: 8-bit fixed-point with 5 integer bits and 3 fractional bits

01101011201101011_2:

Integer part: 011012=131001101_2 = 13_{10} Fractional part: 0112=0×21+1×22+1×23=0.37510011_2 = 0 \times 2^{-1} + 1 \times 2^{-2} + 1 \times 2^{-3} = 0.375_{10}

Value: 13.3751013.375_{10}

Range: [0, 31.875][0,\ 31.875], Precision: 0.1250.125


5. Binary Coded Decimal (BCD)

Definition

In Binary Coded Decimal (BCD), each decimal digit (0–9) is represented by its 4-bit binary equivalent.

DecimalBCD
00000
10001
20010
......
91001

The codes 10101010 through 11111111 are invalid in BCD.

Properties

  • Each decimal digit requires exactly 4 bits (a nibble)
  • A kk-digit decimal number requires 4k4k bits in BCD
  • BCD is less space-efficient than pure binary: e.g., 99910999_{10} requires 12 bits in BCD but only 10 bits in pure binary (111110011121111100111_2)
  • BCD avoids rounding errors in decimal arithmetic — useful in financial systems
warning

Pitfall BCD is NOT the same as converting the entire number to binary. 121012_{10} in BCD is 00010010200010010_2, NOT 110021100_2.


6. Character Encoding

ASCII

The American Standard Code for Information Interchange (ASCII) is a 7-bit character encoding standard representing 128 characters:

  • 003131: Control characters (NUL, BEL, LF, CR, etc.)
  • 3232: Space
  • 48485757: Digits '0'–'9'
  • 65659090: Uppercase letters 'A'–'Z'
  • 9797122122: Lowercase letters 'a'–'z'

Key property: The codes for uppercase and lowercase letters differ by exactly 3232 (252^5), so bit 5 distinguishes them. Specifically, 'A' = 6565 and 'a' = 9797.

Extended ASCII uses 8 bits (256 characters) but is not standardised — various extensions exist.

Unicode

Unicode is a universal character encoding standard that aims to represent every character from every writing system. Key facts:

  • Unicode assigns a unique code point to each character, written as U+XXXX (e.g., 'A' = U+0041, '€' = U+20AC)
  • As of Unicode 15.0, there are over 149,000 characters across 161 scripts
  • Unicode is an abstract standard — it defines code points, not how they are stored in bytes

UTF-8 Encoding

UTF-8 is a variable-length encoding of Unicode code points into bytes. It is backward-compatible with ASCII.

Encoding rules:

Code Point RangeBinary PatternBytes
U+0000U+007F0xxxxxxx1
U+0080U+07FF110xxxxx 10xxxxxx2
U+0800U+FFFF1110xxxx 10xxxxxx 10xxxxxx3
U+10000U+10FFFF11110xxx 10xxxxxx 10xxxxxx 10xxxxxx4

Properties of UTF-8:

  1. Self-synchronising: Any byte can identify whether it is a single-byte character, the start of a multi-byte sequence, or a continuation byte. Continuation bytes always start with 10, start bytes start with 11...0.
  2. ASCII compatible: All ASCII text is valid UTF-8.
  3. Prefix-free: No valid UTF-8 sequence is a prefix of another valid sequence.
Example: Encode '€' (U+20AC) in UTF-8

20AC16=0010 0000 1010 1100220AC_{16} = 0010\ 0000\ 1010\ 1100_2

This falls in the range U+0800U+FFFF, so it uses 3 bytes: 1110xxxx 10xxxxxx 10xxxxxx.

Fill in the bits from the code point:

0010 0000 1010 11000010\ 0000\ 1010\ 1100

Split: 0010x000010xx101100xx\underbrace{0010}_{x} \underbrace{000010}_{xx} \underbrace{101100}_{xx}

Result: 11100010 10000010 10101100 = E2 82 AC in hex.


7. Representing Negative Numbers: Other Methods

Sign and Magnitude

The MSB represents the sign (0 = positive, 1 = negative), and the remaining bits represent the magnitude.

  • Range for nn bits: [(2n11), 2n11][-(2^{n-1} - 1),\ 2^{n-1} - 1]
  • Two representations of zero: +0=0000+0 = 000\ldots0 and 0=1000-0 = 100\ldots0
warning

Pitfall Sign and magnitude is rarely used in practice because:

  1. It has two representations of zero
  2. Addition requires different logic depending on the signs
  3. The range is asymmetric

Problem Set

Problem 1. Convert 2F7162F7_{16} to denary.

Hint

Use the positional formula with b=16b = 16. Remember F=15F = 15.

Answer

2×256+15×16+7=512+240+7=759102 \times 256 + 15 \times 16 + 7 = 512 + 240 + 7 = 759_{10}

Problem 2. Convert 31410314_{10} to binary and hexadecimal.

Hint

Use repeated division by 2, then group into 4-bit nibbles for hex.

Answer

Binary: 314=256+32+16+8+2=1001110102314 = 256 + 32 + 16 + 8 + 2 = 100111010_2

Hex: Group as 0001 0011 1010=13A160001\ 0011\ 1010 = 13A_{16}

Problem 3. Represent 42-42 in 8-bit two's complement. Verify by adding it to +42+42 and showing the result is zero.

Hint

Find the binary of 42, flip all bits, add 1.

Answer

42=00101010242 = 00101010_2

Bitwise NOT: 11010101211010101_2

Add 1: 11010110211010110_2

Verification: 00101010+11010110=10000000000101010 + 11010110 = 100000000. The carry out of 8 bits is discarded, leaving 0000000000000000. ✓

Problem 4. Perform the addition 101101102+01101011210110110_2 + 01101011_2 and interpret the result in 8-bit unsigned and 8-bit two's complement.

Hint

Do the binary addition first. Then interpret based on the encoding scheme.

Answer
1 0 1 1 0 1 1 0
+ 0 1 1 0 1 0 1 1
-----------------
1 0 0 1 0 0 0 0 1

8-bit result (discard carry): 00100001200100001_2

  • Unsigned: 001000012=331000100001_2 = 33_{10}. Check: 182+107=289182 + 107 = 289. But 289mod256=33289 \bmod 256 = 33. ✓
  • Two's complement: 001000012=331000100001_2 = 33_{10}. Check: 74+107=33-74 + 107 = 33. ✓

Problem 5. A fixed-point system uses 12 bits: 8 for the integer part and 4 for the fractional part. What is the range and precision? Convert 01011010.101001011010.1010 to denary.

Hint

Precision is 242^{-4}. Range depends on whether signed or unsigned.

Answer

Unsigned range: [0, 255.9375][0,\ 255.9375], precision: 0.06250.0625 (242^{-4}).

010110102=64+16+8+2=901001011010_2 = 64 + 16 + 8 + 2 = 90_{10}

.10102=0.5+0.125=0.62510.1010_2 = 0.5 + 0.125 = 0.625_{10}

Value: 90.6251090.625_{10}

Problem 6. Encode the string "Hi!" in ASCII (hex). Then explain how many bytes this would take in UTF-8.

Hint

Look up each character's ASCII code point.

Answer

'H' = 72 = 481648_{16} = 01001000 'i' = 105 = 691669_{16} = 01101001 '!' = 33 = 211621_{16} = 00100001

In ASCII hex: 48 69 21

Since all code points are below U+007F, UTF-8 uses 1 byte per character, so 3 bytes total. UTF-8 encoding is identical to ASCII for these characters.

Problem 7. Encode the character 'ñ' (U+00F1) in UTF-8. Show each step.

Hint

U+00F1 falls in the 2-byte range U+0080U+07FF. Use the pattern 110xxxxx 10xxxxxx.

Answer

00F116=0000 1111 0001200F1_{16} = 0000\ 1111\ 0001_2

Split into bits: 00011x110001xx\underbrace{00011}_{x} \underbrace{110001}_{xx}

First byte: 110 + 00011 = 11000011 = C316C3_{16} Second byte: 10 + 110001 = 10110001 = B116B1_{16}

UTF-8: C3 B1

Problem 8. A student claims that 110121101_2 in 4-bit two's complement represents 3-3. Another claims it represents +13+13. Who is correct? Explain.

Hint

What does the MSB tell you in two's complement?

Answer

The first student is correct for two's complement interpretation. The MSB is 1, so it is negative.

Value: (2413)=(1613)=3- (2^4 - 13) = -(16 - 13) = -3. ✓

The second student is interpreting it as an unsigned number: 8+4+1=138 + 4 + 1 = 13. This is also valid — the bit pattern is the same, but the interpretation differs. Context determines which encoding is used.

Problem 9. Prove that the two's complement of the two's complement of xx equals xx (for xx in the valid range, excluding 2n1-2^{n-1}).

Hint

Start with TwosComp(x)=2nx\mathrm{TwosComp}(x) = 2^n - x. Apply the operation again.

Answer

Let y=TwosCompn(x)=2nxy = \mathrm{TwosComp}_n(x) = 2^n - x.

TwosCompn(y)=2ny=2n(2nx)=x\mathrm{TwosComp}_n(y) = 2^n - y = 2^n - (2^n - x) = x. ✓

The exception is x=2n1x = -2^{n-1}, whose two's complement is 2n(2n1)=2n+2n1=2n132^n - (-2^{n-1}) = 2^n + 2^{n-1} = 2^{n-1} \cdot 3, which exceeds nn bits. In nn-bit arithmetic, 2n(2n1)mod2n=2n12^n - (-2^{n-1}) \bmod 2^n = 2^{n-1}, which is the bit pattern 1000100\ldots0 — the same as 2n1-2^{n-1}. So 2n1-2^{n-1} is its own two's complement.

Problem 10. In a system using 6-bit two's complement, what is the result of adding 16-16 and 15-15? Does overflow occur?

Hint

Range of 6-bit two's complement is [32,31][-32, 31]. Compute 16+(15)=31-16 + (-15) = -31. Is this in range?

Answer

16=2616=48=1100002-16 = 2^6 - 16 = 48 = 110000_2 15=2615=49=1100012-15 = 2^6 - 15 = 49 = 110001_2

1 1 0 0 0 0
+ 1 1 0 0 0 1
-----------
1 1 0 0 0 0 1

6-bit result: 1000012100001_2. This is (2633)=31-(2^6 - 33) = -31.

Check: 16+(15)=31-16 + (-15) = -31. No overflow: 31-31 is in range [32,31][-32, 31]. ✓

Both inputs are negative and the result is negative, so no overflow.

:::

:::

:::