← Fundamentals
Fundamentals

Masks, shifts, and bitwise operations

Change a folder’s selected permissions while preserving the rest of its packed record.

Read a bit mask as a selection of positions within an integer.

Outcome

Predict per-bit &, | and ^; set, clear, toggle and test flags with masks; replace a packed field with shifts while preserving other bits; and distinguish bitwise operators from short-circuit logic in each language.

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.
Operator precedence
In C++, == and != bind tighter than &, ^ and |, so mask tests need parentheses.

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.

Both operands go through the usual arithmetic conversions; here they are unsigned.

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
#include <bitset>
#include <iostream>

int main() {
    unsigned a = 0b1100;
    unsigned b = 0b1010;

    std::cout << std::bitset<4>(a & b) << ' ' << std::bitset<4>(a | b) << ' '
              << std::bitset<4>(a ^ b) << '\n';
}
What does the program print?
C++ · terminal.sh
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
clang++ -std=c++2c -Wall -Wextra -pedantic main.cpp -o main && ./main

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 of its promoted operand, so flags & ~mask clears the mask’s bits.

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
#include <bitset>
#include <iostream>

int main() {
    unsigned flags = 0b0101;

    std::cout << std::bitset<4>(flags | 0b0010) << '\n';
    std::cout << std::bitset<4>(flags & ~0b0100u) << '\n';
    std::cout << std::bitset<4>(flags ^ 0b0100) << '\n';
    std::cout << std::bitset<4>(flags ^ 0b1000) << '\n';
}
What does the last line print?

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.

The second test is written without parentheses: flags & mask == mask.

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
#include <iostream>

constexpr unsigned read = 0b0001;
constexpr unsigned write = 0b0010;

int main() {
    unsigned flags = read;
    unsigned mask = read | write;

    bool any = (flags & mask) != 0;
    bool all = flags & mask == mask;

    std::cout << std::boolalpha << any << ' ' << all << '\n';
}
What does the program print?

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.

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
#include <iostream>
#include <string>

bool check(const std::string &name, bool result) {
    std::cout << "check " << name << '\n';

    return result;
}

int main() {
    bool lazy = check("a", false) && check("b", true);
    bool eager = check("c", false) & check("d", true);

    std::cout << std::boolalpha << lazy << ' ' << eager << '\n';
}
Which checks run?

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.

  1. easy · F-BITWISE-V4-1 Grant permissions
  2. medium · F-BITWISE-V4-2 Revoke permissions
  3. hard · F-BITWISE-V4-3 Set the storage tier
Sources

Report a problem in this lesson

For typos, mistakes and things that don’t work. We read every report and fix what’s wrong, but we don’t reply here, so a question won’t get an answer.