1.Lesson overview
- 1.1 Number systems
- 3.1 Binary
- 3.1 Number bases
- 3.2 Converting between number bases
- 3.4 Binary arithmetic
- Understand how and why computers use binary to represent all forms of data.
- Understand the denary, binary and hexadecimal number systems.
- Convert between positive denary and binary, denary and hexadecimal, and hexadecimal and binary.
- Understand how and why hexadecimal is a beneficial method of data representation.
- Add two positive 8-bit binary integers.
- Understand the concept of overflow and why it occurs in binary addition.
- Perform a logical binary shift on a positive 8-bit binary integer and understand its effect.
- Use the two's complement number system to represent positive and negative 8-bit binary integers.
- Understand how sign-and-magnitude represents positive and negative 8-bit integers, and explain why two's complement is preferred.
- Perform an arithmetic shift on an 8-bit two's complement integer and explain how it differs from a logical shift.
Every photograph, song and document inside a computer is ultimately a string of ones and zeros. That is not a design choice made for elegance — it follows from the hardware. A transistor is either conducting or not, and a circuit is reliably either on or off, so a system with exactly two states is the one that can be built.
Working directly in binary is painful for people, though: one byte takes eight digits to write. Hexadecimal exists to solve that human problem, and the reason it works so neatly is that one hex digit is exactly four binary digits.
- Section 2 covers why computers use binary.
- Section 3 covers the three number systems.
- Sections 4–6 cover the conversions.
- Section 7 covers why hexadecimal is useful.
- Section 8 covers binary addition and overflow.
- Section 9 covers logical shifts.
- Section 10 covers two's complement.
- Sections 11–13 consolidate with worked examples, misconceptions and a summary.
2.Why Computers Use Binary
Computers use binary because their components have two easily distinguished states:
- A transistor is either conducting or not conducting.
- A circuit either carries a voltage or does not.
- A region of a hard disk is magnetised in one direction or the other.
Each state is represented by a 1 or a 0, and all forms of data — numbers, text, images, sound, instructions — are represented using them.
A circuit could in principle use ten voltage levels to store a denary digit directly. It is not done because distinguishing ten levels reliably is far harder: small fluctuations in voltage, temperature or component quality could push one level into another and corrupt the data.
With only two states there is a huge margin between them, so a circuit can be built cheaply and still be accurate. Binary trades a longer representation for reliability — and reliability is what matters.
Put numbers on it: a typical digital circuit might treat anything below as a firm 0 and anything above as a firm 1, leaving a wide gap in between that the circuit is never allowed to settle in. Splitting the same – range into ten equal bands for a denary digit would leave barely half a volt per digit and almost no safety margin either side — a small voltage dip caused by a long wire or a warm component could flip a reading from a into a . With only two widely separated states, the same cheap, imperfect components can be trusted completely.
Two pieces of vocabulary follow: a single binary digit is a bit, and eight bits make one byte.
3.The Three Number Systems
| System | Base | Digits used | Example |
|---|---|---|---|
| Denary | 10 | – | |
| Binary | 2 | and | |
| Hexadecimal | 16 | – then A–F |
Every system works the same way: each column is worth the base times the column to its right.
- Denary columns: , , , .
- Binary columns: , , , , , , , — these eight are worth memorising, since they cover one byte.
- Hexadecimal columns: , , .
Formally, the column places from the right (counting the units column as ) is worth . That is why denary columns are powers of ten (, , ), binary columns are powers of two (, , ) and hexadecimal columns are powers of sixteen (, , ). Once this pattern is fixed, converting any whole number in any base to denary is the same two-step recipe: multiply each digit by its column's power of the base, then add the results — the method used throughout this chapter is really only ever this one idea applied three times.
Hexadecimal needs sixteen different digits, but only ten symbols exist, so letters are used for the remaining six:
| Hex | A | B | C | D | E | F |
|---|---|---|---|---|---|---|
| Denary |
Note that F is 15, not 16. Counting starts at zero, so the sixteen digits run from to — the commonest error in hex conversion.
4.Converting Between Denary and Binary
- 1Identify what you have and wantDenary, binary or hexadecimal — and which direction.
- 2Hex and binary?Use the four-bit rule — direct substitution, no arithmetic. Group from the right.
- 3Denary to binary?Work down from 128, subtracting each place value that fits.
- 4Denary to hex?Divide by 16 — the quotient is the first digit, the remainder the second.
- 5Check the answerBinary should be 8 bits; remainders of 10–15 become letters A–F.
Write the place values above the digits and add up the values where there is a 1.
| Place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
| Binary digit | 1 | 0 | 0 | 1 | 1 | 1 | 0 | 0 |
Add the place values above the columns containing 1: .
Work from the largest place value downwards. At each column ask: does this value fit into what is left?
- 1Stage 1Convert 156 to binary.
- 2Stage 2128 fits into 156? Yes -> write 1, remainder 156 - 128 = 28
- 3Stage 364 fits into 28? No -> write 0
- 4Stage 432 fits into 28? No -> write 0
- 5Stage 516 fits into 28? Yes -> write 1, remainder 28 - 16 = 12
- 6Stage 68 fits into 12? Yes -> write 1, remainder 12 - 8 = 4
- 7Stage 74 fits into 4? Yes -> write 1, remainder 4 - 4 = 0
- 8Stage 82 fits into 0? No -> write 0
- 9Stage 91 fits into 0? No -> write 0
- 10Answer10011100
The remainder must reach exactly zero by the end. If it does not, an error has been made somewhere — a useful built-in check.
Answers should normally be given as 8 bits, padding with leading zeros if necessary: is , not .
- 1Write +10001100100 · 8-bit width
- 2Invert every bit10011011 · 0 ↔ 1
- 3Add one10011100 · candidate −100
- 4Check signed values−128 + 16 + 8 + 4 = −100
For 8 bits the place values are −128, 64, 32, 16, 8, 4, 2, 1.
5.Converting Between Denary and Hexadecimal
Multiply each digit by its place value and add.
- 1Convert 9C to denary.
- 2Place value: 16 1
- 3Hex digit: 9 C
- 49 x 16 = 144
- 5, and 12 x 1 = 12
- 6144 + 12 = 156
Divide by 16. The quotient is the first digit and the remainder is the second.
- 1Stage 1Convert 156 to hexadecimal.
- 2Stage 2156 / 16 = 9 remainder 12
- 3Stage 3First
- 4Stage 4Second
- 5Answer9C
Remember to convert any remainder of or more into its letter. A remainder of is written C, not .
6.Converting Between Hexadecimal and Binary
This is the easiest conversion of the three, because one hexadecimal digit is exactly four binary digits. There is no arithmetic — only substitution.
- 1Hex to binaryreplace each hex digit with its 4 bits.
- 2Stage 29 -> 1001
- 3Stage 3C -> 1100
- 4Stage 49C -> 1001 1100 -> 10011100
- 5Binary to hexsplit into groups of 4 from the RIGHT, then convert each.
- 6Stage 610011100 -> 1001 1100
- 7Stage 7| |
- 8Stage 89 C
- 9Answer9C
Split from the right, and pad the leftmost group with leading zeros if it is short. splits as , giving — splitting from the left would give the wrong answer.
Because hex and binary convert so easily, awkward denary-to-binary conversions can be routed through hex:
- 1Denary hex by dividing by 16.
- 2Hex binary by substituting four bits per digit.
For a large value this is often quicker and less error-prone than working down eight binary place values.
7.Why Hexadecimal Is Used
The examinable idea is that hexadecimal is used for the benefit of people, not machines. The computer still works entirely in binary; hex is simply a shorthand for displaying that binary to a human.
So the strongest answer pairs the two facts: hex is shorter and less error-prone to read and write, and it converts to binary trivially because each digit maps to exactly four bits.
You meet this every day without necessarily noticing. A web colour written #2E86C1 is three hexadecimal byte values placed side by side — , and — giving the red, green and blue brightness of a shade of blue, each running from (none) to (full, in denary). A crash report or debugger showing a memory address such as 0x7FFE2A10 is doing the same job: a 32-bit binary address compressed into eight readable hex digits instead of thirty-two error-prone binary ones.
8.Binary Addition and Overflow
- 10 + 0 = 0
- 20 + 1 = 1
- 31 + 0 = 1
- 41 + 1 = 0 carry 1
- 51 + 1 + 1 = 1 carry 1 (when a carry comes in)
Work from right to left, exactly as in denary addition, carrying into the next column.
- 10 1 0 1 1 0 1 0 (90)
- 2+ 0 0 1 1 0 1 1 0 (54)
- 3---------------
- 41 0 0 1 0 0 0 0 (144)
- 5carries: 1 1 1 1
- 1Write the addends11000000₂ = 192 · 01000000₂ = 64
- 2Add in columnsCarry leaves the MSB · Final
- 3Read the true sum1 00000000₂ = 256₁₀
- 4Apply register widthStored: 00000000 · Overflow flag: set
Overflow is the final carry beyond the fixed 8-bit boundary; it is not every carry.
With 8 bits the largest value that can be stored is , which is 255. Adding two 8-bit numbers whose total exceeds 255 produces a ninth bit, and there is nowhere to put it.
- 11 1 0 0 1 0 0 0 (200)
- 2+ 0 1 0 0 0 0 1 1 (67)
- 3---------------
- 41 0 0 0 0 1 0 1 1 (267 needs 9 bits)
- 5^
- 6carry out of the 8-bit
- 7The register keeps only: 00001011 = 11
- 8The answer is WRONG.
The consequence matters: the stored result is not merely inaccurate, it is completely wrong — becomes . This is why programs must check for overflow rather than trusting the result.
To state the cause in an exam: overflow occurs because the result requires more bits than the register has available, so the extra bit is lost.
This is not just an exam abstraction — fixed-width overflow has caused real, well-documented failures. The Ariane 5 rocket's guidance software famously failed seconds after launch in 1996 when a value too large for the 16-bit space allocated to it could not be converted, triggering a shutdown that destroyed the rocket. On a smaller scale, any fixed-width counter — a ticket-sale total, an old odometer — will silently wrap back towards zero once its maximum value is passed, unless the software explicitly checks for it. Choosing how many bits to give a register is really choosing the largest number the system can ever safely represent.
9.Logical Binary Shifts
A logical shift moves every bit a given number of places left or right. Bits shifted off the end are lost, and zeros are shifted in at the other end.
- 1Start: 0 0 0 1 0 1 1 0 = 22
- 2Logical shift LEFT by 1: 0 0 1 0 1 1 0 0 = 44 (x 2)
- 3Logical shift LEFT by 2: 0 1 0 1 1 0 0 0 = 88 (x 4)
- 4Start: 0 0 0 1 0 1 1 0 = 22
- 5Logical shift RIGHT by 1: 0 0 0 0 1 0 1 1 = 11 (/ 2)
- 6Logical shift RIGHT by 2: 0 0 0 0 0 1 0 1 = 5 (/ 4, remainder lost)
- A shift left by places multiplies the value by .
- A shift right by places divides the value by .
This is exactly why shifting is used: multiplying or dividing by a power of two is a single fast operation in hardware, much cheaper than a general multiply.
Two consequences to note. Shifting right loses any remainder — shifted right twice gives , not . And shifting left can push a 1 off the end, losing it entirely and giving a badly wrong answer. In the example above, one more left shift would drop the leading 1.
This is why shifts appear so often behind the scenes: when a compiler translates code that multiplies an integer variable by a power of two, it will often silently replace the multiplication with a shift, because a processor completes a shift in a single clock cycle while a general multiplication can take several. The programmer never sees this substitution — it is one of the simplest examples of a compiler making code faster without changing what it does.
- 1Start00110110₂ = 54 · 8-bit register
- 2Left shift one01101100₂ = 108 · zero fills on right
- 3Right shift one00011011₂ = 27 · zero fills on left
- 4Check lost bitsA bit leaving the MSB can change or overflow
Multiplication or division by 2 only works as expected when lost bits and truncation are accounted for.
10.Two's Complement
Two's complement allows both positive and negative integers to be stored in 8 bits. The key change is that the leftmost bit is worth instead of .
| Place value | -128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
| Positive number | 0 | 1 | 1 | 0 | 0 | 1 | 0 | 0 |
| Negative number | 1 | 0 | 0 | 1 | 1 | 1 | 0 | 0 |
For the positive value, . For the negative value, .
- If the leftmost bit is 0, the number is positive.
- If the leftmost bit is 1, the number is negative.
- The 8-bit range is to .
There are two standard methods, and both give the same answer:
- 1Find -100 in two's complement.
- 2METHOD 1 - flip and add one
- 3100 = 0 1 1 0 0 1 0 0
- 4flip all bits: 1 0 0 1 1 0 1 1
- 5add 1: 1 0 0 1 1 1 0 0 = -100
- 6METHOD 2 - place values
- 7Need -100, and the sign bit gives -128
- 8-128 + ? = -100, so ? = 28
- 928 = 16 + 8 + 4
- 101 0 0 1 1 1 0 0 = -100
Method 1 is quicker and works in either direction — applying it to a negative number gives back the positive. Method 2 is a useful check, and makes clear why the pattern is what it is.
Making the top bit worth looks arbitrary, but it has a valuable consequence: ordinary binary addition then works correctly on negative numbers, with no special handling.
Adding and in two's complement gives once the carry out is discarded — the right answer, from the same adder circuit used for positive numbers. That is why two's complement is used in practice rather than simply reserving a sign bit.
The logical shift from Section 9 always fills the vacated end with zero. That is fine for a positive or unsigned value, but it breaks a negative two's complement value: shifting () one place right logically gives , which is — the sign has flipped and the answer is nonsense.
An arithmetic shift fixes this. On a right shift, it copies the sign bit into each vacated position instead of using zero, so a negative number stays negative and is correctly divided by a power of two:
- 1Start: 1 1 1 1 0 0 0 0 = -16
- 2Arithmetic shift RIGHT by 1: 1 1 1 1 1 0 0 0 = -8 (sign bit copied in)
- 3Arithmetic shift RIGHT by 2: 1 1 1 1 1 1 0 0 = -4 (sign bit copied in twice)
An arithmetic left shift is identical to a logical left shift — zero is still shifted in at the right-hand end, since there is no sign bit to protect at that end. The distinction only ever matters for a right shift.
So the rule is: use a logical shift on an unsigned value, and an arithmetic shift on a two's complement (signed) value. Applying a logical shift to a negative number by mistake is a common source of error.
The most obvious way to store a negative number is to keep the leftmost bit purely as a plus/minus sign (0 for positive, 1 for negative) and store the number's size in the remaining seven bits, exactly as in ordinary written arithmetic. This is called sign-and-magnitude.
| Value | Sign-and-magnitude | Two's complement |
|---|---|---|
Positive values look the same under both schemes, but the two disagree completely on negative ones. Sign-and-magnitude looks simpler, but it has two serious weaknesses that explain why two's complement is used instead:
- Two representations of zero. is and is — a wasted bit pattern, and a program that checks
value == 0must handle both. - Ordinary addition does not work. A normal binary adder cannot simply add two sign-and-magnitude patterns; it would need extra circuitry to compare the signs and subtract the smaller magnitude from the larger when they differ. Two's complement needs none of this — the same adder that adds two positive numbers also gives the correct answer when one or both are negative.
11.Exam-Style Worked Examples
Question. (a) Convert the denary number to 8-bit binary. (b) Convert your answer to hexadecimal. (c) Convert the hexadecimal value to denary.
- 1(a) fits, remainder ; fits, remainder ; and do not; fits, remainder ; fits, remainder ; fits, remainder . So .
- 2(b) Split into fours from the right: . and , giving .
- 3(c) : the is in the sixteens column, so ; .
- 4.
Marking. 1 mark per point. Give binary answers as 8 bits, and remember that F is 15, not 16.
Question. Add the 8-bit binary integers and . (a) Show the addition. (b) State the denary values of both numbers and of the result. (c) State whether overflow occurs and explain.
- 1(a) — a 9-bit result.
- 2(b) and ; .
- 3(c) Overflow does occur.
- 4The result is greater than , the largest value storable in 8 bits, so it requires more bits than the register has available and the extra bit is lost.
Marking. 1 mark per point. The explanation must refer to the result needing more bits than are available — 'the number is too big' alone is weak.
Question. An 8-bit register contains . (a) State its denary value. (b) Perform a logical shift left of 2 places and give the new denary value. (c) State the effect of this shift on the value. (d) State one problem that can occur with a left shift.
- 1(a) .
- 2(b) Shifting left 2 places gives , which is .
- 3(c) The value has been multiplied by 4, since a left shift of places multiplies by .
- 4(d) Bits shifted off the left-hand end are lost, so if a 1 is pushed out the result is badly wrong.
Marking. 1 mark per point. Zeros are shifted in at the vacated end — a shift never wraps around.
Question. (a) Give the 8-bit two's complement representation of . (b) State the denary value of in two's complement. (c) State the range of an 8-bit two's complement number.
- 1(a) . Flip the bits: . Add 1: .
- 2(b) The leftmost bit is 1, so the number is negative. Using place values: .
- 3(c) to .
- 4The range is asymmetric because zero occupies one of the positive patterns, leaving one fewer positive value than negative.
Marking. 1 mark per point. Check (a) by adding and : the result is zero once the carry out is discarded.
12.Exam Tips & Common Misconceptions
- Learn the eight binary place values: .
- Give binary answers as 8 bits, padding with leading zeros.
- A–F are 10–15. F is 15, not 16.
- When splitting binary into hex, group in fours from the right.
- Justify hexadecimal by shorter and less error-prone for people, plus trivial conversion to binary.
- Explain overflow as the result needing more bits than the register has.
- A left shift of multiplies by ; a right shift divides and loses the remainder.
- In two's complement the leftmost bit is worth , and the range is to .
- Check a two's complement conversion by adding it to the positive — the result should be zero.
- On a right shift, use a logical shift for unsigned values and an arithmetic shift (sign bit copied in) for two's complement values; left shifts are identical either way.
- Sign-and-magnitude just flags the sign and stores the size; two's complement is preferred because it has one representation of zero and works with ordinary addition.
- Common misconception: computers use binary because it is faster. They use it because components have two reliably distinguishable states.
- Common misconception: hexadecimal is what the computer stores. The computer stores binary; hex is a shorthand for people.
- Common misconception: F is 16. The digits run –, so F is 15.
- Common misconception: binary is grouped into fours from the left. Group from the right, padding the left with zeros.
- Common misconception: overflow just makes the answer slightly wrong. The lost bit makes it completely wrong — becomes .
- Common misconception: a logical shift wraps bits around. Bits shifted off the end are lost and zeros are shifted in.
- Common misconception: shifting right always halves the value exactly. Any remainder is lost.
- Common misconception: in two's complement the first bit is just a sign flag. It is a place value of , which is what makes normal addition work.
- Common misconception: the 8-bit range is to . It is to .
- Common misconception: a right shift always fills with zero. That is only true for a logical shift; an arithmetic shift copies the sign bit in so a negative value stays negative.
- Common misconception: sign-and-magnitude and two's complement are just two ways of writing the same pattern. They differ completely for negative values, and sign-and-magnitude has two patterns for zero.
13.Summary
- Computers use binary because components have two reliably distinguishable states; all data is represented with 1s and 0s. A bit is one digit; a byte is eight.
- Denary is base 10, binary base 2, hexadecimal base 16 using digits – then A–F for –.
- Binary to denary: add the place values where there is a 1. Denary to binary: work down from , subtracting as you go.
- Hex to denary: multiply each digit by its place value. Denary to hex: divide by 16, quotient then remainder.
- Hex to binary: each hex digit is exactly four bits. Group from the right when converting back.
- Hexadecimal is used because it is shorter and less error-prone for people and converts to binary by direct substitution — used in MAC and IPv6 addresses, colour codes and memory dumps.
- Binary addition follows four rules, with carry .
- Overflow occurs when a result needs more bits than the register has; the extra bit is lost and the stored answer is wrong.
- A logical shift left of places multiplies by ; a shift right divides by and loses any remainder. Bits shifted off the end are lost; zeros are shifted in.
- In two's complement the leftmost bit is worth , so a leading 1 means negative and the range is to . Convert by flipping all bits and adding 1.
- An arithmetic shift behaves like a logical shift except on a right shift of a signed value, where it copies the sign bit in instead of zero, so a negative value stays negative.
- Sign-and-magnitude stores a sign flag plus a magnitude but has two patterns for zero and cannot use ordinary addition — which is why two's complement is used in practice.
- I can explain why computers use binary rather than denary.
- I can convert in both directions between denary and binary.
- I can convert in both directions between denary and hexadecimal.
- I can convert between hexadecimal and binary using the four-bit rule.
- I can justify the use of hexadecimal and give real examples.
- I can add two 8-bit binary integers and identify overflow with a reason.
- I can perform logical shifts and state their effect on the value.
- I can represent positive and negative numbers in two's complement and state the range.
- I can perform an arithmetic shift and explain how it differs from a logical shift.
- I can represent a value in sign-and-magnitude and explain why two's complement is preferred.