Skip to content

Bit operations / bit manipulations [mp]

Micro pattern

Description

Use masks, shifts, and bitwise identities to represent states or perform arithmetic compactly.

Problems

XOR cancellation and comparison

XOR removes equal pairs or identifies positions where two values differ.

Remove the lowest set bit

The expression \(n \mathbin{\&} (n-1)\) clears the least-significant set bit.

Shift and append bits

Open a bit position and append a bit from the source.

Bitwise arithmetic

Use shifts and masks for powers of two or fixed-width arithmetic.

Packed state

Store multiple Boolean conditions in different bits of an integer.