All puzzles › Bit Manipulation
Bit Manipulation Practice Problems
47 puzzles (17 easy, 21 medium, 9 hard), 15 free, in Python, JavaScript and C++.
Every number in a computer is a row of bits, and a handful of operators let you work on those bits directly. It looks like magic until you have seen the five or six moves that everything is built from: test a bit, set one, clear one, flip one, and the fact that a value XORed with itself is zero.
How to spot a bit manipulation problem
The hints are pairs that cancel, powers of two, flags packed into one integer, and any request to do it without extra memory. If every value appears twice except one, XOR finds the odd one out in a single pass with one variable.
New to it? Read the lesson first: Bit Manipulation.
The puzzles, easiest first
- An Id That Sorts by Time (Easy, free): Many machines need to hand out unique ids at the same time without asking each other.
- Free Bits in Every Pointer (Easy, free): Memory allocators hand out addresses that are multiples of 8, so the lowest three bits of every pointer are always zero.
- One Question per Bit (Easy, free): An oracle hides an n-bit secret.
- Which Eighth of the Box (Easy, free): An octree splits a box of space into eight smaller boxes around its center, and to store a point you need to know which of the eight it falls in.
- Count Set Bits (Easy): Given a number, return how many 1 bits it has in binary.
- Count Trailing Zeros (Easy): Given a number, return how many 0 bits come after its last 1 bit.
- Draw the Netmask (Easy): Given a CIDR prefix length from 0 to 32, return the subnet mask as four numbers.
- Drop the Last One (Easy): Given a number, return it with its lowest 1 bit turned off and everything else the same.
- Exactly One Bit (Easy): Given a number, return true if exactly one of its bits is 1.
- Flip One Bit (Easy): Given a number n and a position i, return n with bit i flipped and every other bit left alone.
- Isolate the Last One (Easy): Given a number, return just its lowest 1 bit, with every other bit cleared.
- Read One Bit (Easy): Given a number n and a position i, return 1 if bit i is set, or 0 if it isn't.
- Single Number (Easy): Given an array where every value appears twice except one, return the one that appears once.
- Swap Without a Spare (Easy): Given two numbers, swap them without a temporary variable, using XOR three times: x = x ^ y, then y = x ^ y, then x = x ^ y.
- The Next Number (Easy): Given a seed, return the next one: multiply by 1664525, add 1013904223, and keep the low 32 bits.
- The Sign Bit (Easy): Given the raw bits of a signed integer, read as an unsigned number, and its width in bits, return the value it actually stores.
- Who Is Allowed to Touch? (Easy): Each object has a layer, one bit saying what it is, and a mask, a bitmask of every layer it reacts to.
- A Checksum That Notices Order (Medium, free): A plain sum cannot tell when two bytes have swapped places.
- A Length Hidden in Two Bits (Medium, free): QUIC, the protocol under HTTP/3, stores numbers in as few bytes as they need.
- One Bit per Dimension (Medium, free): An embedding can be squeezed to one bit per dimension and still find the right document most of the time.
- Read the Contour From the Corners (Medium, free): To draw a contour line through a grid of measurements, look at each small square formed by four neighboring values.
- Round Up, Then Split (Medium, free): A buddy allocator only deals in blocks whose sizes are powers of two.
- The Error Points to Itself (Medium, free): A Hamming code sends 4 data bits as 7, and the receiver can repair any one flipped bit without asking for it again.
- Which Side of Each Line (Medium, free): Locality-sensitive hashing gives similar vectors the same short code, so finding near-duplicates becomes a lookup.
- A Name Becomes a Number (Medium): Given a puzzle name, return a 32-bit seed.
- Biggest Power That Fits (Medium): Given n, return the biggest power of two that's n or less.
- How Many Bits Differ? (Medium): Given two numbers, return how many bit positions they differ in.
- Modular Exponentiation (Medium): Given base, exp, and mod, return base to the power exp, mod m, without the number blowing up.
- Multiply With Shifts (Medium): Given two numbers a and b, multiply them using only shifts and addition.
- Reverse Bits (Byte) (Medium): Given a number from 0 to 255, reverse the order of its 8 bits and return the result.
- Same Side of the Wall (Medium): Given two IPv4 addresses as four numbers each and a prefix length, return true if they're on the same subnet, meaning their first prefix-length bits match.
- Set Bits for Everyone (Medium): Given n, return an array with the number of 1 bits in every number from 0 to n.
- Subsets by Counting (Medium): Given a list, return every subset by counting from 0 to 2^n - 1.
- Swap Neighbouring Bits (Medium): Given a byte, swap every pair of neighboring bits and return the result.
- The Missing Number (Medium): Given n different numbers from 0 to n, with exactly one missing, return the missing one.
- Tiles That Know Their Neighbors (Medium): Autotiling picks each wall's picture from its neighbors, so walls join up into smooth shapes on their own.
- Undo a Gray Code (Medium): Given a Gray code value, return the normal binary number it stands for.
- XOR Everything to n (Medium): Given n, return 0 XOR 1 XOR 2 XOR ...
- A Curve That Never Jumps (Hard, free): A Hilbert curve visits every cell of a 2^order by 2^order grid exactly once, and each step moves to a touching cell.
- A Prime Test That Scales (Hard, free): Trial division is hopeless for the huge primes cryptography needs.
- A Running Total You Can Edit (Hard, free): A table of running totals answers any range sum at once, but changing one number means rebuilding it.
- Count by the Longest Run of Zeros (Hard, free): HyperLogLog counts distinct items using a few bytes, by noticing how rare a hash that starts with many zeros is.
- Addition Without Adding (Hard): Given two numbers, add them without using +.
- Morton Code (Z-Order) (Hard): Given x and y, return their Morton code by interleaving their bits: x's bits go in the even positions and y's bits go in the odd ones.
- The Gray Code Walk (Hard): Given n, return the n-bit Gray code sequence, where each number differs from the one before by exactly one bit.
- The One That Came Once (Hard): Given a list where every value appears three times except one, return the one that appears once.
- The Two That Came Once (Hard): Given a list where every value appears twice except two, return those two, smaller first.
Practice another pattern
Or browse by subject
Browse all 676 puzzles