Skip to content
CodeItRaw
Path

Playing with numbers in binary: XOR tricks, counting bits and building an answer bit by bit.

Key ideas

  • XOR (^): equal bits give 0, different ones 1. a ^ a = 0, a ^ 0 = a; order does not matter.
  • (v >> k) & 1 reads bit k, v | (1 << k) sets it, v & -v isolates the lowest set bit.
  • Bits are independent: you can often count per bit and combine the results.

Pattern

for bit in range(BITS):
    ones = sum((v >> bit) & 1 for v in nums)
    # combine ones and len(nums) - ones

Finish these first:Hashing

Lessons

Each lesson explains one idea from zero and ends with a few tasks. Lessons open in order; the explanation can be read at any time.

  1. 01

    Bits and four operators

    0/2 tasks

  2. 02

    XOR cancels itself

    0/2 tasks

  3. 03

    Masks, and thinking bit by bit

    0/3 tasks

Boss

  • 04

    Maximum XOR

    Function

  • The boss opens when every lesson of the topic is finished. Solve it too and the topic is complete, and the topics after it open.