</> MAANG.io
coding interview ยท 101

Foundations

Master coding interviews with comprehensive coverage of data structures, algorithms, and problem-solving techniques. Progress from fundamentals to advanced topics with expertly curated content.

0/255 solved 0% complete

Fundamentals

What is this?

Think of a number as a row of light switches that are each either on or off. These pages teach you the basic vocabulary for working with those switches โ€” how to read one, turn it on, turn it off, or flip it โ€” before you tackle any puzzles. It is the alphabet you need before you can read the words.

flowchart TD A["See number as bit switches"] A --> B["Read a switch with AND"] B --> C["Turn on with OR, flip with XOR"] C --> D["Build a mask to target columns"] D --> E["Handle signs with twos complement"]

๐Ÿ’ก Fun fact: A single byte holds eight bits, which is why a classic color channel runs from 0 to 255 โ€” that is every possible combination of eight on/off switches, exactly 256 values.

๐Ÿ”“ The deep dives in this chapter are free. Sign in with Google or Microsoft to read the deep dives.


Core idea: every integer is just a fixed-width string of bits, and AND, OR, XOR, and the shifts let you read, set, clear, and move those bits one column at a time. Once you see numbers as bit vectors instead of magnitudes, a surprising amount of work collapses into a single machine instruction.

The pattern

These pages are concept primers, not problems โ€” they build the vocabulary the rest of the topic assumes.

  • The operators. AND keeps a bit only where both inputs have it (great for masking off what you don't want). OR keeps it where either does (turning bits on). XOR flips a bit where the inputs differ (toggling, and the engine behind self-canceling tricks). NOT inverts everything.
  • Shifts. x << k multiplies by 2^k and slides bits toward the high end; x >> k divides by 2^k (floor) and slides toward the low end. Shifting is how you build powers of two and reposition fields.
  • Masks. A mask is a hand-built bit pattern you combine with a value to isolate or alter specific columns: x & mask reads bits, x | mask sets them, x & ~mask clears them, x ^ mask flips them. 1 << i builds a single-bit mask for position i.
  • Two's complement. Signed 32-bit integers store negatives as the complement-plus-one, so the top bit is the sign and arithmetic "just works" without special cases. This is why -1 is all ones and why overflow wraps around instead of erroring.
  • Where it shows up. Permission flags and feature toggles packed into one int, RGBA color channels, network masks and IP subnetting, bitsets and Bloom filters, hardware register control, and compression. Anywhere memory or speed is tight, bits earn their keep.

The problems

This bucket holds tutorials rather than coding problems: Bit Manipulation Foundations, Essential Bit Tricks, 32-Bit Signed Representation, and Bit Manipulation Applications. Read them first โ€” the powers, XOR, and bit-pattern chapters all lean on the masks and two's-complement reasoning introduced here.

Key takeaways

  1. Treat an integer as a fixed-width bit vector; the operators act column by column.
  2. AND masks, OR sets, XOR toggles, NOT inverts โ€” pick the operator by what you want each bit to do.
  3. Left shift multiplies by powers of two, right shift divides; both reposition fields cheaply.
  4. Two's complement makes the sign bit and wraparound behavior fall out naturally on 32-bit ints.
  5. Bit tricks earn their place wherever space and speed matter โ€” flags, colors, networking, and compact data structures.

Bit Manipulation Foundations

Core idea: An integer is a fixed row of bits, and six operators (& | ^ ~ << >>) let you read and rewrite those bits in parallel. Master the set mental model plus two identities โ€” x & (x-1) (clear lowest set bit) and x & -x (isolate lowest set bit) โ€” and the whole topic opens up.


1. Bits as a set

The single most useful reframing: a non-negative integer is a set of positions where the bit is 1. Bit i is "in the set" iff (n >> i) & 1 == 1.

Integer 22 as the set of bit positions {1, 2, 4}, where the 1-bits mark the members

Under that lens the operators are set algebra:

Operator Bit-level Set meaning
a & b 1 where both are 1 intersection
a | b 1 where either is 1 union
a ^ b 1 where exactly one is 1 symmetric difference / toggle
~a flip every bit complement
a << k append k zeros (ร—2แต) shift positions up
a >> k drop k low bits (รท2แต) shift positions down

^ (XOR) is the quiet star: a ^ a == 0, a ^ 0 == a, and it's associative/commutative โ€” so XOR-ing a list cancels every value that appears an even number of times, leaving the odd one out.


Sign in to continue reading

The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.

Sign in to read the full lesson

Sign in to MAANG.io

Use your Google or Microsoft account โ€” no password to remember.

Continue with Google Continue with Microsoft

Please accept the terms above to continue.