Binary magnitudes and number bases
Every piece of data inside a computer, whether a number, a letter, a pixel or a sample of sound, is stored as a pattern of bits. This note covers how those bit patterns are counted (binary and decimal prefixes) and how whole numbers are written in denary, binary and hexadecimal, together with the conversions between them. Paper 1 almost always opens with a short conversion question, and the same skills reappear in assembly language, bit manipulation, IP addresses and colour codes, so they need to be fast and error-free without a calculator.
Bits, nibbles and bytes
A bit (binary digit) is the smallest unit of data: a single 0 or 1. Physically it might be a high or low voltage, a magnetised or unmagnetised region of a disk, or a charged or uncharged cell of flash memory. Computers use two states because two states are easy to make reliably and to tell apart, even when the signal is noisy.
Bits are grouped:
- a nibble is 4 bits;
- a byte is 8 bits, the smallest unit most memory is addressed in;
- a word is the number of bits a processor handles as one unit (often 32 or 64), which varies between processors.
With bits you can make different patterns. That fact underlies almost every calculation in this topic.
With bits there are different patterns, so bits can represent unsigned integers from to .
| Bits | Patterns | Unsigned range |
|---|---|---|
| 4 | 16 | 0 to 15 |
| 8 | 256 | 0 to 255 |
| 16 | 65 536 | 0 to 65 535 |
| 32 | 4 294 967 296 | 0 to 4 294 967 295 |
Binary prefixes and decimal prefixes
When we measure storage we need words for large numbers of bytes. There are two systems, and the syllabus expects you to know both and to use them correctly.
Decimal prefixes (kilo, mega, giga, tera) are the ordinary SI prefixes and go up in powers of 1000. They are used by storage manufacturers, network speeds and most data sheets.
Binary prefixes (kibi, mebi, gibi, tebi) go up in powers of . They were introduced by the IEC because memory sizes are naturally powers of two, and using "kilo" to mean 1024 had caused decades of confusion.
A kibibyte (KiB) is bytes. A kilobyte (kB) is bytes.
A mebibyte (MiB) is bytes. A megabyte (MB) is bytes.
A gibibyte (GiB) is bytes. A gigabyte (GB) is bytes.
A tebibyte (TiB) is bytes. A terabyte (TB) is bytes.
| Decimal prefix | Value | Binary prefix | Value |
|---|---|---|---|
| kilo (k) | kibi (Ki) | ||
| mega (M) | mebi (Mi) | ||
| giga (G) | gibi (Gi) | ||
| tera (T) | tebi (Ti) |
Each binary prefix is times the one before. Each decimal prefix is times the one before.
The difference grows with size. A kibibyte is only 2.4% larger than a kilobyte, but a tebibyte is about 10% larger than a terabyte. That is why a drive sold as "500 GB" appears in an operating system that counts in binary units as about 465 GiB: nothing is missing, the same number of bytes is simply divided by a bigger unit.
Do not mix the two systems inside one calculation. If a question gives sizes in MiB, convert using 1024; if it gives MB, use 1000. If it gives bits and asks for bytes, divide by 8 first. Write the unit at every step so that you can see which system you are in.
A hard disk is advertised as 500 GB. Calculate its capacity in GiB, to two decimal places, and explain the difference.
Solution
bytes.
The number of bytes is the same. A gibibyte ( bytes) is larger than a gigabyte ( bytes), so fewer of them are needed to make up the same capacity.
Number bases
A number base (or radix) is the number of different digits a place-value system uses. Each column is worth the base times the column to its right.
- Denary (base 10) uses digits 0 to 9. Columns are worth 1, 10, 100, 1000, ...
- Binary (base 2) uses digits 0 and 1. Columns are worth 1, 2, 4, 8, 16, 32, 64, 128, ...
- Hexadecimal (base 16) uses digits 0 to 9 then A to F for ten to fifteen. Columns are worth 1, 16, 256, 4096, ...
When a number could be read in more than one base, write the base as a subscript: , , are three different numbers. In Cambridge assembly language, B marks binary (B01001010), & marks hexadecimal (&4A) and # marks denary (#123).
| Denary | Binary | Hex | Denary | Binary | Hex |
|---|---|---|---|---|---|
| 0 | 0000 | 0 | 8 | 1000 | 8 |
| 1 | 0001 | 1 | 9 | 1001 | 9 |
| 2 | 0010 | 2 | 10 | 1010 | A |
| 3 | 0011 | 3 | 11 | 1011 | B |
| 4 | 0100 | 4 | 12 | 1100 | C |
| 5 | 0101 | 5 | 13 | 1101 | D |
| 6 | 0110 | 6 | 14 | 1110 | E |
| 7 | 0111 | 7 | 15 | 1111 | F |
Learn this table. Every binary-hex conversion is just a lookup of four bits at a time.
Why hexadecimal exists
Hexadecimal is not used by the hardware: the hardware only ever stores binary. Hex is a shorthand for humans. Because , every hex digit corresponds to exactly four bits, so a byte is always exactly two hex digits. 11010110 is hard to read and easy to miscopy; D6 is not. The applications of hex (memory dumps, colour codes, MAC addresses, error codes and assembly language) are covered in BCD and hexadecimal in practice.
Converting between bases
Binary to denary
Write the column values above the bits and add up the columns that contain a 1.
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|
| 1 | 0 | 1 | 1 | 0 | 1 | 1 | 0 |
.
Denary to binary
There are two reliable methods. Use whichever you make fewer mistakes with.
- Write the column headings 128, 64, 32, 16, 8, 4, 2, 1 (extend left if the number is 256 or more).
- Starting at the left, if the column value fits into what remains, write 1 and subtract it; otherwise write 0.
- Continue to the right until the remainder is 0, filling remaining columns with 0.
- Check by adding the columns back up.
- Divide the number by 2, writing down the quotient and the remainder (0 or 1).
- Repeat with the quotient until the quotient is 0.
- Read the remainders from the last to the first: that is the binary number.
- Pad with leading zeros to the required number of bits.
The same repeated-division method works for any base: divide by 16 to convert denary to hex.
Binary to hexadecimal and back
- Split the binary number into groups of four bits, starting from the right.
- Pad the leftmost group with zeros if it has fewer than four bits.
- Replace each group by its hex digit.
For hex to binary, do the reverse: replace each hex digit by its four-bit pattern, keeping leading zeros inside the number (3 is 0011, not 11).
Hexadecimal to denary and back
Hex to denary: multiply each digit by its column value (1, 16, 256, 4096) and add. Denary to hex: either divide repeatedly by 16, or convert to binary first and then group into fours. Going via binary is often safer without a calculator because it only uses the four-bit table.
Convert to an 8-bit binary number.
Solution
Subtract place values from the left:
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|
| 1 | 0 | 1 | 0 | 1 | 1 | 0 | 1 |
; 64 does not fit; ; 16 does not fit; ; ; 2 does not fit; .
So .
Check: .
Convert the binary number 10110110 to hexadecimal and to denary.
Solution
Hex: split into nibbles 1011 0110. 1011 is B, 0110 is 6, so the hex value is B6.
Denary: either add the binary columns, , or use the hex digits, .
Both routes give 182, which is a useful self-check.
Convert to denary and to binary.
Solution
Denary: the columns are 256, 16 and 1.
Binary: replace each hex digit by four bits: 3 is 0011, E is 1110, 7 is 0111.
, which is 1111100111 without the leading zeros.
Convert to hexadecimal.
Solution
| Division | Quotient | Remainder |
|---|---|---|
| 126 | 8 | |
| 7 | 14 = E | |
| 0 | 7 |
Reading the remainders from bottom to top gives 7E8.
Check: .
How many bits are needed?
A frequent short question asks for the minimum number of bits to represent a given number of different values, or to store a given maximum value.
- For different values, find the smallest with .
- For unsigned integers from 0 up to a maximum , there are values, so find the smallest with , which is the same as .
A school gives every student a unique code. There are 300 students. Calculate the minimum number of bits needed for each code, and state how many more students could be added before another bit is needed.
Solution
, which is fewer than 300. , which is at least 300.
So 9 bits are needed. 9 bits give 512 codes, so more students can be added before a tenth bit is needed.
A video file is 3 MiB. Calculate its size in bytes and in kilobytes (kB).
Solution
The question changes from a binary prefix to a decimal prefix, so the first step multiplies by and the second divides by .
Reading remainders the wrong way. With repeated division the first remainder is the least significant digit. Writing the remainders top-to-bottom reverses the number. Always check by converting back.
Dropping zeros inside a hex conversion. is 0011 1110 0111; writing 11 1110 111 loses a bit in the middle group and gives the wrong value. Only leading zeros at the very left may be dropped.
Treating hex letters as separate numbers. In , the E is a single digit worth 14 in the sixteens column, not "1" and "4".
Calculators are not allowed in any 9618 paper, so practise these conversions by hand until they are automatic. Questions use command words like convert, show your working and state. When asked to show working, write the place-value headings or the division table: the method mark is for visible working, not just the answer.
When a question says "8-bit binary", give exactly 8 bits, including leading zeros: 00101101, not 101101. When asked for hexadecimal, give the digits without a prefix unless the question shows one.
For prefix questions, examiners expect the correct symbol (KiB versus kB) and the correct power: bytes, not . A common lost mark is writing "1 kibibyte = 1000 bytes".
- bits give patterns; unsigned range to .
- Decimal prefixes (kilo, mega, giga, tera) are powers of ; binary prefixes (kibi, mebi, gibi, tebi) are powers of .
- Binary columns double; hex columns multiply by 16; hex digits A to F mean 10 to 15.
- One hex digit is exactly four bits, so binary-hex conversion is done a nibble at a time from the right.
- Denary to any base: repeated division, reading remainders from last to first; or subtract place values.
- Minimum bits for values: smallest with .
- Show working and give exactly the number of bits requested.
Practice
- Convert to 8-bit binary and to hexadecimal.
- Convert to denary and to 8-bit binary.
- Convert the binary number
1101011100101to hexadecimal. - State the largest denary number that can be stored as an unsigned integer in 16 bits.
- Explain the difference between 1 kilobyte and 1 kibibyte, giving the number of bytes in each.
- A memory card holds 16 GB. Calculate its capacity in GiB to one decimal place. Show your working.
- Convert to hexadecimal, showing your working.
- A sensor produces readings from 0 to 1000 inclusive. Calculate the minimum number of bits needed to store one reading, and the largest value that number of bits could store.
- A programmer writes the value
&2Fin an assembly language program. A second programmer reads it as denary 2 followed by the letter F. Explain what&2Fmeans and give its denary and binary values. - A file of is to be split into pieces of . Calculate how many pieces there are, giving your answer as a power of 2 and as a denary number.
Answers
- , so
01001101. Nibbles0100 1101give 4D. - . In binary
1100 0000. - Group from the right:
1 1010 1110 0101becomes0001 1010 1110 0101, which is 1AE5. - .
- A kilobyte uses the decimal prefix: bytes. A kibibyte uses the binary prefix: bytes. A kibibyte is 24 bytes larger.
- .
- r ; r (E); r . Reading upwards: 3E8. Check: .
- There are 1001 different values. and , so 10 bits. The largest value is .
&marks a hexadecimal number, so&2Fis the hex value 2F: in denary, and0010 1111in binary.- bytes. bytes. Number of pieces .