Binary Conversion Drill
exercises
computer-systems
binary
Convert between different number representations. All binary numbers are eight bits.
On the HTML site, fill in the blank with your answer (as a quoted string, e.g. "1A"), then click Run Code on the exercise to check. Reload the page for a new random question. In the PDF, the reference implementation is shown instead (interactive exercises need a browser).
The two short reference sections below are the minimum needed to attempt the drills without looking anything up; for the full treatment see binary-number-representations, and for the lab on these conversions, week1-lab-number-representations. Fractions, which these drills leave out, are in fixed-point-numbers and floating-point-numbers.
Doing a base conversion: one number, all four bases
The first drill picks two of decimal, hexadecimal, octal and unsigned binary at random and asks you to convert between them. Which pair comes up decides whether the job is regrouping or arithmetic:
- A hexadecimal digit stands for sixteen values, and so do 4 bits (\(2^4 = 16\)). An octal digit stands for eight values, and so do 3 bits. Both therefore convert to binary digit by digit, with no arithmetic at all.
- A base-10 digit stands for ten values, and so do \(3.321928\ldots\) bits — not a whole number. Decimal never lines up with a group of bits, so every decimal conversion costs real arithmetic: repeated division by 2 going in, a sum of powers of two coming out.
Figure 1: The same value, 205, written in all four bases the first drill uses, with the two groupings drawn on the one row of bits. The hexadecimal nibbles and the octal triples are cut from the same eight bits; the octal grouping needs a padding zero on the left, by the rule that 123 = 0123 but 456 is not 4560.
Two things to carry into the drill:
- Group from the right, and pad the leftmost group with zeroes. If the bit count is not a multiple of 3 or 4, the short group is the leading one. \(123 = 0123\), but \(456 \neq 4560\).
- Hex to octal has no shortcut. Three and four bits share no smaller common grouping, so expand the hex digits to bits and regroup them in threes. For example,
0xC55E7201becomes 32 bits, pads to 33, and regroups as \(30527471001_8\).
Doing a signed conversion: four recipes on the same number
The second drill converts between decimal and the four signed formats. Going from decimal to bits, each format has its own recipe, and only one of them is really a negation at all.
Figure 2: Plus and minus 47 carried through all four formats, arranged so the recipes can be compared on one number. Orange marks the bit a recipe actually changes; blue marks bits that are recomputed wholesale. The last row is the trap: excess-128 biases positive values too, so it is the one format that does not share the positive patterns with the others.
As a table, for looking up mid-drill:
| Format | Encode a negative by | Range, 8-bit |
|---|---|---|
| Signed magnitude | inverting the sign bit alone | \(-127\) to \(+127\) |
| Ones’ complement | inverting all the bits | \(-127\) to \(+127\) |
| Two’s complement | inverting all the bits and adding 1 | \(-128\) to \(+127\) |
| Excess-128 | storing value \(+128\) as plain unsigned | \(-128\) to \(+127\) |
Signed magnitude and ones’ complement each spend two of their 256 patterns on zero (00000000 and 10000000; 00000000 and 11111111), which is exactly the one value of range they give up compared with two’s complement.
Number base conversion
A random question is generated once per page load using an ojs cell, then passed into the Python exercise cells below via the input cell option – this keeps the question stable across repeated “Run Code” clicks (setup blocks re-run on every evaluation, so randomising there would change the question out from under the learner).
Signed number format conversion
Reading one pattern under one format
“What is 10010010 converted to decimal?” has no answer until someone names the format. This drill is that question, generated: it hands you eight bits and one format, and asks what value they hold. Answer with a plain integer (e.g. -110), not a string.