Reading a flag is only half the job. You may need to turn one setting on or off without disturbing the others. Bitwise operations let you express those changes directly.
| Operation | Meaning at each bit |
|---|---|
a | b | Keep a one when either operand has a one |
a & b | Keep a one only when both have a one |
~a | Reverse every bit of the operand |
a ^ b | Keep a one when the operands differ |
Use OR to set a bit: flags |= mask abbreviates flags = flags | mask. Use AND with an inverted mask to clear it: flags &= ~mask. The inverted mask has zeros at the selected positions and ones elsewhere, so unrelated bits survive.
#include <iostream>
int main() {
constexpr unsigned int highlighted = 0b010u;
unsigned int style = 0b001u;
style |= highlighted;
std::cout << style << '\n';
style &= ~highlighted;
std::cout << style << '\n';
}
The first update changes 0b001u to 0b011u; the second restores 0b001u. The output shows the decimal values 3 and 1, not those binary spellings. Setting an already-set bit leaves it set; clearing an already-clear bit leaves it clear. In contrast, XOR toggles a bit, so repeating that operation reverses the first change.
The editor gives you two independent flag values. Set an editing permission in the first, and clear it from the second. Keep the masks unsigned so the inversion and combination use the intended unsigned type. These tasks use named masks rather than unexplained numeric additions or subtractions; arithmetic can accidentally affect neighboring bits when a flag is already set.
Edit main.cpp; the supplied Console probes already call your code.
|= with edit to set that bit in granted. Expect Granted: 3.&= with ~edit to clear that bit in restricted. Expect Restricted: 5.Use Run to inspect the Console, then Submit to check your work.