Bit Manipulation

In short: Bit manipulation uses operators like AND, OR, XOR, NOT, and bit shifts to work directly with the individual bits of a number. It enables fast, memory-efficient tricks for setting, clearing, toggling, and checking bits that show up in low-level and interview problems.

At a deep level, your computer works with bits—1s and 0s. But these get bundled up into more human readable things, like characters and lists. This is called abstraction.

While abstractions are nice, sometimes we want to work directly with bits. You can do this with bit manipulation, which involves bitwise operations.

Bitwise operations include AND, OR, XOR, NOT, and Bit Shifts.

AND, for example, takes two bits and returns 1 if both bits are 1. Otherwise, it returns 0. OR returns 1 if either of the bits are 1.

# AND 1 & 1 # gives 1 0 & 1 # gives 0 0010 & 0111 # gives 0010 # OR 0 | 1 # gives 1 0 | 0 # gives 0 1001 | 0100 # gives 1101

Frequently Asked Questions

What is bit manipulation?

Working directly with the binary digits of a number using bitwise operators (AND, OR, XOR, NOT) and shifts to set, clear, toggle, or test individual bits.

Why use bit manipulation?

It's fast and memory-efficient: you can pack flags into a single integer, do arithmetic with shifts, and solve certain problems in O(1) space.

What are common bit manipulation tricks?

Checking if a bit is set with n & (1 << i), setting a bit with n | (1 << i), toggling with XOR, and checking for a power of two with n & (n-1) == 0.

Last updated: June 17, 2026

What's next?

If you're ready to start applying these concepts to some problems, check out our mock coding interview questions.

They mimic a real interview by offering hints when you're stuck or you're missing an optimization.

Try some questions now

. . .