Bit Manipulation

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

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

. . .