Bit Manipulation

Bit manipulation: reading and changing single bits with AND, OR, XOR and shifts, and where Python, Java and TypeScript give different answers.

  • 8 cards
  • 4 questions
  • About 19 min
  • Intermediate to Advanced
Share

Bit manipulation treats an integer as a row of bits and reads or changes them with AND, OR, XOR, NOT and the two shifts, each in one step. It matters because questions about a small set of on-or-off choices, about values that cancel in pairs, or about powers of two can be answered this way in O(1) extra space, and because Python, Java and TypeScript give different answers at the 32nd bit. The topic applies the six operators to one pair of numbers, finds a badge scanned once with XOR, and gives the common bit tricks in three languages with the lines where their outputs differ. It counts set bits by clearing the lowest one, compares pizza orders stored as bitmasks, shows why a sign test fails with a shift alone, and ends with an exercise on a missing ticket number.

Contents

8 cardsnot started