ICS3U Computer Science • Ontario Grade 11

Revision sheet: binary, hexadecimal, ASCII and Unicode (ICS3U)

This sheet is not a summary of the data representation chapter of ICS3U, Introduction to Computer Science, Grade 11: you already have your notes.

What the sheet covers

It answers one question only, what makes students lose marks on binary, hexadecimal, ASCII, Unicode and storage units, and which precise gesture avoids each loss.

Most of these mistakes come from reading a pattern with the wrong code or a size with the wrong unit: the arithmetic is right, the answer is not. The code is in Python 3, and every value on this sheet comes from a program that was run.

  • 8 mistakes that cost marks
  • 4 figures
  • 10 min reading
Do the chapter's exercises

The thread of the chapter

A computer stores only bits: the same pattern is a number, a character or a colour according to the code that reads it, and every positional code is read by weights, powers of 2 or of 16, counted from the right.

Topics covered

  • Binary number system
  • Hexadecimal number system
  • Positional notation
  • Bit and byte
  • Repeated division conversion
  • ASCII character code
  • Unicode code point
  • UTF-8 encoding
  • RGB colour code
  • SI and binary prefixes
  • Data transfer rate

The essentials

Weights counted from the right

  • • In binary, the rightmost digit has the weight 1 = 2 ** 0, and each place to the left doubles the weight: 1, 2, 4, 8, 16, 32, 64, 128 for a byte.
  • • A binary number is the sum of the weights above its 1s: 1011 is 8 + 2 + 1 = 11.
  • • In hexadecimal, the weights are 1, 16, 256, 4096, and the digits run from 0 to F, with A = 10 up to F = 15: B5 is 11 × 16 + 5 = 181.
  • • n bits give 2 ** n values, from 0 to 2 ** n - 1: a byte holds 256 values, 0 to 255.
  • • The number of bits for N values is the smallest n with 2 ** n ≥ N: 100 values need 7 bits, since 2 ** 6 = 64 is too few and 2 ** 7 = 128 is enough.
n0123456789102 ** n12481632641282565121024Powers of 2: the weights of binary, from the right
The weights of binary from the right: 2 ** 7 = 128 is the weight of the eighth bit, and 2 ** 10 = 1024, the kibi of storage units.

Write the weights above the digits before adding anything: it takes five seconds and prevents the first trap below.

A code gives the pattern its meaning

  • • The same byte 0100 1011 is the number 75, the hexadecimal 4B, the character 'K' in ASCII, or a component of a colour: the bits do not say which, the program does.
  • • ASCII gives the codes 0 to 127 to the unaccented letters, the digits, the punctuation and some control characters: the digits '0' to '9' are 48 to 57, 'A' to 'Z' are 65 to 90, 'a' to 'z' are 97 to 122.
  • • Unicode numbers every character of every writing system with a code point, written U+ and hexadecimal; its first 128 code points are ASCII.
  • • In Python, ord(c) gives back the code of the character c, and chr(n) the character of code n: ord('K') is 75, chr(107) is 'k'.
  • • UTF-8 stores one code point on 1 to 4 bytes: 1 byte below 128, 2 bytes up to 2047, 3 bytes up to 65535, 4 bytes beyond.
StoredCodeBytespace320010 0000'0'480011 0000'7'550011 0111the number 770000 0111'K'750100 1011'k'1070110 1011A character is stored as its code
The character '7' is stored as 55, 0011 0111, not as 0000 0111; 'K' and 'k' differ by 32, a single bit.

Before using a value, say what it is: a number, a character, or the code of a character. '7', 55 and 7 are three different answers.

The rules in table form

Each row reads from left to right: the assumptions, then the result. A red cell is not an answer, it is the finding that the form settles nothing and the instruction to rewrite it. Every case is followed by a worked example.

Storage and speed units

SI prefixes count in powers of 1000, binary prefixes in powers of 1024. Every example was computed in Python 3.

UnitMeansBytes or bits
bit, byte (B) one binary digit; 8 bits 1 B = 8 bits

Example: A 3-byte colour takes 24 bits.

kB, MB, GB, TB kilo, mega, giga, tera 1000, 1000 ** 2, 1000 ** 3, 1000 ** 4 bytes

Example: A 256 GB phone holds 256000000000 bytes.

KiB, MiB, GiB, TiB kibi, mebi, gibi, tebi 1024, 1024 ** 2, 1024 ** 3, 1024 ** 4 bytes

Example: The same 256000000000 bytes are 238.42 GiB.

Mbps megabits per second 1000000 bits per second

Example: A 50 Mbps plan carries at most 50 / 8 = 6.25 MB per second.

1 KB = 1024 bytes and 1 GB = 1000 MB in one calculation two systems mixed no single answer units mixed

Example: 256 GB counted as 256 × 1024 ** 3 bytes gives 274877906944, 18.8 GB too many.

What to do: Convert everything to bytes with ONE system, the one named by the unit, then convert the result.

A size divided by a speed gives a time once both are in bits or both in bytes: multiply the bytes by 8 first.

The mistakes that cost marks

These are the errors I correct most often in session. Each one costs marks on a paper, even when the reasoning behind it is right.

1. Converting binary to decimal

the whole conversion, and every later question that uses the value

What not to write

“1011 is 1 + 0 + 4 + 8 = 13: the first digit has the weight 1.”

What to write

“Weights from the right: 8, 4, 2, 1 above 1, 0, 1, 1, so 1011 = 8 + 2 + 1 = 11.”

Why: A number is written with its largest weight on the left, in binary as in decimal: the 1 of 1011 is worth 8, as the 1 of 1011 in decimal is worth 1000.

2. Reading the remainders of repeated division

1 to 2 marks, and a wrong answer that looks right: 10101101 is 173

What not to write

“181: the remainders are 1, 0, 1, 0, 1, 1, 0, 1, so 181 is 10101101.”

What to write

“The remainders are read upward, from the last to the first: 181 is 10110101. Check: 128 + 32 + 16 + 4 + 1 = 181.”

nn // 2remainder1819019045045221221101151521210101181 divided by 2 until the quotient is 0read upward: 10110101
The remainders of 181, read from the bottom up, give 10110101; read downward, they give 10101101, which is 173.

Why: The first remainder is the bit of weight 1, the rightmost bit. In a program, the new bit goes in front: bits = str(n % 2) + bits.

3. Going between binary and hexadecimal

1 mark per digit, and a value that is no longer the same number

What not to write

“1101011 grouped by four: 1101 and 011, that is D3.”

What to write

“Group by four from the RIGHT, adding 0s on the left: 0110 1011, that is 6B. One hexadecimal digit is four bits, a byte two digits.”

011010111101011from the right: 0110 1011 = 6Bfrom the left: 1101 011 gives D3, wrongGrouping 1101011 by four bits
Grouped from the right, 1101011 gives 0110 1011, that is 6B = 107; grouped from the left it gives D3 = 211, another number.

Why: Each hexadecimal digit replaces the four bits of the same weights, 1 to 8, 16 to 128, and those groups start at the right.

4. A byte written in hexadecimal

the whole code: with a digit missing, the app reads other components

What not to write

“The colour with red 255, green 0 and blue 10 is #FF0A.”

What to write

“Each component takes exactly two digits: 0 is 00 and 10 is 0A, so #FF000A.”

Why: In a colour code, each byte takes two hexadecimal digits; a leading 0 keeps the pairs in their places. format(10, '02X') gives back '0A'.

5. Using a digit read as text

every sum that uses the digits, by 48 per digit

What not to write

“d = '7', then total = total + ord(d) adds 7.”

What to write

“ord('7') is 55, the code of the character. Its value is ord(d) - ord('0'), or int(d): 55 - 48 = 7.”

Why: A character is stored as its code, and the codes of '0' to '9' are 48 to 57. In Java a char is a number, and ch - '0' gives the digit.

6. Reading the capacity of a drive

a written answer marked wrong on a units question

What not to write

“My 256 GB phone shows 238.42: 17.58 GB are missing.”

What to write

“256 GB = 256000000000 bytes = 238.42 GiB: the same bytes in a larger unit. Nothing is missing.”

Why: A gibibyte is 1024 ** 3 bytes, about 7.4 % more than a gigabyte, so the same capacity is a smaller number of GiB.

7. A speed in Mbps

the answer, eight times too small, and its unit marks

What not to write

“At 50 Mbps, a 1 GB file takes 1000 / 50 = 20 seconds.”

What to write

“50 Mbps is 50 / 8 = 6.25 MB/s, so 1000 / 6.25 = 160 s, at best. Or: 1 GB = 8000 megabits, 8000 / 50 = 160 s.”

Why: The lower-case b of Mbps is a bit, the capital B of MB a byte, and a byte is 8 bits.

8. The size of a text

1 mark on a size question, and a buffer too small in a program

What not to write

“The word naïve has 5 characters, so it takes 5 bytes.”

What to write

“In UTF-8, ï has the code point 239, from 128 to 2047, and takes 2 bytes: naïve takes 6 bytes. One byte per character holds for ASCII only.”

Why: len counts characters; UTF-8 gives a character 1 to 4 bytes according to its code point.

Which method to choose

Which method for a representation question?

Look at what is given and what is asked.

  • If a binary or hexadecimal number, its value asked → write the weights from the right, then add digit × weight

    Example: B5 = 11 × 16 + 5 = 181

  • If a decimal number, binary or hexadecimal asked → divide by 2 or 16 until the quotient is 0, then read the remainders upward

    Example: 181 // 16 = 11 and 181 % 16 = 5, so B5

  • If binary to hexadecimal, or back → four bits per digit, grouped from the right

    Example: 1011 0101 is B5

  • If a number of values, bits asked → the smallest n with 2 ** n ≥ the number of values

    Example: 100 values: 2 ** 7 = 128, so 7 bits

  • If a character, its code asked, or a code, its character → ord(c) and chr(n), or the table given

    Example: ord('K') is 75, chr(107) is 'k'

  • If a size and a speed, a time asked → both in bits or both in bytes, both with SI prefixes, then divide

    Example: 1 GB at 50 Mbps: 8000 / 50 = 160 s

A question that gives a table of codes or a rule for UTF-8 expects you to use it: the values are not to be known by heart.

How the answer is expected to be written

A marker ticks steps. Here they are in order, with the concluding sentence expected word for word.

Converting a number on a unit test

When to use it: « Convert 181 to binary and hexadecimal » or « What is 10110101 in decimal? »

  1. 1 Name the method: repeated division, or weights from the right.
  2. 2 Write the divisions, one per line, with the quotient and the remainder.
  3. 3 Read the remainders from the last to the first, and write the result with its base.
  4. 4 For hexadecimal, group the bits by four from the right, or divide by 16.
  5. 5 Check by converting back with the weights.

Concluding sentence

“181 = 128 + 32 + 16 + 4 + 1, so 181 is 10110101 in binary and B5 in hexadecimal, since 11 × 16 + 5 = 181.”

The trap: Giving the remainders in their order of arrival, which writes the number backwards.

Marking: A common split: 1 mark for the method, 1 for the result, 1 for the check.

Computing a size or a transfer time

When to use it: « How many bytes does the image take? » or « How long does the download take? »

  1. 1 List the quantities with their units: pixels, bytes per pixel, bits per second.
  2. 2 Convert to one unit: bytes, or bits, with one system of prefixes.
  3. 3 Compute, keeping the unit at each step.
  4. 4 Convert the result to the unit asked, and say which prefix it uses.

Concluding sentence

“1 GB = 8000 megabits; at 50 Mbps, the download takes 8000 / 50 = 160 s, that is 2 min 40 s, at best.”

The trap: Dividing megabytes by megabits per second.

Marking: A common split: 1 mark for the units, 1 for the computation, 1 for the answer with its unit.

Check before you hand in

Five minutes of checking recover more marks than one more problem started in a hurry.

  • Convert back

    A binary or hexadecimal answer is checked by its weights in five seconds.

    10110101 = 128 + 32 + 16 + 4 + 1 = 181: the conversion holds.

  • Odd or even

    The rightmost bit is 1 for an odd number and 0 for an even one.

    181 is odd, so its binary form ends with 1; an answer ending with 0 is wrong.

  • The count of digits

    A byte is 8 bits or 2 hexadecimal digits; the number of bits is four times the number of hexadecimal digits.

    A 6-digit colour code is 24 bits, 3 bytes.

  • The factor 8

    In a time computed from a speed, look for the 8: its absence makes the answer eight times too small.

    1 GB at 50 Mbps: 160 s with the 8, 20 s without it.

The typical problem, taken apart

Converting 181 to binary and hexadecimal

Convert 181 to binary by repeated division, then to hexadecimal, and check both results. Then write the program that does the binary conversion, and give what it prints.

Python
n = 181
bits = ''
while n > 0:
    bits = str(n % 2) + bits
    n = n // 2
print(bits, int(bits, 2), hex(181))

Step 1

181 // 2 = 90, remainder 1; 90 // 2 = 45, remainder 0; 45 // 2 = 22, remainder 1; 22 // 2 = 11, remainder 0; 11 // 2 = 5, remainder 1; 5 // 2 = 2, remainder 1; 2 // 2 = 1, remainder 0; 1 // 2 = 0, remainder 1.

Why

Each remainder is one bit, and dividing by 2 moves to the next weight. The loop stops when the quotient reaches 0.

Step 2

Read upward: 10110101.

Why

The first remainder is the bit of weight 1, so it is written last, on the right.

Step 3

Group by four from the right: 1011 and 0101, that is 11 and 5, written B and 5: B5.

Why

Four bits make one hexadecimal digit, with no further division.

Step 4

Check: 128 + 32 + 16 + 4 + 1 = 181, and 11 × 16 + 5 = 176 + 5 = 181.

Why

Converting back with the weights catches a remainder read in the wrong order.

Step 5

The program prints 10110101 181 0xb5: int(bits, 2) reads the string back in base 2, and hex() writes 0x then the digits in lower case.

Why

The built-in subprograms check a hand conversion; on a unit test, the algorithm is what is asked.

The conclusion, written out

“181 is 10110101 in binary and B5 in hexadecimal.”

The classic mistake on this problem: Writing the remainders in their order of arrival, 10101101, which is 173.

Learn by heart

  • • Weights from the right: 1, 2, 4, 8, 16, 32, 64, 128; in hexadecimal 1, 16, 256.
  • • Repeated division: read the remainders upward.
  • • n bits: 2 ** n values, from 0 to 2 ** n - 1.
  • • One hexadecimal digit = 4 bits; one byte = 2 hexadecimal digits.
  • • ord('7') is 55; the value of a digit is ord(d) - ord('0').
  • • ASCII: 0 to 127; UTF-8: 1 to 4 bytes per character.
  • • kilo = 1000, kibi = 1024: GB and GiB count the same bytes.
  • • Mbps counts bits: divide by 8 for MB/s.

Frequently asked questions

How do you convert a decimal number to binary by hand?

Divide the number by 2 and write the remainder, then divide the quotient by 2 again, until the quotient is 0. The remainders, read from the last one to the first one, are the binary digits. For 181 the remainders are 1, 0, 1, 0, 1, 1, 0, 1, and read upward they give 10110101. Check by adding the weights of the 1s.

Why does one hexadecimal digit equal four bits?

Because a hexadecimal digit takes one of sixteen values, and sixteen is two to the power four: four bits give sixteen patterns, from 0000 to 1111. So each hexadecimal digit replaces a group of four bits, counted from the right, and a byte of eight bits is written with two hexadecimal digits.

Why does my 1 TB drive show only 931 GB on my computer?

Because the drive maker counts with the SI prefixes, where one terabyte is a thousand to the power four bytes, while many operating systems count in powers of 1024 and display gibibytes. The same trillion bytes are about 931 gibibytes. No space is missing: the unit is larger, so the number is smaller.

What is the difference between the character 7 and the number 7 in Python?

The character is text, stored as its code: ord of the character 7 gives 55. The number 7 is a value you can compute with. Adding the character to a total adds 55, and joining two digit characters with plus gives a two-character string. Convert first, with int or with ord minus ord of the character 0.

Is Mbps the same as MB per second?

No. Mbps means megabits per second, and MB per second means megabytes per second. One byte is eight bits, so a speed in megabits per second is eight times the same speed in megabytes per second: a 50 Mbps plan downloads at most 6.25 megabytes each second.

Mark this sheet as read or add it to your favourites: a free account, no password, keeps your read sheets and favourites from one visit to the next, tells you which chapter to tackle next and lets you ask for the method or exercise you are missing. Create your space, an email is enough.

Practise it

Corrected exercises: Binary, hexadecimal, ASCII and Unicode, ICS3U

A method is proved on a paper, not on a sheet. The set for the same chapter takes each of these traps into a problem, with the solution written out step by step.

  • 12 corrected exercises
  • 120 points
  • 150 minutes
Do the exercises
Something missing from this sheet? A method, a trap, an exercise to practise it, a mistake you spotted: tell me, I read every request.

Sending goes through your free space: that is how I can answer you and tell you when the exercise is added. An email is enough, no password. Create your space. Rather not create an account? Write to me directly.

What is it about?
I read every request myself, nobody else sees it. No need to write your name or your school.
When your request is dealt with, you hear about it by email and in your space.

By sending, you agree that I read your request to answer it. It is kept with your account, erased with it, and never published: privacy policy.

© Ahmed Squalli Houssaini. Revision sheet published at www.letuteurscientifique.ca/en/fiches/ics3u-binary-hexadecimal-data-representation. Free for personal and classroom use; republishing it elsewhere requires written permission (legal notice).

Preparing for an ICS3U unit test?

I tutor Grade 11 computer science in English or in French, online. Get in touch for a first session.

Site by Studio Squalli