How do you use a bit mask to check if a bit is set?
General knowledgeAsked by Anonymous23 Oct 2026, 11:22 AM ISTUpdated
In one line
You use a bit mask to check if a bit is set by doing an AND operation and seeing if the result is not zero.
The rule
To check if the k-th bit in a number is set, use a bit mask with only that bit as 1, and do a bitwise AND.
Formula: if (num & (1 << k)) != 0, then the k-th bit is set
num = your number
k = the bit position (starting from 0 for the rightmost bit)
& = bitwise AND operation
1 << k = a mask with only the k-th bit as 1
The formula
if (num & (1 << k)) != 0
numthe number you are checking
&bitwise AND operation
1 << kmask with only the k-th bit as 1
kthe bit position (starting from 0)
Let's understand it simply
Imagine you are checking if a switch is ON in a row of switches. Each switch is a bit. You want to check one switch, not all. A bit mask is like a cover with only one hole, showing just the switch you care about. If you shine a torch (do the AND operation) and see light (result is not zero), the switch is ON (bit is set).
How it works
Decide which bit you want to check (for example, 2nd bit from the right).
Make a mask with only that bit as 1: 1 << k.
Do a bitwise AND: num & mask.
If the result is not zero, the bit is set (ON). If it is zero, the bit is not set (OFF).
How to check if a bit is set using a bit mask
Choose the bit position k (start from 0 on the right)
Make the mask: 1 << k
Do AND: num & mask
If result is not zero, the bit is set
The story behind it
Bit masking became popular when computers started using binary numbers for everything. Early programmers needed a fast way to check and change single bits, especially in things like device control or games. Bit masks made this easy and quick, saving both time and memory.
Where you see it in real life
Checking permissions: In your phone, app permissions are stored as bits. Bit masks check if an app can use the camera.
Cricket scoreboard: Each light for runs/wickets can be a bit. Bit mask checks if a light should be ON.
Traffic lights: Each light (red, yellow, green) can be a bit. Bit mask checks which light is ON.
Games: To check if a player has a power-up (like a shield), a bit mask checks the right bit.
Why it matters
Used in programming for fast checks and changes.
Saves memory and makes code run faster.
Needed in hardware, games, and security systems.
Helps you understand how computers store and use information.
Worked example
Given: num = 13 (which is 1101 in binary), k = 2
Step 1: Make the mask mask = 1 << 2 = 4 (binary 0100)
Step 2: AND operation num & mask = 13 & 4 = 1101 & 0100 = 0100 (which is 4)
Step 3: Check result 4 is not zero, so the 2nd bit is set
Common mistakes
Counting bits from the left instead of the right (always start from the right, which is bit 0).
Forgetting that bit positions start at 0, not 1.
Using == 1 instead of != 0 to check the result (the result could be any power of 2, not just 1).
Remember it
Bit mask: AND with (1 << k), if result is not zero, the bit is set.
Check yourself
If num = 10 (1010 in binary), is the 1st bit set?
No, because 10 & (1 << 1) = 10 & 2 = 0.
Why do we use (1 << k) as the mask?
Because it makes only the k-th bit 1 and all others 0, so we check just that bit.
What happens if you check bit 3 in num = 8 (1000 in binary)?
The result is not zero (8 & 8 = 8), so bit 3 is set.
Quick recap
Bit positions start from 0 on the right.
A bit mask isolates just one bit using 1 << k.
AND operation with the mask checks if that bit is set.