How do you turn a specific bit on or off using a mask?
General knowledgeAsked by Anonymous23 Oct 2026, 11:14 AM ISTUpdated
In one line
You use a mask (a special number) with AND or OR to turn a specific bit on or off in another number.
The rule
To turn a bit ON, use OR (|) with a mask that has 1 at that bit's place. To turn a bit OFF, use AND (&) with a mask that has 0 at that bit's place (and 1s everywhere else).
Formulas:
Turn ON: result = number | mask
Turn OFF: result = number & ~mask
Where:
number = your original number
mask = a number with 1 only at the bit you want to change
~mask = NOT mask (flips all bits)
Let's understand it simply
Imagine you have a row of switches (like tube lights in your classroom). Each switch is a bit: ON (1) or OFF (0). Sometimes, you want to turn only one switch ON or OFF, without touching the others. A mask is like a glove with only one finger sticking out — it lets you touch just one switch at a time.
How it works
Decide which bit (switch) you want to change. For example, the 3rd bit from the right.
Make a mask: a number with 1 at that position, 0 elsewhere. (For the 3rd bit: 00000100 in binary, which is 4 in decimal.)
To turn ON: Use OR (|). This keeps all other bits the same, but sets your chosen bit to 1.
To turn OFF: First flip the mask (so your chosen bit is 0, others are 1). Then use AND (&). This keeps all other bits the same, but sets your chosen bit to 0.
Turning a bit ON or OFF with a mask
Decide which bit to change (e.g., 2nd bit)
Make a mask with 1 at that bit (e.g., 00000100)
To turn ON: Use OR (number | mask)
To turn OFF: Use AND with NOT mask (number & ~mask)
The story behind it
The idea of using masks comes from early computer hardware, where each bit controlled something (like a bulb or a switch). Engineers needed a fast way to change just one bit without disturbing the rest. Bitwise operations like AND, OR, and NOT were built into computers for this reason.
Where you see it in real life
Controlling LEDs: Each bit turns a light ON or OFF in a display board.
Permissions in files: Each bit can mean read, write, or execute permission.
Microcontroller pins: Turning a pin ON or OFF to control a fan or motor.
Cricket scoreboard: Each bit can represent a player's score light.
Games: Storing which levels are unlocked (each bit = one level).
Why it matters
Used in almost every electronic device to control settings.
Saves memory: one number can store many ON/OFF values.
Makes programs run faster, as computers handle bits very quickly.
Needed for competitive programming, coding interviews, and exams.
Worked example
Let's say you have the number 10 (in binary: 00001010). You want to turn ON the 2nd bit (counting from 0 on the right).
Given: number = 10 (00001010), bit to turn ON = 2
Step 1: Make mask = 1 << 2 = 4 (00000100)
Step 2: Use OR: result = 10 | 4 = 14 (00001110)
Step 3: Answer: Bit 2 is now ON in 14
Now, to turn OFF the 3rd bit (bit 3):
Given: number = 14 (00001110), bit to turn OFF = 3
Step 1: Make mask = 1 << 3 = 8 (00001000)
Step 2: Flip mask: ~8 = 11110111
Step 3: Use AND: result = 14 & (~8) = 6 (00000110)
Step 4: Answer: Bit 3 is now OFF in 6
Common mistakes
Forgetting to flip the mask when turning OFF: Always use ~mask for AND.
Counting bits from the wrong side: Bits are counted from the right (least significant bit is 0).
Using OR to turn OFF: OR only turns bits ON, never OFF.
Remember it
"OR to turn ON, AND with NOT to turn OFF."
Check yourself
If you want to turn ON the 4th bit of number 8, what mask do you use?
Use mask 16 (00010000 in binary) and OR it with 8.
Why do we use AND with NOT mask to turn a bit OFF?
Because AND with 0 turns the bit OFF, and NOT mask has 0 at the position you want to clear.
What happens if you use OR to try to turn a bit OFF?
It won't work; OR only sets bits to 1, never to 0.
Quick recap
A mask is a number with 1 at the bit you want to change.
Use OR (|) with a mask to turn a bit ON.
Use AND (&) with NOT mask (~mask) to turn a bit OFF.
Bits are counted from the right, starting at 0.
Masks let you change one bit without touching others.