Learning path

Full curriculum

Full curriculum

Unit content

Bitwise operations and bit masks

Bitwise operations treat an integer as a fixed pattern of bits and operate on those bits directly.

Boolean operations on bits

AND, OR, XOR and NOT apply Boolean logic independently to corresponding bit positions.

A bit mask selects particular positions. For example, x & mask can extract selected flags, while x | mask can set them.

Shifts

A left shift moves bits toward more significant positions and inserts zeros on the right. For an unsigned value, shifting left by $k$ positions corresponds to multiplication by $2^k$ when no significant bits are lost.

A right shift moves bits in the opposite direction. The exact treatment of the sign bit for signed values depends on the representation and language rules.

Flags and packed fields

Low-level interfaces often pack several Boolean flags or small fields into one integer. Masks and shifts can test, set, clear and extract those fields without storing each one separately.

These operations are common in instruction encodings, device registers, permissions, protocols and performance-sensitive code.