Bitwise Operators
AND, OR, XOR, NOT, and shifts.
Bitwise Operators is a free Zig Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Down to the Bits
Every integer is really a row of bits. Bitwise operators let you work on those individual ones and zeros directly, with no rounding or math in between.
Bitwise AND
The & operator keeps a bit only where both inputs have a 1. It is the classic tool for masking off the bits you do not want.
const masked = value & 0x0F;Bitwise OR
The | operator sets a bit where either input has a 1. Use it to turn specific flags on without disturbing the rest.
const withFlag = value | 0b0010;Bitwise XOR
The ^ operator sets a bit only where the inputs differ. XOR is handy for toggling a flag or comparing two patterns.
const toggled = value ^ 0b0001;Bitwise NOT
The unary ~ operator flips every bit at once: each 1 becomes 0 and each 0 becomes 1. It builds the complement of a value.
const inverted = ~flags;Shifting Left
The << operator slides bits toward the high end, filling in zeros. Each shift left by one doubles an unsigned value.
const doubled = x << 1;Shifting Right
The >> operator moves bits toward the low end. For an unsigned integer, each right shift by one halves the value.
const halved = x >> 1;Masking Reads Bits
Combine AND with a mask to test whether a bit is set. If value & mask is nonzero, that flag is on; if zero, it is off.
const isSet = (value & mask) != 0;Setting and Clearing
OR a mask to set bits, and AND with an inverted mask to clear them. Together they give you precise control over each flag.
const cleared = value & ~mask;Shift Amounts Are Checked
Zig will not let you shift by more than the type holds. A shift amount out of range is a defined error, not silent garbage.
Why Bits Matter
Bit operations power flags, permissions, hardware registers, and compact data. They let you store many true-or-false answers in a single number.
Quick Check
You want a bit set only when the two inputs differ. Which operator?
Recap: Master of Bits
You now use &, |, ^, ~, plus the shifts << and >>, to read, set, clear, and move bits with full precision. ⚙️
Frequently asked questions
Is the “Bitwise Operators” lesson free?
Yes — the full text of “Bitwise Operators” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.
What will I learn in “Bitwise Operators”?
AND, OR, XOR, NOT, and shifts. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Zig Academy?
No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Bitwise Operators” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Zig Academy lesson?
Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.