Do binary arithmetic and bitwise operations, and convert between bases.
Binary is base-2: only digits 0 and 1. Each position is a power of 2. Binary arithmetic follows the same rules as decimal but carries happen at 2 (not 10). Bitwise operations work bit-by-bit on the binary representations: AND returns 1 only where both bits are 1; OR where at least one is 1; XOR where exactly one is 1; NOT flips all bits. Shifts multiply (<<) or divide (>>) by powers of 2.
Binary to decimal
d = Σ bᵢ × 2ⁱ (sum of bit values)
Bitwise identities
AND: 1 only if both 1. OR: 1 if either 1. XOR: 1 if exactly one 1.
255 = 11111111₂ — eight 1 bits, because 2⁸ − 1 = 255. It's also 0xFF in hexadecimal and 0o377 in octal. This is the maximum value of an unsigned 8-bit byte.
XOR (exclusive OR) outputs 1 only when the two input bits differ. It's used in cryptography (XOR with a key), error detection (parity), and the classic bit-swap trick (a^=b; b^=a; a^=b swaps two variables without a temporary).
Shifting bits left by n places is the same as multiplying by 2ⁿ. For example, 5 = 101₂; 5 << 1 = 1010₂ = 10 = 5 × 2. Right shift divides by 2ⁿ (integer division).