AlgoScope

Set Bit

AlgorithmbeginnerBit Manipulation

OR with a mask that has a single 1 at bit i.

Decision · step 2 of 3Bit Manipulation: Set a bit that was 0
0011020314150617bit

The mask 1 shl 2 is 4, a single 1 at bit 2. Bit 2 of 178 is currently 0. OR with the mask forces that one bit to 1 and leaves the rest alone.

Open in the player →or start at step 2

What you will see

A one-hot mask lines up under n; OR turns the bit on.

How bit manipulation works →

Cost

BestO(1)
AverageO(1)
WorstO(1)
SpaceO(1)

How you work with it here

play it through, step one change at a time, run it on your own input, try operations in any order.

Screen readers: Each bit announces its position and value; each step announces the operation and the resulting number in binary and decimal.

Reduced motion: Bits flip with a crossfade; masks appear in place.

Before this

Leads to

Topics that need this one first.

Taught by the same lesson

Bit Manipulation covers these too, in the same run.