Binary magnitudes and number bases

AS · 11 min

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 nn bits you can make 2n2^n different patterns. That fact underlies almost every calculation in this topic.

Key result

With nn bits there are 2n2^n different patterns, so nn bits can represent unsigned integers from 00 to 2n−12^n - 1.

BitsPatternsUnsigned range
4160 to 15
82560 to 255
1665 5360 to 65 535
324 294 967 2960 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 210=10242^{10} = 1024. 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.

Definition

A kibibyte (KiB) is 210=10242^{10} = 1024 bytes. A kilobyte (kB) is 103=100010^{3} = 1000 bytes.

A mebibyte (MiB) is 2202^{20} bytes. A megabyte (MB) is 10610^{6} bytes.

A gibibyte (GiB) is 2302^{30} bytes. A gigabyte (GB) is 10910^{9} bytes.

A tebibyte (TiB) is 2402^{40} bytes. A terabyte (TB) is 101210^{12} bytes.

Key result
Decimal prefixValueBinary prefixValue
kilo (k)103=100010^{3} = 1000kibi (Ki)210=10242^{10} = 1024
mega (M)106=1 000 00010^{6} = 1\ 000\ 000mebi (Mi)220=1 048 5762^{20} = 1\ 048\ 576
giga (G)10910^{9}gibi (Gi)230=1 073 741 8242^{30} = 1\ 073\ 741\ 824
tera (T)101210^{12}tebi (Ti)240=1 099 511 627 7762^{40} = 1\ 099\ 511\ 627\ 776

Each binary prefix is 10241024 times the one before. Each decimal prefix is 10001000 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.

Watch out

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.

Comparing prefixes

A hard disk is advertised as 500 GB. Calculate its capacity in GiB, to two decimal places, and explain the difference.

Solution

500 GB=500×109500\ \text{GB} = 500 \times 10^{9} bytes.

500×109230=500 000 000 0001 073 741 824≈465.66 GiB\frac{500 \times 10^{9}}{2^{30}} = \frac{500\ 000\ 000\ 000}{1\ 073\ 741\ 824} \approx 465.66\ \text{GiB}

The number of bytes is the same. A gibibyte (2302^{30} bytes) is larger than a gigabyte (10910^{9} 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: 101121011_2, 1011101011_{10}, 1011161011_{16} are three different numbers. In Cambridge assembly language, B marks binary (B01001010), & marks hexadecimal (&4A) and # marks denary (#123).

Key result
DenaryBinaryHexDenaryBinaryHex
000000810008
100011910019
200102101010A
300113111011B
401004121100C
501015131101D
601106141110E
701117151111F

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 16=2416 = 2^4, 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.

1286432168421
10110110

128+32+16+4+2=182128 + 32 + 16 + 4 + 2 = 182.

Denary to binary

There are two reliable methods. Use whichever you make fewer mistakes with.

Denary to binary by subtracting place values
  1. Write the column headings 128, 64, 32, 16, 8, 4, 2, 1 (extend left if the number is 256 or more).
  2. Starting at the left, if the column value fits into what remains, write 1 and subtract it; otherwise write 0.
  3. Continue to the right until the remainder is 0, filling remaining columns with 0.
  4. Check by adding the columns back up.
Denary to binary by repeated division
  1. Divide the number by 2, writing down the quotient and the remainder (0 or 1).
  2. Repeat with the quotient until the quotient is 0.
  3. Read the remainders from the last to the first: that is the binary number.
  4. 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

Binary to hex
  1. Split the binary number into groups of four bits, starting from the right.
  2. Pad the leftmost group with zeros if it has fewer than four bits.
  3. 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.

Denary to 8-bit binary

Convert 17310173_{10} to an 8-bit binary number.

Solution

Subtract place values from the left:

1286432168421
10101101

173−128=45173 - 128 = 45; 64 does not fit; 45−32=1345 - 32 = 13; 16 does not fit; 13−8=513 - 8 = 5; 5−4=15 - 4 = 1; 2 does not fit; 1−1=01 - 1 = 0.

So 17310=101011012173_{10} = 10101101_2.

Check: 128+32+8+4+1=173128 + 32 + 8 + 4 + 1 = 173.

Binary to hex and denary

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, 128+32+16+4+2=182128 + 32 + 16 + 4 + 2 = 182, or use the hex digits, 11×16+6=176+6=18211 \times 16 + 6 = 176 + 6 = 182.

Both routes give 182, which is a useful self-check.

Hex with three digits

Convert 3E7163E7_{16} to denary and to binary.

Solution

Denary: the columns are 256, 16 and 1.

3×256+14×16+7×1=768+224+7=9993 \times 256 + 14 \times 16 + 7 \times 1 = 768 + 224 + 7 = 999

Binary: replace each hex digit by four bits: 3 is 0011, E is 1110, 7 is 0111.

3E716=0011 1110 011123E7_{16} = 0011\ 1110\ 0111_2, which is 1111100111 without the leading zeros.

Denary to hex by repeated division

Convert 2024102024_{10} to hexadecimal.

Solution
DivisionQuotientRemainder
2024÷162024 \div 161268
126÷16126 \div 16714 = E
7÷167 \div 1607

Reading the remainders from bottom to top gives 7E8.

Check: 7×256+14×16+8=1792+224+8=20247 \times 256 + 14 \times 16 + 8 = 1792 + 224 + 8 = 2024.

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.

Minimum number of bits
  1. For NN different values, find the smallest nn with 2n≥N2^n \ge N.
  2. For unsigned integers from 0 up to a maximum MM, there are M+1M + 1 values, so find the smallest nn with 2n≥M+12^n \ge M + 1, which is the same as 2n−1≥M2^n - 1 \ge M.
Bits for a code

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

28=2562^8 = 256, which is fewer than 300. 29=5122^9 = 512, which is at least 300.

So 9 bits are needed. 9 bits give 512 codes, so 512−300=212512 - 300 = 212 more students can be added before a tenth bit is needed.

Storage in binary prefixes

A video file is 3 MiB. Calculate its size in bytes and in kilobytes (kB).

Solution3 MiB=3×220=3×1 048 576=3 145 728 bytes3\ \text{MiB} = 3 \times 2^{20} = 3 \times 1\ 048\ 576 = 3\ 145\ 728\ \text{bytes}3 145 728÷1000=3145.728 kB3\ 145\ 728 \div 1000 = 3145.728\ \text{kB}

The question changes from a binary prefix to a decimal prefix, so the first step multiplies by 2202^{20} and the second divides by 10310^3.

Watch out

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. 3E7163E7_{16} 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 3E73E7, the E is a single digit worth 14 in the sixteens column, not "1" and "4".

Exam tip

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: 1 KiB=2101\ \text{KiB} = 2^{10} bytes, not 10310^3. A common lost mark is writing "1 kibibyte = 1000 bytes".

Summary
  • nn bits give 2n2^n patterns; unsigned range 00 to 2n−12^n - 1.
  • Decimal prefixes (kilo, mega, giga, tera) are powers of 10001000; binary prefixes (kibi, mebi, gibi, tebi) are powers of 1024=2101024 = 2^{10}.
  • 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 NN values: smallest nn with 2n≥N2^n \ge N.
  • Show working and give exactly the number of bits requested.

Practice

Question
  1. Convert 771077_{10} to 8-bit binary and to hexadecimal.
  2. Convert C016C0_{16} to denary and to 8-bit binary.
  3. Convert the binary number 1101011100101 to hexadecimal.
  4. State the largest denary number that can be stored as an unsigned integer in 16 bits.
  5. Explain the difference between 1 kilobyte and 1 kibibyte, giving the number of bytes in each.
  6. A memory card holds 16 GB. Calculate its capacity in GiB to one decimal place. Show your working.
  7. Convert 1000101000_{10} to hexadecimal, showing your working.
  8. 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.
  9. A programmer writes the value &2F in an assembly language program. A second programmer reads it as denary 2 followed by the letter F. Explain what &2F means and give its denary and binary values.
  10. A file of 256 MiB256\ \text{MiB} is to be split into pieces of 1 KiB1\ \text{KiB}. Calculate how many pieces there are, giving your answer as a power of 2 and as a denary number.
Answers
  1. 77=64+8+4+177 = 64 + 8 + 4 + 1, so 01001101. Nibbles 0100 1101 give 4D.
  2. C016=12×16+0=192C0_{16} = 12 \times 16 + 0 = 192. In binary 1100 0000.
  3. Group from the right: 1 1010 1110 0101 becomes 0001 1010 1110 0101, which is 1AE5.
  4. 216−1=65 5352^{16} - 1 = 65\ 535.
  5. A kilobyte uses the decimal prefix: 103=100010^3 = 1000 bytes. A kibibyte uses the binary prefix: 210=10242^{10} = 1024 bytes. A kibibyte is 24 bytes larger.
  6. 16×109÷230=16 000 000 000÷1 073 741 824≈14.9 GiB16 \times 10^9 \div 2^{30} = 16\ 000\ 000\ 000 \div 1\ 073\ 741\ 824 \approx 14.9\ \text{GiB}.
  7. 1000÷16=621000 \div 16 = 62 r 88; 62÷16=362 \div 16 = 3 r 1414 (E); 3÷16=03 \div 16 = 0 r 33. Reading upwards: 3E8. Check: 3×256+14×16+8=768+224+8=10003 \times 256 + 14 \times 16 + 8 = 768 + 224 + 8 = 1000.
  8. There are 1001 different values. 29=512<10012^9 = 512 < 1001 and 210=1024≥10012^{10} = 1024 \ge 1001, so 10 bits. The largest value is 210−1=10232^{10} - 1 = 1023.
  9. & marks a hexadecimal number, so &2F is the hex value 2F: 2×16+15=472 \times 16 + 15 = 47 in denary, and 0010 1111 in binary.
  10. 256 MiB=28×220=228256\ \text{MiB} = 2^8 \times 2^{20} = 2^{28} bytes. 1 KiB=2101\ \text{KiB} = 2^{10} bytes. Number of pieces =228÷210=218=262 144= 2^{28} \div 2^{10} = 2^{18} = 262\ 144.

How well do you know this?

Where this leads

Console

Search notes, courses and tools, or run an action