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) & 1reads bit k,v | (1 << k)sets it,v & -visolates 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) - onesFinish 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.
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.