Bit manipulation: masks, XOR and popcount in practice

12 steps · about 4 h 45 min · Intermediate

  1. Bitwise Operations & Bit Masking (video, YouTube, 15 min)

    A beginner-level start that shows what AND, OR, XOR, shifts and masks do before you use any tricks.

  2. Bit manipulation visualizer: Bitwise Ops Step by Step (interactive, p14m.com, 15 min)

    You can type expressions and watch each operator change the bits, which turns the first video into hands-on practice.

  3. Bit manipulation (article, Wikipedia, 10 min)

    Shows where bit manipulation is used (compression, encryption, device control) so you know why the tricks matter.

  4. Bit manipulation - Algorithms for Competitive Programming (article, CP-Algorithms, 25 min)

    A concise reference for set, flip and clear a bit, clearing the lowest set bit, and the standard-library helpers.

  5. Bitwise Operations tutorial #1 | XOR, Shift, Subsets (video, YouTube, 25 min)

    Moves from single operators to XOR properties and looping over all subsets with a bitmask.

  6. Bit Manipulation Tricks Every Developer Should Know (With Examples) (article, CodeEasy, 20 min)

    A cheat sheet of everyday tricks (odd check, shifts, extract and set bits) with an explanation of each.

  7. Bit Tricks for Competitive Programming (article, GeeksforGeeks, 25 min)

    Explains why x & (x-1) and x & ~(x-1) work, so you can derive the tricks instead of memorising them.

  8. Coding Interview: Bit Manipulation Patterns — XOR Tricks, Bitmask, Counting Bits, Power of Two, Bitwise Operators (article, techinterview.org, 25 min)

    Applies XOR cancellation to the Single Number problems, including the two-unique-numbers variant, as practice.

  9. Bit tricks for performance (article, The DSA Handbook, 30 min)

    Compares Kernighan's popcount with the branch-free SWAR version and hardware POPCNT, and also covers adding without + using XOR and AND.

  10. Hamming weight (reference, Wikipedia, 20 min)

    Gives the theory behind popcount: Hamming distance as popcount of an XOR, and uses in coding theory and RSA.

  11. Bit Twiddling Hacks (reference, Sean Eron Anderson, Stanford, 45 min)

    The classic collection of advanced tricks, best read last once you can derive the basic ones yourself.

  12. bithacks: Bit Twiddling Hacks By Sean Eron Anderson: code snippets in Python (repo, GitHub (ianbrayoni), 30 min)

    Python code for the parity and bit-counting hacks, so you can run and test the ideas from the Stanford page.