Math Fundamentals18 sections · 814 units
Open in Course

Bitwise Operations Reference

AND OR XOR NOT

Four bitwise operations you will use constantly: AND (&\&), OR (|), XOR (\wedge), and NOT (\sim). Each operates on individual bits.

AND returns 1 only when both bits are 1. OR returns 1 when at least one bit is 1. XOR returns 1 when bits differ. NOT flips all bits. Example: 1010&1100=10001010 \& 1100 = 1000, 10101100=11101010 | 1100 = 1110, 10101100=01101010 \wedge 1100 = 0110, 1010=0101\sim 1010 = 0101 (in 4-bit).

Shift operations: left shift (<<<<) multiplies by 2, right shift (>>>>) divides by 2 (for unsigned). 5<<1=105 << 1 = 10 (binary 101101 becomes 10101010). 10>>1=510 >> 1 = 5. These are faster than multiplication and division on most CPUs.