Workspace Bit Masking
0d
Problem-solving patterns

Bit Masking

Select specific binary positions with shifts and a mask.

Recognition signal

The problem tests flags, powers of two or population count.

Method

Specify width, construct the mask and apply AND, OR or XOR.

Worked example

12 is 1100₂. Clearing its lowest set bit leaves 1000₂.

Common mistake

Shifting into a sign bit can change signed results or exceed a language’s defined range.