Terminology (10)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- Bit
- One binary digit, 0 or 1.
- Byte
- Here, an eight-bit colour channel (an octet): 256 patterns representing 0 to 255, written with two hex digits. A C++ storage byte has an implementation-defined bit count.
- Base (radix)
- How many distinct digits a numeral uses, and so how much each place is worth: 2, 8, 10 or 16 here.
- Place value
- A digit’s contribution: its value times the base raised to its position, counting from zero at the right.
- Hexadecimal
- Base 16, with digits 0–9 and a–f. One hex digit covers four bits.
- Octal
- Base 8, with digits 0–7. Written 0o… where a language supports it.
- Digit separator
- A character inside a literal that groups digits for reading without changing the value: _ in TypeScript, Go and Rust, ’ in C++.
- Formatting width
- The minimum number of characters a formatter writes, filled with spaces unless you choose another fill.
- Parsing
- Converting text into a value, here digit text into a number in a stated base.
- parseInt
- Reads digits of a radix from the start of a string and returns NaN only when it cannot read one.
Representing a value with digits
A bit has two possible values, 0 and 1. A numeral’s base determines the weight of each digit position: binary uses powers of two and hexadecimal uses powers of sixteen. For example, binary 101 represents the same integer as decimal 5. Changing the notation need not change the value.
The colour format in this reading groups bits into eight-bit bytes, also called octets. Source literals, stored numeric values and text fields are separate representations. A literal parser interprets source syntax; a formatter chooses text and padding; an input converter applies its own acceptance rules.
01Base prefixes
Check the channel copied from another project
A colour-code tool is being connected to a form and a style sheet. Its red channel arrived from another project as 036, alongside 0x1e and 0b0001_1110. Before using it, check whether those source spellings name the same value in this language. Then unpack the stored colour into channels, write fixed-width channel text and decide which edited text the tool should accept back.
The imported red channel has several source spellings. A literal’s prefix selects the base: 0x for hexadecimal (base 16), 0b for binary (base 2). Each digit contributes its value times a power of the base, counting from the right: 0x1e is 1 × 16 + 14, and 0b0001_1110 is 16 + 8 + 4 + 2.
The underscores, or the apostrophe in C++, are digit separators. They group digits for reading and change nothing. Predict what the program prints, including the fourth value, which is written with a leading zero.
| Spelling | Base | Digits × place weights | Value |
|---|---|---|---|
| 0x1e | 16 | 1 × 16 + 14 × 1 | 30 |
| 0b0001_1110 | 2 | 1 × 16 + 1 × 8 + 1 × 4 + 1 × 2 | 30 |
| 30 | 10 | 3 × 10 + 0 × 1 | 30 |
const hex = 0x1e;
const binary = 0b0001_1110;
const decimal = 30;
const padded = 036;
console.log(hex, binary, decimal, padded);
npx tsc --noEmit --strict main.ts && node main.ts
Use the selected language’s setup from the first reading and replace the file for each experiment. Predict the result of the displayed command, including whether it accepts the source. C++ uses the track’s C++26 mode; these programs use features available since C++14.
02Bytes as digits
Unpack the stored colour into form channels
The colour format gives each channel eight bits, an octet, with 2⁸ = 256 patterns representing 0 to 255. Byte here means this eight-bit group. Each hex digit covers four bits, so two digits spell one channel, from 0x00 to 0xff.
The stored value 0x1e90ff packs red 0x1e, green 0x90 and blue 0xff into three base-256 places. The program extracts those channels for the form. A proposed low-channel calculation uses % 255, treating the largest channel value as the base; the last line tests that calculation. Predict its result.
const colour = 0x1e90ff;
const blue = colour % 256;
const green = Math.floor(colour / 256) % 256;
const red = Math.floor(colour / 65536);
console.log(colour);
console.log(red, green, blue);
console.log(colour % 255);
03Formatting
Give each channel its required display width
A low channel such as 9 still occupies two hex positions in #rrggbb. Test the formatter’s default spelling before assembling the style-sheet code. The probe also writes binary text to check how a display width is requested. Predict the first line.
Number.prototype.toString(radix) returns the digits of a number in that base. padStart pads a string to a length.
const channel = 9;
console.log(channel.toString(16));
console.log(channel.toString(2));
console.log(channel.toString(2).padStart(8, "0"));
04Parsing digit text
Check what edited channel text the converter accepts
The form returns text, even when it looks like a channel number. The colour tool needs two hex digits per channel, but its numeric converter has its own rules for signs, spaces and leftover text. Test that converter before relying on success as the field’s acceptance decision.
The probe tries "1e", "1g" and "+f" as hexadecimal. Predict which fields the conversion accepts as numbers; the form’s stricter grammar is a separate requirement.
parseInt(text, 16) parses in base 16. It returns NaN when it cannot read a number.
const fields = ["1e", "1g", "+f"];
for (const field of fields) {
console.log(field, parseInt(field, 16));
}
05Repair the colour tool
Write channels, read codes, expand short codes
Finish the colour tool’s two-way connection. Write channel numbers in the style sheet’s fixed-width layout, then accept edited codes only when they meet the field’s digit grammar. Finally support the designer’s three-digit #rgb shorthand with 0 and f still at the channel’s darkest and brightest endpoints.
The lab opens in your reading language and has its own language control. Each file contains its imports, types, reader and complete main. Later levels supply the earlier correct helpers.
Repair the colour-code tool
Keep the style-sheet layout, enforce the input grammar, then derive what a short code means for each full-range channel.
Sources
Sign in to keep your read marks on every device. Sign in
Was this helpful?