Number systems — solved exercises with the channels of a colour, opacities in binary, a 12-bit colour and a practice quiz (and the 256 that did not fit in 8 bits)

In the first practice midterm I did for this topic there was one of those questions you answer in five seconds: «What is the minimum number of bits needed to represent the number 256?». I thought: «2⁸ = 256, so 8», wrote it down and moved on. When I marked it: 9. With 8 bits there are 256 combinations, yes, but they go from 0 to 255. 256 is exactly the first one that no longer fits: 100000000, a 1 followed by eight zeros. Since then, every time I see a power of 2 in a range question, I stop to think whether they are asking how many numbers or which is the largest.

These are the exercises for the second paper topic of IC1. You already have the number systems theory (bases, weights, conversions and ranges) and the practice with four problems solved step by step. Now it is your turn, with the colours of the photos as always: the channels of a colour, some opacities in binary and a 24-bit colour reduced to 12. After that, three worked answers with a hidden mistake, the full solutions, a practice quiz and the cheat sheet.

How to use this article

  1. Try each level on paper, without a calculator (there is none in the exam), with the method from the practice: data with their base, method, sums in order and check.
  2. If you get stuck, open the hint for that level. Just one.
  3. Before looking at the solutions, do «What’s wrong here?»: finding the mistake in someone else’s answer is exactly what helps you not to make it.
  4. Compare your working with the step-by-step solution, not just the final result, and finish with the quiz.

Level 1: the channels of a flower

In the same sunset photo there is a bougainvillea flower in the foreground. The eyedropper reads #C84B1F.

I ask you for:

  • a) The decimal value of each channel (red C8, green 4B, blue 1F).
  • b) Each channel in 8-bit binary.
  • c) The whole colour in octal.

Expected result: a) 200, 75 and 31; b) 11001000, 01001011 and 00011111; c) 62045437(8)62045437_{(8)}.

For a), each channel is d1⋅16+d0d_1 \cdot 16 + d_0, with the letters replaced by their value. For b) there is no need to go through decimal: each hex digit is 4 bits. For c), put the 24 bits together and cut them in 3s from the right.

Level 2: opacities in binary

The editor stores the opacity of each layer in binary with 5 fractional digits. For these three layers:

  • a) A shadow with opacity 0.8125.
  • b) A gradient with opacity 0.6.
  • c) A text layer with a font size of 13.6 points (this value is also stored with 5 fractional digits).

For each one, write the stored binary number, the decimal value it really represents and the error.

Expected result: a) 0.11010 = 0.8125, error 0; b) 0.10011 = 0.59375, error 0.00625; c) 1101.10011 = 13.59375, error 0.00625.

Before multiplying, ask yourself whether each fractional part is a sum of halves, quarters, eighths… One is and two are not. And in c), the integer part goes separately, with divisions.

Level 3 (challenge): a 12-bit colour

Some programs accept «short» colours of 3 hex digits, like #FA6: 4 bits per channel instead of 8. The screen shows them by repeating each digit: #FA6 is painted as #FFAA66.

I ask you for:

  • a) How many different colours can be written with 3 hex digits? And with 6?
  • b) Reduce the sky colour, #F4A261, to 3 digits by keeping the most significant digit of each channel.
  • c) What decimal value does each channel of the short colour have as the screen paints it, and how much has it changed from 244, 162 and 97?
  • d) Write the short colour in binary (12 bits) and in octal.

Expected result: a) 4096 and 16 777 216; b) #FA6; c) 255, 170 and 102 (+11, +8 and +5); d) 111110100110 and 7646(8)7646_{(8)}.

In c), FF, AA and 66 are converted to decimal like any two-digit hex number. Trick: repeating a hex digit d is the same as multiplying it by 17, because d⋅16+d=17dd \cdot 16 + d = 17d.

What’s wrong here?

Three answers from classmates, each with a mistake. Read the working, try to find the error before reading the explanation and think how you would fix it.

Snippet 1: the remainders in their order

A classmate converted the sky’s green, 162, to binary like this:

162 / 2 = 81  remainder 0
 81 / 2 = 40  remainder 1
 40 / 2 = 20  remainder 0
 20 / 2 = 10  remainder 0
 10 / 2 =  5  remainder 0
  5 / 2 =  2  remainder 1
  2 / 2 =  1  remainder 0
last quotient: 1

Result: 01000101

What’s wrong? The divisions are perfect, but the remainders are written from top to bottom with the last quotient at the end. 01000101 is 64 + 4 + 1 = 69, not 162. The first remainder is the least significant bit: you have to read from the last quotient upwards.

Reading the remainders in the order they come out. If you cannot remember which order it is, think of the first remainder: it says whether the number is even (0) or odd (1), so it is the units bit, the one on the right.

Fixed: last quotient and remainders from bottom to top: 10100010 = 128 + 32 + 2 = 162.

Snippet 2: the group left on the right

Another classmate had to convert 1011101(2)1011101_{(2)} to hexadecimal:

1011101 → groups of 4: 1011 | 101
          1011 = B,  101 = 5
Result: B5

What’s wrong? She grouped from the left, so the incomplete group (101) ended up on the right, in the units position. But in a number the zeros that do not change the value are the ones on the left, not on the right.

Why you group from the rightThe number 1011101 grouped wrongly, from the left, gives B5 (181), and correctly, from the right with a padding zero, gives 5D (93), which is its value.from the left1011B1015B5₁₆ = 181 (wrong)from the right010151101D5D₁₆ = 93 (right)1011101₂ = 64 + 16 + 8 + 4 + 1 = 93
The incomplete group has to end up on the left, where zeros are worth nothing.

Grouping from the left. B5 is 181, almost double the real value. Always group from the point (here, from the right) and pad the last group with zeros on the outside.

Fixed: from the right, 101 1101 → 0101 1101 = 5D. Check: 5⋅16+13=935 \cdot 16 + 13 = 93 and 1011101(2)=64+16+8+4+1=931011101_{(2)} = 64 + 16 + 8 + 4 + 1 = 93.

Snippet 3: ranges off by one

A third classmate answered two range questions like this:

a) Largest number with 3 hex digits: FFF = 16^3 = 4096
b) Minimum bits for the number 256: 2^8 = 256 → 8 bits

What’s wrong? Both have the same mistake: confusing the number of values (BmB^m) with the largest value (Bm−1B^m – 1). In a), 163=409616^3 = 4096 is how many numbers there are, from 000 to FFF; the largest, FFF, is 4095. In b), with 8 bits you reach 255, so 256 no longer fits.

Forgetting that 0 also counts. With m digits there are BmB^m numbers, but they start at 0 and end at Bm−1B^m – 1.

Fixed: a) FFF(16)=163−1=\mathrm{FFF}_{(16)} = 16^3 – 1 = 4095. b) 256=28=100000000(2)256 = 2^8 = 100000000_{(2)}: 9 bits.

Solutions

Level 1

Before any working, look at how the code is built: #C84B1F is read two digits at a time, C8 | 4B | 1F, and each pair is an 8-bit channel. Parts a) and b) are done channel by channel. Part c) is not: there the three channels become a single string of 24 bits.

a) Each channel to decimal (sum of weights with powers of 16, C = 12, B = 11, F = 15):

ChannelHexSumDecimal
RedC812⋅16+812 \cdot 16 + 8200
Green4B4⋅16+114 \cdot 16 + 1175
Blue1F1⋅16+151 \cdot 16 + 1531

Multiplying the wrong digit by 16: 8 × 16 + 12 = 140 instead of 200. The left-hand digit counts groups of 16, just as the 7 in 75 counts tens.

Lots of red, some green and hardly any blue: an intense orange-red, as a bougainvillea should be.

b) Each channel to binary. Each hex digit by its 4 bits, without going through decimal:

ChannelHexBinaryCheck
RedC81100 1000128 + 64 + 8 = 200
Green4B0100 101164 + 8 + 2 + 1 = 75
Blue1F0001 111116 + 8 + 4 + 2 + 1 = 31

What matters in this table are the leading zeros: the 4 in 4B is written 0100, not 100, and the 1 in 1F, 0001. For the value of a single channel they make no difference, but in c) the three channels are joined together and there position is everything.

Dropping those zeros. You are left with 20 bits instead of 24, everything shifts and the octal comes out as 3104577. If you do not get exactly 24 bits when you join the channels, a zero is missing somewhere.

c) The whole colour in octal. Put the 24 bits together and cut them in 3s from the right:

Bits110010000100101100011111
Octal62045437
C84B1F(16)=110010000100101100011111(2)=62045437(8) \mathrm{C84B1F}_{(16)} = 110010000100101100011111_{(2)} = 62045437_{(8)}

Why it is done this way: in b) going through decimal and dividing would work, but it is seven divisions per channel; groups of 4 bits are a table you already know. In c) you cannot convert channel by channel to octal (8 bits are not a multiple of 3): you have to put the 24 bits together first. That is why the octal of a colour cannot be read by channels, and why nobody writes colours in octal.

The tempting shortcut is to convert each channel to octal separately and stick the results together:

ChannelDecimalIn octalWhy it does not work
Red200310Its 8 bits do not split into groups of 3: it shares the third group with green.
Green75113The same happens to it on both sides.
Blue3137It gets the last two digits right by chance, but the group before them mixes one bit of green with two of blue.

Stuck together it gives 31011337, not 62045437. Since 24 is a multiple of 3, the whole string splits into exactly 8 groups, with no padding.

Level 2

The plan before multiplying: multiplying a binary number by 2 moves the point one place to the right, so the digit that crosses into the integer part is the next fractional digit. That is why on each round you write down the integer part and carry on only with what is after the point.

And before the first round you already know which one ends: 0.8125 = 13/16 has a power of 2 underneath and terminates; 0.6 = 3/5 has a 5 and never terminates, so the 5 digits are a cut-off and the error appears.

a) 0.8125. Multiplications by 2:

I multiplyResultDigit
0.8125 × 21.6251
0.625 × 21.251
0.25 × 20.50
0.5 × 21.01

0 is left in the fourth round: 0.1101 exact. With 5 digits it is completed with a 0 on the right: 0.11010, which is 0.5 + 0.25 + 0.0625 = 0.8125. Error: 0. It was the one that is a sum of halves, quarters and sixteenths.

b) 0.6.

I multiplyResultDigitCarry on with
0.6 × 21.210.2
0.2 × 20.400.4
0.4 × 20.800.8
0.8 × 21.610.6
0.6 × 21.210.2 (it repeats)

Digits from top to bottom: 0.10011. It is 0.5 + 0.0625 + 0.03125 = 0.59375. Error: 0.6 − 0.59375 = 0.00625, smaller than the weight of the last digit (2−5=0.031252^{-5} = 0.03125), as it should be.

Here truncating and rounding give the same thing: the sixth digit would be a 0 (0.2 × 2 = 0.4). And the last row is the check: you are back to «carry on with 0.2», as after the first round, and from there the block 0011 repeats for ever.

Carrying on multiplying the whole result. If on the second round you do 1.2 × 2 = 2.4, you get a «digit» 2, which does not exist in binary: you forgot to remove the integer part.

c) 13.6. The integer part with divisions: 13 / 2 = 6 remainder 1; 6 / 2 = 3 remainder 0; 3 / 2 = 1 remainder 1; quotient 1 → 1101. The fractional part is the same 0.6 as in b): 10011. Together: 1101.10011, which is 13 + 0.59375 = 13.59375. Error: 0.00625, the same as in b), because the integer part adds no error.

Putting the whole 13.6 into the multiplications (13.6 × 2 = 27.2) or into the divisions (13.6 / 2 = 6.8). Divisions for the left of the point, multiplications for the right: split them before you start and join them at the end. The integer part adds no error because any integer is written exactly in binary (13 = 8 + 4 + 1).

Why it is done this way: in c) part b) is reused. In the exam, if two numbers share a fractional part, reusing the working is not cheating: it saves time and mistakes.

Level 3

a) 3 hex digits are 12 bits: 212=163=2^{12} = 16^3 = 4096 colours. 6 digits are 24 bits: 224=2^{24} = 16 777 216. Half the bits do not give half the colours but the square root: every bit you remove divides by two.

Why you multiply and do not add: each hex digit can take 16 values and each value of one combines with all the values of the others, so it is 16 × 16 × 16. Adding (16 + 16 + 16 = 48) counts digit values, not colours. Check of the square root: 4096 × 4096 = 16 777 216.

b) Keep the most significant digit of each channel: F4 → F, A2 → A, 61 → 6. Short colour: #FA6. Dropping the right-hand digit is dropping the 4 least significant bits of each channel.

In bits you can see why it is «dropping the 4 least significant bits»:

ChannelHex8 bitsKeptShort digit
RedF41111 01001111F
GreenA21010 00101010A
Blue610110 000101106

c) The screen paints #FFAA66:

ChannelOriginalShort (painted)DecimalChange
RedF4 = 244FF15⋅17=25515 \cdot 17 = 255+11
GreenA2 = 162AA10⋅17=17010 \cdot 17 = 170+8
Blue61 = 97666⋅17=1026 \cdot 17 = 102+5
The colour in 12 bitsThe 24-bit colour F4A261 reduced to the 12-bit FA6, which the screen reads as FFAA66; each channel goes up a little: red by 11, green by 8 and blue by 5.#F4A26124 bits#FA612 bits (= #FFAA66)drop4 bitschannelbeforeaftererrorred244255+11green162170+8blue97102+52¹² = 4096 colours versus 2²⁴ = 16777216: it looks alike, but it is not the same
Fewer bits, fewer possible colours: the closest one is not always the same.

All three channels go up a little: the short colour is slightly lighter and more saturated than the original. It looks alike, but it is not the same.

Cutting does not always give the closest short colour. A short channel can only show values in steps of 17 (0, 17… 238, 255), and 244 is 6 away from EE = 238 and 11 away from FF = 255. The exercise asks you to cut because it is the quick way. And all three channels go up because the repeated digit is bigger than the one removed; the rise is exactly the difference: 15 − 4 = 11, 10 − 2 = 8 and 6 − 1 = 5.

d) Each hex digit by its 4 bits: F = 1111, A = 1010, 6 = 0110 → 111110100110. In 3s from the right: 111 110 100 110 = 7646(8)7646_{(8)}. Check via decimal: FA6(16)=15⋅256+10⋅16+6=4006\mathrm{FA6}_{(16)} = 15 \cdot 256 + 10 \cdot 16 + 6 = 4006 and 7646(8)=7⋅512+6⋅64+4⋅8+6=40067646_{(8)} = 7 \cdot 512 + 6 \cdot 64 + 4 \cdot 8 + 6 = 4006.

Why it is done this way: the trick of 17 in the hint (d⋅16+d=17dd \cdot 16 + d = 17d) saves four sums of weights and explains why the «round» greys are 00, 11, 22… FF: they are multiples of 17. And in d), 12 bits is a multiple of 3 and of 4, so no padding is needed.

Practice quiz

Twenty questions to go over the whole topic: weights, conversions in the four bases, fractional parts, groups of bits and ranges. It does not count for anything (the IC1 exam is written, not multiple choice): it is there so you can see what you have clear and what you do not. You can repeat it as many times as you like; each answer comes with its explanation. In the boxes, write binary numbers without spaces.

Question 1Single choice

What does it mean that the decimal system is positional?

  1. That the value of each digit depends on the position it occupies
  2. That the digits are always written in the same order
  3. That each number has a fixed position on the number line
  4. That only ten positions can be used
Solution

Answer: That the value of each digit depends on the position it occupies

In a positional system the number is a sum of digits multiplied by their weights, and the weight depends on the position: di×Bid_i \times B^i. Binary, octal and hexadecimal are positional too.

Question 2Single choice

In the binary number 0.01(2)0.01_{(2)}, what is the weight of the position holding the 1?

  1. 0.25
  2. 0.01
  3. 0.5
  4. 2
Solution

Answer: 0.25

After the point the weights are negative powers of the base. In binary: 2−1=0.52^{-1} = 0.5, 2−2=0.252^{-2} = 0.25, 2−3=0.1252^{-3} = 0.125… So 0.01(2)=0.25(10)0.01_{(2)} = 0.25_{(10)}.

Question 3Calculation

Convert each number to decimal (sum of weights). Write only the decimal number.

NumberBaseValue in decimal
11001002
1448
6416
Solution
NumberBaseValue in decimal
11001002100
1448100
6416100

All three are the same number. 1100100(2)=64+32+4=1001100100_{(2)} = 64 + 32 + 4 = 100; 144(8)=1×64+4×8+4=100144_{(8)} = 1 \times 64 + 4 \times 8 + 4 = 100; 64(16)=6×16+4=10064_{(16)} = 6 \times 16 + 4 = 100. That is why stating the base matters so much: «64» says nothing without it.

Question 4Short answer

Convert 162(10)162_{(10)} (the green channel of the colour) to 8-bit unsigned binary. Write the 8 bits together.

Solution

Answer: 10100010

Successive divisions by 2: remainders 0, 1, 0, 0, 0, 1, 0 and last quotient 1. Read from bottom to top: 10100010. Check: 128+32+2=162128 + 32 + 2 = 162.

Question 5True or false

True or false about base conversion:

  1. In successive divisions, the first remainder is the least significant bit.
  2. Decimal 0.1 can be written exactly in binary with enough bits.
  3. Decimal 0.75 is exactly 0.11 in binary.
  4. With m bits you can represent numbers up to 2m2^m.
Solution
  • T — In successive divisions, the first remainder is the least significant bit.
  • F — Decimal 0.1 can be written exactly in binary with enough bits.
  • T — Decimal 0.75 is exactly 0.11 in binary.
  • F — With m bits you can represent numbers up to 2m2^m.

The first remainder is the LSB, which is why they are read from bottom to top. 0.1 is periodic in binary (0.000110011…): it never ends, so there is always an error. 0.75=0.5+0.250.75 = 0.5 + 0.25, exact. With m bits the largest number is 2m−12^m – 1: with 8 bits, 255.

Question 6Order

Put in order the steps to convert a decimal integer to binary by successive divisions:

  1. Check the result with the sum of weights
  2. Write the remainders after it in reverse order, back to the first one
  3. Take the last quotient as the most significant bit
  4. Repeat the division with the quotient while it can still be divided
  5. Divide the number by 2 and write down the quotient and the remainder
Solution

Correct order:

  1. Divide the number by 2 and write down the quotient and the remainder
  2. Repeat the division with the quotient while it can still be divided
  3. Take the last quotient as the most significant bit
  4. Write the remainders after it in reverse order, back to the first one
  5. Check the result with the sum of weights

The reading order is the key: last quotient and then the remainders from bottom to top. Checking with the sum of weights takes seconds and catches the most common mistake (reading them the wrong way round).

Question 7Fill in the gaps

Fill in:

Each octal digit is a group of ____ bits, because 23=82^3 = 8, and each hexadecimal digit a group of ____ bits. To go from octal to hexadecimal you go through ____ first.

Words:

  • 4
  • binary
  • 2
  • decimal
  • 8
  • 3
Solution

Each octal digit is a group of 3 bits, because 23=82^3 = 8, and each hexadecimal digit a group of 4 bits. To go from octal to hexadecimal you go through binary first.

Since 8 and 16 are powers of 2, each digit is a fixed number of bits and the conversion is direct. There is no direct grouping between octal and hexadecimal (3 and 4 bits do not fit), so you go through binary and regroup.

Question 8Match

Match each system with its digits:

Binary
Octal
Decimal
Hexadecimal

Definitions:

  • 0 to 9
  • 0 to 7
  • 0 to 9 and A to F
  • 0 to 8
  • 0 and 1
Solution
  • Binary → 0 and 1
  • Octal → 0 to 7
  • Decimal → 0 to 9
  • Hexadecimal → 0 to 9 and A to F

A base B has B digits, from 0 to B−1B – 1. That is why there is no 8 in octal and hexadecimal needs six more symbols: A = 10, B = 11, C = 12, D = 13, E = 14 and F = 15.

Question 9Calculation

Write 200(10)200_{(10)} in the other three bases (binary with 8 bits, no spaces).

BaseValue
Binary
Octal
Hexadecimal
Solution
BaseValue
Binary11001000
Octal310
HexadecimalC8

200=128+64+8200 = 128 + 64 + 8, so in binary it is 11001000. In 4s: 1100 1000 = C8. In 3s from the right: 011 001 000 = 310.

Question 10Single choice

What is 1011101(2)1011101_{(2)} in hexadecimal?

  1. 5D
  2. B5
  3. 5B
  4. D5
Solution

Answer: 5D

1011101(2)=64+16+8+4+1=931011101_{(2)} = 64 + 16 + 8 + 4 + 1 = 93 and 5D(16)=5×16+13=93\mathrm{5D}_{(16)} = 5 \times 16 + 13 = 93. B5 is 181: almost double.

Question 11Short answer

Convert AF(16)\mathrm{AF}_{(16)} to octal.

Solution

Answer: 257

Via binary: AF = 1010 1111. In 3s from the right: 010 101 111 = 2 5 7. Check: AF(16)=175\mathrm{AF}_{(16)} = 175 and 257(8)=2×64+5×8+7=175257_{(8)} = 2 \times 64 + 5 \times 8 + 7 = 175.

Question 12Single choice

You convert an opacity of 0.4 to binary with 4 fractional digits and get 0.0110. Which number is really being stored?

  1. 0.375
  2. 0.4
  3. 0.4375
  4. 0.25
Solution

Answer: 0.375

Cutting at a fixed number of digits stores a slightly smaller number: 0.0110(2)=0.3750.0110_{(2)} = 0.375. The difference, 0.025, is the representation error.

Question 13Multiple · wrong ticks subtract

Which of these decimal numbers can be written exactly in binary? (Tick all the correct ones)

  1. 0.5
  2. 0.625
  3. 0.8125
  4. 0.1
  5. 0.3
Solution

Answer: 0.5 · 0.625 · 0.8125

A fraction is exact in binary if it is a sum of halves, quarters, eighths, sixteenths… (its denominator is a power of 2). If multiplying by 2 brings back a value you have already seen, the number is periodic and never ends.

Question 14Short answer

What is the minimum number of bits needed to write the number 300 in unsigned binary?

Solution

Answer: 9

28=2562^8 = 256 and 29=5122^9 = 512. With 8 bits you reach 255 (not enough); with 9, 511. 300 falls between 256 and 512: 9 bits.

Question 15Single choice

What range of numbers can be represented with 3 hexadecimal digits?

  1. 0 to 4095
  2. 0 to 4096
  3. 0 to 999
  4. 0 to 48
Solution

Answer: 0 to 4095

With m digits in base B there are BmB^m numbers, from 0 to Bm−1B^m – 1. 3 hex digits are 12 bits: 212=40962^{12} = 4096.

Question 16Short answer

Convert 0.101(2)0.101_{(2)} to decimal.

Solution

Answer: 0.625

1×0.5+0×0.25+1×0.125=0.6251 \times 0.5 + 0 \times 0.25 + 1 \times 0.125 = 0.625.

Question 17Single choice

What is 777(8)777_{(8)} in decimal?

  1. 511
  2. 777
  3. 512
  4. 21
Solution

Answer: 511

777 in octal is 111 111 111 in binary: nine ones, 29−1=5112^9 – 1 = 511. It is the largest number that fits in 3 octal digits.

Question 18True or false

True or false (look at the base of each number):

  1. 10(16)=16(10)10_{(16)} = 16_{(10)}
  2. 10(2)=2(10)10_{(2)} = 2_{(10)}
  3. 100(8)=64(10)100_{(8)} = 64_{(10)}
  4. 11(2)11_{(2)} is greater than 10(10)10_{(10)}
Solution
  • T — 10(16)=16(10)10_{(16)} = 16_{(10)}
  • T — 10(2)=2(10)10_{(2)} = 2_{(10)}
  • T — 100(8)=64(10)100_{(8)} = 64_{(10)}
  • F — 11(2)11_{(2)} is greater than 10(10)10_{(10)}

«10» in any base B is worth B, and «100» is worth B2B^2. 11(2)=311_{(2)} = 3, less than 10: the same digits say nothing without the base.

Question 19Single choice

A colour is stored with 24 bits (8 per channel). How many different colours can be stored?

  1. 16777216
  2. 16777215
  3. 768
  4. 256
Solution

Answer: 16777216

Each channel has 28=2562^8 = 256 values and they combine: 256×256×256=224=16 777 216256 \times 256 \times 256 = 2^{24} = 16\,777\,216 colours, from #000000 to #FFFFFF.

Question 20Calculation

The eyedropper picks the blue of the sea: #1E6F9C. Convert each channel to decimal.

ChannelHexadecimalDecimal
Red1E
Green6F
Blue9C
Solution
ChannelHexadecimalDecimal
Red1E30
Green6F111
Blue9C156

1E=1×16+14=30\mathrm{1E} = 1 \times 16 + 14 = 30; 6F=6×16+15=111\mathrm{6F} = 6 \times 16 + 15 = 111; 9C=9×16+12=156\mathrm{9C} = 9 \times 16 + 12 = 156. Lots of blue, quite a lot of green and hardly any red: a light navy blue.

What next?

Number systems appear in everything left in IC1. If you want to see these ideas at work:

  • In QtSpim everything is in hexadecimal: memory addresses (0x10010000), register contents and the data segment. Go over it in MIPS program structure and data, where each .word appears in hex in the dump.
  • In MIPS load, arithmetic and logic masks like andi $t0, $t0, 0x0F are groups of 4 bits: with this topic you can read them at a glance.
  • The strings in MIPS arrays and strings are bytes, and each byte, two hex digits.
  • And when you want to test yourself with the full practical exam, there is the IC1 practical mock exam, which marks itself when you hand it in.

The next paper topic is binary arithmetic: adding and subtracting in binary and representing negative numbers with sign-magnitude and ones’ complement. Everything from today (weights, groups, ranges) is used there from the very first line.

To check your solutions, type the number into the One number, every form simulator: you will see each division and each multiplication by 2; its «Test yourself» section brings new numbers to convert.

IC1 exam coming up?The theory and MIPS packs with new solved exams, their cheat sheets, blank exams and .s solutions, for free.See the pack

Cheat sheet: number systems

The essentials in 30 seconds

  • Value of a number = sum of each digit times its weight, di⋅Bid_i \cdot B^i; after the point, negative weights.
  • Base B: B digits, from 0 to B−1B – 1. Hex: A = 10 … F = 15.
  • With m digits: BmB^m numbers, from 0 to Bm−1B^m – 1.
  • Decimal → binary: divisions (remainders from bottom to top) and multiplications (digits from top to bottom).
  • 1 octal digit = 3 bits; 1 hex digit = 4 bits; group from the point, zeros on the outside.

I want to…

I want to…MethodExample
Convert from any base to decimalSum of weightsF4(16)=15⋅16+4=244\mathrm{F4}_{(16)} = 15 \cdot 16 + 4 = 244
Convert a decimal integer to base BSuccessive divisions by B162 → 10100010
Convert a decimal fraction to binarySuccessive multiplications by 20.75 → 0.11
Convert binary to octal or hexGroups of 3 or 4 bits from the point11110100 → 364₈ / F4₁₆
Convert between octal and hexVia binary7531₈ → F59₁₆
Know how many bits N needsSmallest m with 2m−1≥N2^m – 1 \ge N1000 → 10 bits
Know the representation errorValue asked for − value stored0.4 with 4 digits → 0.025

Powers and digits

2n2^nValue2−n2^{-n}ValueHexBinaryOctal
202^012−12^{-1}0.50-70000-01110-7
242^4162−22^{-2}0.258100010
282^82562−32^{-3}0.125A (10)101012
2102^{10}10242−42^{-4}0.0625C (12)110014
2122^{12}40962−52^{-5}0.03125F (15)111117

Templates

Successive divisions (integer part): N / B = quotient, remainder → repeat with the quotient until it is smaller than B → result = last quotient + remainders from bottom to top.

Successive multiplications (fractional part): f × 2 = integer part (digit) + rest → repeat with the rest until 0 or until the digits asked for → result = digits from top to bottom → error = f − stored value.

Grouping: outwards from the point, 3 bits (octal) or 4 bits (hex); padding with zeros on the left of the integer part and on the right of the fractional part.

Symptom / likely cause

SymptomLikely cause
The binary is worth a number that looks nothing like the starting decimalRemainders read backwards
The fractional part comes out greater than 1 or very oddDivided instead of multiplying
The hex or octal comes out almost double (or half)Grouped from the left or padded on the inside
You get an 8 in octal or a «12» in a hex digitDigit outside the base: in hex, 12 is C
The range ends at 2m2^m or at 4096Confused how many numbers with the largest
The representation error is larger than the weight of the last digitOne of the multiplication digits is wrong

Before you hand it in, ask yourself

  • Does each number carry its base?
  • Have I written the number of bits they asked for, with zeros on the left?
  • Have I checked each conversion by another route (sum of weights)?
  • If there was a fractional part, have I given the stored value and the error?
Keep goingAll Computers articles

Did this article help you? Here is how you can support SergioLearns →

Ready for the exam?IC1 practical mock exam — MIPS in QtSpim with other data, subroutines and keyboard input (and why part 1 decides almost everything)