Welcome to Bit Manipulation — working directly with the 1s and 0s that computers actually use! 🔢
💡 Think of it like this: Computers store everything as binary (base 2). The number 5 in binary is 101, and 3 is 011. Bit manipulation lets us do super-fast operations on these binary digits.
Essential bit operations:
& AND — both bits must be 1: 5 & 3 = 101 & 011 = 001 = 1| OR — at least one bit is 1: 5 | 3 = 101 | 011 = 111 = 7^ XOR — bits must be different: 5 ^ 3 = 101 ^ 011 = 110 = 6~ NOT — flip all bits: ~5 = -6<< Left shift — multiply by 2: 3 << 1 = 6>> Right shift — divide by 2: 6 >> 1 = 3🌟 XOR Magic: XOR has a special property — a ^ a = 0 and a ^ 0 = a. This is the key to many bit problems!
5 & 3
1
5 | 3
7
5 ^ 3
6
result
4