Terminology (8)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.
- Fixed-width integer
- An integer type with a set number of bits, and so a set range.
- Unsigned
- A type whose bit patterns all mean values from 0 upward: 0 to 2ⁿ − 1.
- Two’s complement
- The signed encoding in which the top bit is worth −2ⁿ⁻¹, giving −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1.
- Overflow
- An arithmetic result outside the range of the type the operation is done in.
- Wrapping
- Keeping the low n bits of a result, which is the value modulo 2ⁿ.
- Saturating
- Replacing an out-of-range result with the nearest end of the range.
- Narrowing conversion
- Converting to a type with fewer bits. The ordinary unsigned integer conversions shown keep low bits; a clamped typed-array store saturates instead.
- Typed array
- An array of fixed-width elements such as Uint8Array or Int32Array; storing a number converts it to the element type.
Finite ranges and arithmetic policies
A fixed-width integer has a finite set of bit patterns. Signedness determines how those patterns represent values; overflow occurs when an arithmetic result exceeds the range of the type doing the calculation. A later conversion to a narrower type is another boundary, even when the calculation itself fit.
Choose the policy for the job: preserve a growing total, reject an out-of-range input, clamp it to a limit or deliberately wrap within a fixed width. The expression’s type, destination type and selected language’s rules determine what the program actually does. A wider destination cannot repair information already lost in an earlier operation.
01Signed and unsigned
The same eight bits have two readings
A retro-game save editor reads one byte of coins per stage. Its summary must add counts without losing coins, its checksum must follow the save format’s 32-bit rule, and its edits must leave valid byte counts. Start by checking how the selected language reads the stored bytes. Then separate the arithmetic used for each job.
The save format calls its coin field an unsigned byte. Before adding counts, check what happens if the editor reads that byte through a signed type. Eight bits give 256 patterns: an unsigned 8-bit type uses them for 0 to 255; a signed 8-bit type uses them for −128 to 127.
The program reads two bytes, 0xff and 0x80, first as unsigned values and then as signed ones. Predict the signed line.
TypeScript has no 8-bit number type. Typed arrays store fixed-width elements, and an Int8Array over the same buffer as a Uint8Array reads those bytes as signed.
const bytes = Uint8Array.of(0xff, 0x80);
const signed = new Int8Array(bytes.buffer);
console.log(bytes.join(" "));
console.log(signed.join(" "));
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. Rust uses cargo run with the default dev profile and no overflow-check overrides. The Go target has 64-bit int. The C++ target has 8-bit bytes, 32-bit int and 64-bit std::size_t; its exact-width integer types are available. C++ uses the track’s C++26 mode; std::in_range needs C++20.
02One past the top
At the edge, arithmetic wraps, stops or widens
The summary must count one more coin after a stage reaches 255. Does the addition happen in the byte type, in a wider type, or only become a byte when stored? This small probe separates calculation from storage before the editor sums whole slots. Predict what the program prints.
levels is a Uint8Array. levels[0] + 1 is ordinary number arithmetic, and levels[0] += 1 stores the result back into the array.
const levels = Uint8Array.of(255);
const next = levels[0] + 1;
levels[0] += 1;
console.log(next, levels[0]);
03Narrowing
Check what an edited count stores
An edit can propose 300 coins or −20 coins even though neither fits the saved field. The program isolates the store from the addition. Does conversion refuse the proposal, stop at an endpoint, or produce another count? Predict the first line.
Uint8Array.of stores each argument as an 8-bit element. The second line uses Uint8ClampedArray instead.
const reading = 300;
console.log(Uint8Array.of(reading)[0]);
console.log(Uint8ClampedArray.of(reading)[0]);
console.log(Uint8Array.of(-20)[0]);
04Choosing the width
The type of the arithmetic decides where it wraps
The save format defines its checksum modulo 2³². Making it wider like the coin total would change what is written to the save. This probe takes the largest unsigned 32-bit value and adds 1 using different arithmetic types. Predict where each result crosses its boundary.
max is 0xffffffff. >>> 0 converts a number to an unsigned 32-bit integer, and | 0 converts it to a signed one.
const max = 0xffffffff;
console.log(max + 1);
console.log((max + 1) >>> 0);
console.log(max | 0);
05Repair the save editor
Totals, a 32-bit checksum, then clamped edits
One save editor for a retro game continues through three repairs. Each stage’s coin count is a byte. First report running totals that do not wrap. Then compute the save’s 32-bit checksum, which must wrap at exactly 32 bits. Finally apply signed edits that keep every count between 0 and 255.
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 save editor
A total wider than a byte, wrapping at exactly 32 bits, then a range that holds at both ends.
Sources
Sign in to keep your read marks on every device. Sign in
Was this helpful?