General knowledgeAsked by Anonymous23 Oct 2026, 5:05 AM ISTUpdated
In one line
A bit mask for a specific bit is a number with only that bit set to 1, and all others set to 0.
The rule
To create a bit mask for the nth bit, use: 1 << n
Here, 1 means the number 1 in binary, << means 'shift left', and n is the bit position (starting from 0).
The formula
1 << n
1the number 1 (binary: 00000001)
<<shift left operator
nbit position (starting from 0)
Let's understand it simply
Imagine you have a row of switches, each can be ON (1) or OFF (0). A bit mask is like a row where only one switch is ON, and the rest are OFF. If you want to check or change just one switch, you use a mask that points only to that one.
For example, if you want the 2nd switch ON (counting from 0), your mask will look like 00000100 in binary (which is 4 in decimal).
How it works
Start with the number 1. In binary, this is 00000001.
Decide which bit you want. For example, the 3rd bit (n = 3).
Shift the 1 left by n places using <<. So, 1 << 3 becomes 00001000 (which is 8).
This number is your bit mask. Only the nth bit is 1; all others are 0.
How to make a bit mask for bit n
Start with 1 (binary: 00000001)
Choose the bit position n (e.g., n = 3)
Shift 1 left by n: 1 << n (binary: 00001000)
This is your mask (only bit n is 1)
The story behind it
Bit masks are used in computers to work directly with the bits inside numbers. This idea became popular when computers had very little memory and needed fast ways to check or change just one bit. Programmers found that using bit masks made their code faster and more efficient.
Where you see it in real life
Checking if a cricket player is out: If each player has a bit, a mask can check just one player's status.
Turning on/off a kitchen appliance in a smart home: Each appliance is a bit; a mask controls just one.
Permissions in a mobile app: Each permission (camera, location) is a bit; masks check or set them.
Traffic lights control: Each light is a bit; a mask changes just one light.
LED display boards: Each LED is a bit; masks turn specific LEDs on or off.
Why it matters
Used in all programming languages (C, Java, Python) for fast, low-level operations.
Needed for hardware control, like sensors or robots.
Helps save memory and speed up code.
Important for competitive coding and technical job interviews.
Common mistakes
Counting bits from 1 instead of 0: Remember, the first bit is position 0.
Forgetting to use parentheses: In some languages, write (1 << n) to avoid errors.
Using the wrong operator: Use << (shift left), not >> (shift right).
Remember it
Think: "1 shifted left by n gives a mask for bit n."
Check yourself
What is the bit mask for the 2nd bit (n=2)?
1 << 2 = 4 (binary: 00000100)
Why do we use bit masks in programming?
To work with or check only one specific bit in a number.
If you want to set the 5th bit, what mask do you use?
1 << 5 = 32 (binary: 00100000)
What mistake happens if you use 1 << 1 for the first bit?
You get the second bit, not the first; bit positions start from 0.
Quick recap
A bit mask for bit n is made by shifting 1 left by n places.
Bit positions start from 0 (rightmost bit).
Bit masks help you work with just one bit in a number.