Learning objectives

Convert whole numbers between denary, binary and hexadecimal, and explain why hexadecimal is used as a shorthand for binary in memory dumps, MAC addresses, IP addresses and colour codes

Represent signed integers in two's complement over a fixed word length, negate a value by inverting every bit and adding one, and state the range that word length can hold; read a value held in one’s complement and explain why two’s complement is preferred

Carry out binary addition and subtraction on two's complement values and identify overflow by testing whether the result has left the representable range

Convert between binary coded decimal and denary, and compare the ASCII and Unicode character sets on bits per character, coverage and file size, and distinguish the binary prefixes (KiB, MiB, GiB, TiB) from the decimal prefixes (kB, MB, GB, TB)

Key definitions

Binary

A base-2 number system in which every place value is a power of 2 and the only digits are 0 and 1, matching the two states a switching circuit can hold.

Hexadecimal

A base-16 number system using the digits 0 to 9 followed by the letters A to F, in which one hexadecimal digit stands for exactly four binary digits.

Two's complement

A representation of signed integers in which the most significant bit carries the negative place value −2ⁿ⁻¹, so every value has exactly one pattern and subtraction can be performed by addition.

Overflow

The condition that arises when the result of an arithmetic operation needs more bits than the word length provides, so the value actually stored is not the true result.

Binary coded decimal (BCD)

A representation in which each denary digit is coded separately in its own group of four bits, instead of the number as a whole being converted to binary.

Character set

The agreed list of characters a system can represent together with the unique code assigned to each one, for example ASCII or Unicode.

One’s complement

A representation of signed integers in which a negative value is formed by inverting every bit of the positive value; it has two patterns for zero, and n bits hold −(2ⁿ⁻¹ − 1) to +(2ⁿ⁻¹ − 1).

Binary prefix

A prefix that stands for a power of 2 — kibi (Ki) = 2¹⁰, mebi (Mi) = 2²⁰, gibi (Gi) = 2³⁰, tebi (Ti) = 2⁴⁰ — as distinct from the decimal prefixes kilo, mega, giga and tera, which stand for powers of 10.

Explanation

Place value and conversion. In denary the column headings are powers of 10; in binary they are powers of 2, so an 8-bit byte has the headings 128 64 32 16 8 4 2 1. Reading 11001011 gives 128 + 64 + 8 + 2 + 1 = 203. To go the other way, subtract the largest power of 2 that fits and repeat, or divide repeatedly by 2 and collect the remainders from the bottom upwards. An n-bit pattern has 2ⁿ arrangements, so the largest unsigned value it can hold is 2ⁿ − 1: 255 for a byte, 65 535 for 16 bits.

Why hexadecimal. Sixteen is 2⁴, so one hexadecimal digit replaces exactly four bits and one byte becomes two hex digits. Splitting 11001011 into 1100 and 1011 gives C and B, so the byte is CB. Hexadecimal is not a different way of storing data — the machine still holds bits — it is a shorthand for people. A memory dump, a MAC address such as 4C-2E-9A-11-B0-7F, an IPv6 address, an HTML colour code such as #FF8800 and the operand column of an assembly listing are all written in hex because a technician can read, copy and check hex digits far more reliably than long runs of ones and zeros. A conversion can also be written as an algorithm:

DECLARE Value : INTEGER

DECLARE Pattern : STRING

DECLARE Index : INTEGER

Value ← 203

Pattern ← ""

FOR Index ← 1 TO 8

IF Value MOD 2 = 1 THEN

Pattern ← "1" & Pattern

ELSE

Pattern ← "0" & Pattern

ENDIF

Value ← Value DIV 2

NEXT Index

OUTPUT Pattern

Signed integers. Two's complement keeps the ordinary place values but makes the leading one negative: for a byte the headings become −128 64 32 16 8 4 2 1. So 11010011 is −128 + 64 + 16 + 2 + 1 = −45. To negate a value, invert every bit and add one: 45 is 00101101, inverting gives 11010010, and adding 1 gives 11010011. Applying the same rule again returns the original value. The representable range for n bits is −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1, so a byte holds −128 to +127 and a 16-bit word holds −32 768 to +32 767. The range is not symmetrical because the single pattern for zero sits on the non-negative side.

One’s complement. An older way of storing signed integers is one’s complement: a negative value is formed by inverting every bit of the positive value and nothing more, so +45 is 00101101 and −45 is 11010010. Read with place values, the leading bit is worth −(2ⁿ⁻¹ − 1), which is −127 in a byte, so 11010010 is −127 + 64 + 16 + 2 = −45. The range for n bits is therefore symmetrical, −(2ⁿ⁻¹ − 1) to +(2ⁿ⁻¹ − 1), or −127 to +127 for a byte, because one pattern is wasted: 00000000 is +0 and 11111111 is −0. Two zeros make every test for equality awkward, and addition needs an extra correction, because a carry out of the leading column has to be added back in at the least significant bit. That is why processors store signed integers in two’s complement, which has a single zero and needs no correction. To read a one’s complement byte whose leading bit is 1, invert every bit and write a minus sign in front: 11100110 inverts to 00011001 = 25, so it represents −25.

Addition, subtraction and overflow. Add two's complement values column by column exactly as unsigned binary, carrying where a column totals 2 or more, and discard any carry out of the leading column. Subtraction is done by negating the second value and adding: 01010110 (86) − 00011101 (29) becomes 01010110 + 11100011 = 00111001, which is 57. Overflow is detected by the signs, not by the discarded carry: if two positive values give a negative result, or two negative values give a positive result, the true answer has left the range. Adding 01001101 (77) and 00110110 (54) gives 10000011, which reads as −125 rather than 131, because 131 is outside −128 to +127. Adding values of opposite sign can never overflow.

Binary coded decimal. In BCD each denary digit is coded on its own in four bits, so 617 is stored as 0110 0001 0111. Only the patterns 0000 to 1001 are valid; 1010 to 1111 are unused. BCD is deliberately wasteful of space and awkward to do arithmetic with, but it converts instantly to and from the digits a person sees, which is why it appears in calculator and instrument displays, in seven-segment driver chips and in machines that handle monetary amounts, where holding a value such as $12.45 as exact decimal digits avoids the rounding that comes from storing 0.45 in binary.

Character sets. A character set maps each character to a number. ASCII uses 7 bits and so covers 128 codes — the unaccented English letters, digits, punctuation and control codes — and extended ASCII stretches this to 8 bits and 256 codes. That is far too few for the writing systems of the world, so Unicode was introduced: it has room for over a million code points and already assigns codes to roughly 150 000 characters, covering Arabic, Chinese, Devanagari, Greek, emoji and historic scripts, and it keeps the original ASCII codes as its first 128 values so older files still read correctly. The number of bits actually stored depends on the encoding: UTF-8 uses one byte for ASCII characters and up to four for others, while UTF-32 uses four bytes for every character. Unicode text is therefore generally larger than the same text in ASCII, which is the price paid for universal coverage.

Binary and decimal prefixes. Sizes of storage use two families of prefix, and the syllabus expects you to keep them apart. The decimal prefixes are powers of 10, exactly as in science: kilo (kB) = 10³ = 1000 bytes, mega (MB) = 10⁶, giga (GB) = 10⁹ and tera (TB) = 10¹² bytes. The binary prefixes are powers of 2 and have an i in the symbol: kibi (KiB) = 2¹⁰ = 1024 bytes, mebi (MiB) = 2²⁰ = 1 048 576 bytes, gibi (GiB) = 2³⁰ and tebi (TiB) = 2⁴⁰ bytes. The two families start 2.4% apart and drift further apart at every step, because each binary step multiplies by 1024 instead of 1000: a tebibyte is almost 10% larger than a terabyte. That is why a drive sold as 500 GB, which is 500 × 10⁹ bytes, is reported as about 465.7 GiB by an operating system that counts in binary units — no space is missing, it is simply being measured in a larger unit. RAM is built in powers of two, so its size is really a binary quantity even when the label says GB, while the makers of drives, and the speeds of networks, use decimal units.

Formulae

Largest unsigned value in n bits = 2ⁿ − 1; number of different patterns = 2ⁿ

Two's complement range for n bits: −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1 (byte: −128 to +127)

Negate: invert every bit, then add 1

1 hexadecimal digit = 4 bits; 1 byte = 2 hexadecimal digits

Overflow test: same-sign operands producing an opposite-sign result

ASCII: 7 bits, 128 codes (8-bit extended ASCII: 256 codes)

One’s complement range for n bits: −(2ⁿ⁻¹ − 1) to +(2ⁿ⁻¹ − 1), with two patterns for zero (byte: −127 to +127)

Binary prefixes: 1 KiB = 2¹⁰ = 1024 bytes; 1 MiB = 2²⁰; 1 GiB = 2³⁰; 1 TiB = 2⁴⁰ bytes. Decimal prefixes: 1 kB = 10³; 1 MB = 10⁶; 1 GB = 10⁹; 1 TB = 10¹² bytes

Worked examples

q: A memory dump shows the single byte CB. (a) Write the byte in binary. (b) State its denary value if the byte is unsigned. (c) State its denary value if the byte is a two's complement signed integer. (d) Write the two's complement byte for −45. (e) Give your answer to (d) in hexadecimal.

steps: (a) C is 1100 and B is 1011, so the byte is 11001011.

(b) Unsigned: 128 + 64 + 8 + 2 + 1 = 203.

(c) The most significant bit is 1, so the value is negative: −128 + 64 + 8 + 2 + 1 = −53. Checking by negation, inverting 11001011 gives 00110100 and adding 1 gives 00110101 = 53, so the byte is −53.

(d) 45 = 00101101. Invert every bit: 11010010. Add 1: 11010011.

(e) Split 11010011 into 1101 and 0011, giving D and 3, so −45 is D3.

In context

A MAC address is 48 bits, but it is printed as twelve hexadecimal digits in six pairs so that a network technician can read it off a label and type it into a filter without miscounting a run of ones and zeros.

Web colour codes are hexadecimal bytes: #FF8800 sets the red channel to 255, the green channel to 136 and the blue channel to 0.

Point-of-sale terminals and metering equipment often hold amounts in BCD, because each digit can be sent straight to its own display driver and money values stay exact in decimal.

Common misconceptions

Students often treat the leading bit of a two's complement number as a sign flag that is simply stripped off. In fact it carries the place value −2ⁿ⁻¹, so 11010011 is −128 + 64 + 16 + 2 + 1 = −45, not −83.

Students often give the range of a signed byte as −127 to +127. In fact it is −128 to +127, because there is only one pattern for zero and it is counted on the non-negative side.

Students often read a BCD pattern as ordinary binary. In fact 0110 0001 0111 is BCD for 617, whereas plain binary for 617 is 1001101001 — the same number, completely different bits.

Students often say Unicode always uses 16 bits per character. In fact Unicode defines code points and the encoding decides the storage: UTF-8 spends one byte on an ASCII character and up to four on others, while UTF-32 always spends four.

Students often treat a kilobyte and a kibibyte as the same size. In fact 1 kB is 1000 bytes and 1 KiB is 1024 bytes, and the gap widens at every step, so a 2 TB drive holds only about 1.82 TiB.

Study tips

Write the headings 128 64 32 16 8 4 2 1 above the bits before converting; most lost marks are place-value slips rather than arithmetic errors.

Group bits into fours from the right when converting to hexadecimal, padding the leftmost group with zeros if it is short.

For an overflow question name the two values, give the true total and then say that it lies outside −128 to +127; the word overflow on its own earns nothing.

Show the inverted pattern as a separate line when negating a number, so a method mark survives a slip in the final addition.

Summary

Binary place values are powers of 2; one hexadecimal digit stands for exactly four bits.

In two's complement the leading bit has place value −2ⁿ⁻¹, giving the range −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1.

Negate by inverting every bit and adding 1; subtract by adding the negated value.

Overflow occurs when two same-sign values produce a result of the opposite sign.

BCD codes each denary digit in its own four bits; ASCII uses 7 bits, Unicode covers over a million code points.

Check your understanding

Question 1: The 8-bit two's complement pattern 11110000 represents…

1. −16

2. 240

3. −112

Question 2: How many different patterns can be held in 12 bits?

1. 2048

2. 144

3. 4096

Question 3: The denary number 2025 written in hexadecimal is…

1. 5F7

2. 7E9

3. 8A1