Number systems and binary arithmetic

IGCSE Computer Science · Data Representation

Why computers use binary

A computer is built from switches that are either on or off. Two states map naturally onto the two digits 0 and 1, so all data must be represented in binary.

Denary to binary

Write the place values for $8$ bits and fit the number into them, largest first:

$128$$64$$32$$16$$8$$4$$2$$1$

For $77$: take $64$ (leaving $13$), then $8$ (leaving $5$), then $4$ (leaving $1$), then $1$.

$$77_{10}=01001101_2$$

Check by adding the place values back: $64+8+4+1=77$ ✓. Always do this check — it catches almost every slip.

Hexadecimal

Hexadecimal is base 16, using the digits $0$–$9$ then $A$–$F$ for $10$–$15$.

Its value is that one hex digit represents exactly four bits, so a byte becomes just two characters:

$$01001101_2=0100\;1101=4\text{D}_{16}$$

Why it is used: hex is shorter and far easier for people to read and write than long strings of bits, so there are fewer mistakes. Computers still work entirely in binary — hex is only for humans.

Common uses: memory addresses, MAC addresses, colour codes in HTML, and error codes.

Binary addition and overflow

The rules: $0+0=0$; $0+1=1$; $1+1=10$ (write $0$, carry $1$); $1+1+1=11$ (write $1$, carry $1$).

Overflow happens when the result needs more bits than are available. Adding two $8$-bit numbers can produce a $9$-bit answer, but only $8$ bits can be stored — so the leftmost bit is lost and the stored answer is wrong.

$$10000000+10000000=1\underline{00000000}$$

The $1$ carried out of the eighth column has nowhere to go. Note the cause: not the arithmetic, but the fixed number of bits.

Logical shifts

A logical left shift moves every bit left; zeros come in on the right and bits falling off the left are lost. Each place shifted multiplies by 2.

$$00001010\;(10)\ \xrightarrow{\text{shift left 1}}\ 00010100\;(20)$$

A logical right shift divides by 2, and any $1$ shifted off the right end is lost — so information can be destroyed.

$$00001011\;(11)\ \xrightarrow{\text{shift right 1}}\ 00000101\;(5)$$

Note $11\div 2=5{,}5$ but the result is $5$: the remainder was lost, not rounded.

Overflow: the fault is not the addition, but the LIMITED NUMBER OF BITS 1 0 0 0 0 0 0 0 + 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 8 bits can store 9th bit This bit has NO PLACE TO GO in an 8-bit register → IT IS LOST Correct: 128 + 128 = 256 Machine stores: 00000000 = 0 ← WRONG result The same addition in a 16-bit register does NOT overflow. The cause is THE NUMBER OF BITS, not the calculation.
The same addition in a 16-bit register does not overflow. That is evidence that the cause lies in the limited number of bits and not in the calculation itself.

Worked example: converting between number systems and checking the result

(a) Convert $77_{10}$ to $8$-bit binary and then to hexadecimal. (b) Convert $\text{B3}_{16}$ to binary and then to decimal. Show how to check at each step.

Solution

(a) Convert $77$ to binary.

Write the place values: $128\ \ 64\ \ 32\ \ 16\ \ 8\ \ 4\ \ 2\ \ 1$.

$$77_{10}=01001101_2$$

Check: $64+8+4+1=77$ ✓

To hexadecimal. Split into two groups of four bits:

$$0100\;\big|\;1101$$

$0100_2 = 4$ and $1101_2 = 8+4+1 = 13 = \text{D}$.

$$77_{10}=4\text{D}_{16}$$

Check: $4\times 16 + 13 = 64+13=77$ ✓

(b) Convert $\text{B3}_{16}$ to binary.

Convert each digit into four bits — this is the strength of hexadecimal:

$\text{B}=11=1011_2$ and $3=0011_2$.

$$\text{B3}_{16}=10110011_2$$

To decimal. Add the place values that have a bit of $1$:

$$128+32+16+2+1=179$$

Check by a different route: $\text{B}\times 16 + 3 = 11\times 16 + 3 = 176+3=179$ ✓

Why you should always check. Converting between number systems is mechanical work, so it is very easy to miss a bit, and one wrong bit throws the result right off.

A check takes only a few seconds but catches almost every error — especially when you check by a different route from the one used to calculate, as in part (b).

Worked example: overflow and bit shifts

(a) Add $10011100_2$ and $11010101_2$ in an $8$-bit register. State what happens and explain the cause. (b) Logical shift $00001010_2$ left by one place, then logical shift $00001011_2$ right by one place. State the effect of each operation.

Solution

(a) Add the two numbers. Add from right to left:

$10011100 + 11010101$

Column $1$: $0+1=1$. Column $2$: $0+0=0$. Column $3$: $1+1=10$ → write $0$, carry $1$. Column $4$: $1+0+1(\text{carry})=10$ → write $0$, carry $1$. Column $5$: $1+1+1=11$ → write $1$, carry $1$. Column $6$: $0+0+1=1$. Column $7$: $0+1=1$. Column $8$: $1+1=10$ → write $0$, carry 1 out.

$$10011100+11010101=1\,01110001$$

What happens: OVERFLOW. The result needs $9$ bits but the register only has $8$ bits. The leftmost carry bit has nowhere to be stored so it is lost, and the value left is $01110001_2 = 113$ — wrong.

Check in decimal: $156 + 213 = 369$, but $8$ bits can only store up to $255$. Just as predicted.

The cause. It is not that the addition is wrong — it is completely correct. The problem is the register's fixed number of bits. The same calculation in a $16$-bit register would not overflow, and that is evidence of the real cause.

(b) Logical shift left by one place.

$$00001010\ \rightarrow\ 00010100$$

Every bit moves one place to the left, and $0$ is filled in on the right.

Effect: $10 \rightarrow 20$, that is, multiplying by $2$.

Logical shift right by one place.

$$00001011\ \rightarrow\ 00000101$$

Every bit moves one place to the right, $0$ is filled in on the left, and the rightmost $1$ bit falls off and is lost.

Effect: $11 \rightarrow 5$, that is, dividing by $2$.

An important detail. $11 \div 2 = 5.5$ but the result is $5$ — the remainder has been lost completely, not rounded.

This is why a right shift can destroy information: shifting right and then left again does not return the original number if the bit that fell off was $1$.

Common mistake

Explaining overflow with the phrase “the addition is wrong”. The binary addition is completely correct — the problem is that the result needs more bits than the register has. Adding two $8$-bit numbers can give a $9$-bit result, and the ninth bit has nowhere to be stored so it is thrown away, leaving a wrong value. The most concise evidence to state in an answer: the same calculation in a $16$-bit register does not overflow. If the cause were the addition, changing the number of bits would not have helped. The accompanying trap lies in the logical right shift: thinking that it rounds the result. Shifting $00001011$ ($11$) right gives $00000101$ ($5$), while $11\div 2 = 5.5$ — the remainder is lost completely, not rounded up or down. Consequence: shifting right and then left again does not return the original number if the bit that fell off was $1$, so shifts destroy information.

Key takeaway

Computers use binary because a switch has only two states. Conversions between number systems must always be checked by adding the place values. Hexadecimal exists because one digit corresponds to exactly four bits, so it is short and less error-prone — computers still run entirely in binary. Overflow is caused by the limited number of bits, not by a wrong addition. A left shift multiplies by $2$, a right shift divides by $2$ — and a right shift loses the remainder rather than rounding it.

All IGCSE Computer Science lessons · Practice IGCSE Computer Science questions (free, instant scoring) · Text, images, sound and compression →