Terminology (7)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.
- Bitwise operator
- An operator that combines integers bit position by bit position: &, |, ^ and bit inversion.
- Mask
- An integer whose 1 bits select the positions an operation reads or changes.
- Set, clear, toggle
- Make the selected bits 1, make them 0, or flip each one.
- Bit field
- A group of adjacent bits that together hold one small number, such as a two-bit tier.
- Shift
- Moving every bit left or right by a number of positions: x << 4 moves a value into bits 4 and up.
- Short-circuit evaluation
- && and || skip the right operand when the left one already decides the result.
- 32-bit conversion
- With number operands, &, | and ^ convert to 32-bit signed integers. bigint operations do not use that conversion.
Operating on selected bits
Bitwise operations combine corresponding bit positions independently. A mask marks the positions an operation will inspect or change. One integer can therefore carry several flags or small fields, provided each operation preserves the positions outside its responsibility.
A flag update and a condition ask different questions. Setting, clearing and toggling have different behavior when repeated; checking whether any selected bit is present differs from checking all of them. Keep those operations distinct from logical evaluation, where deciding whether another expression runs can matter.
01Bit by bit
Combine permission masks without numeric carries
The folder tool stores permissions, a storage tier, locked and archived status in one eight-bit record. A grant or revoke can arrive more than once and can select several bits. The tool must make the requested change without disturbing other settings. Before connecting these commands to permission checks, inspect what each operator preserves and what a multi-bit test actually proves.
Two permission masks select positions 1100 and 1010. Before using a mask to change the record, check how &, | and ^ combine these selections. Predict each result as four binary digits.
These operands are number values. Their &, | and ^ operations convert them to 32-bit signed integers and return a number; bigint has separate bitwise operations.
const a = 0b1100;
const b = 0b1010;
const show = (value: number) => value.toString(2).padStart(4, "0");
console.log(show(a & b), show(a | b), show(a ^ b));
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 displayed command’s result, including whether the source is accepted.
02Set, clear, toggle
Make repeated permission commands keep their meaning
The folder can already hold some permissions named by a command. This probe starts each operation from flags = 0101: it sets bit 1, clears bit 2, then toggles bit 2 and bit 3. Predict the last line, which toggles a bit that starts clear.
~ inverts every bit, so flags & ~mask keeps everything except the mask’s bits.
const flags = 0b0101;
const show = (value: number) => value.toString(2).padStart(4, "0");
console.log(show(flags | 0b0010));
console.log(show(flags & ~0b0100));
console.log(show(flags ^ 0b0100));
console.log(show(flags ^ 0b1000));
03Testing flags
Require every permission for the combined action
The folder has READ but not WRITE. An indicator can report whether either permission is present; an action requiring both needs a stronger test. The program compares those questions using the mask READ | WRITE. Predict both results.
Both comparisons put flags & mask in parentheses.
const READ = 0b0001;
const WRITE = 0b0010;
const flags = READ;
const mask = READ | WRITE;
console.log((flags & mask) !== 0, (flags & mask) === mask);
04Bitwise versus logical
Choose whether the next check should run
If a permission check is false, the caller may not need a second check. The probe gives each call a printed name and compares && with &. Logical && can skip its right operand; predict which named calls run, or whether the source is refused.
function check(name: string, result: boolean): boolean {
console.log("check", name);
return result;
}
const lazy = check("a", false) && check("b", true);
const eager = check("c", false) & check("d", true);
console.log(lazy, eager);
05Repair the permissions
Grant, revoke, then replace the tier
Finish the folder command handler. Grant every requested bit without carrying into another position, revoke every requested bit even when some are already absent, then replace the storage tier while preserving the surrounding settings. Grant and revoke apply to every position selected by the supplied eight-bit masks.
The tier uses bits 4–5. For the supplied tier values 0 to 3, tier << 4 moves their two-bit pattern into that field: 1 becomes 00010000, 2 becomes 00100000, and 3 becomes 00110000. Bits 0–3 hold permissions, bit 6 locked and bit 7 archived.
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 folder permissions
Keep repeated grants stable, handle partially held revocation masks, then change the packed tier without losing other settings.
Sources
Sign in to keep your read marks on every device. Sign in
Was this helpful?