📖 Scenario: Imagine you are working with a simple device that uses a number to represent different settings. Each setting is controlled by a single bit in this number. You want to learn how to change these bits to turn settings on, off, or switch them.
🎯 Goal: You will create a program that can set, clear, or toggle a specific bit in a number. This helps you control individual settings easily.
📋 What You'll Learn
Create an integer variable called number with the value 42.
Create an integer variable called bitPosition with the value 3.
Create an integer variable called mask that has a 1 only at the bitPosition.
Create three integer variables called setBit, clearBit, and toggleBit.
Use bitwise operations to set, clear, and toggle the bit at bitPosition in number.
Print the values of setBit, clearBit, and toggleBit.
💡 Why This Matters
🌍 Real World
Controlling individual settings or flags in devices, games, or software often uses bit manipulation to save space and increase speed.
💼 Career
Understanding bitwise operations is important for roles in embedded systems, game development, and performance-critical software engineering.
Progress0 / 4 steps
1
Create the initial number and bit position
Create an integer variable called number and set it to 42. Then create an integer variable called bitPosition and set it to 3.
DSA C#
Hint
Use int number = 42; and int bitPosition = 3; to create the variables.
2
Create a mask for the bit position
Create an integer variable called mask that has a 1 only at the bitPosition. Use the left shift operator (1 << bitPosition).
DSA C#
Hint
Use int mask = 1 << bitPosition; to create the mask.
3
Set, clear, and toggle the bit
Create three integer variables called setBit, clearBit, and toggleBit. Use bitwise OR | to set the bit, bitwise AND with NOT & ~ to clear the bit, and bitwise XOR ^ to toggle the bit in number using mask.
DSA C#
Hint
Use setBit = number | mask;, clearBit = number & ~mask;, and toggleBit = number ^ mask;.
4
Print the results
Print the values of setBit, clearBit, and toggleBit each on a new line using Console.WriteLine.
DSA C#
Hint
Use Console.WriteLine(setBit);, Console.WriteLine(clearBit);, and Console.WriteLine(toggleBit);.
Practice
(1/5)
1. What does the operation number |= (1 << position) do to the bit at position in number?
easy
A. Toggles the bit at position
B. Clears the bit at position to 0
C. Sets the bit at position to 1
D. Checks if the bit at position is set
Solution
Step 1: Understand the bitwise OR with shifted 1
The expression 1 << position creates a number with only the bit at position set to 1. Using OR (|=) with this sets that bit in number to 1 without changing other bits.
Step 2: Effect on the bit at position
Since OR with 1 always results in 1, the bit at position is set to 1 regardless of its previous value.
Final Answer:
Sets the bit at position to 1 -> Option C
Quick Check:
OR with 1 sets bit [OK]
Hint: Use OR with shifted 1 to set a bit [OK]
Common Mistakes:
Confusing OR with XOR for toggling
Thinking it clears the bit
Assuming it checks the bit instead
2. Which of the following C# statements correctly clears (sets to 0) the bit at position pos in an integer num?
easy
A. num &= ~(1 << pos);
B. num |= (1 << pos);
C. num ^= (1 << pos);
D. num = num << pos;
Solution
Step 1: Understand clearing a bit
To clear a bit, we use AND with the complement of a mask that has 1 at the target bit. The mask is 1 << pos, so complement is ~(1 << pos).
Step 2: Apply AND with complemented mask
Using num &= ~(1 << pos); clears the bit at pos while leaving others unchanged.
Final Answer:
num &= ~(1 << pos); -> Option A
Quick Check:
AND with complement clears bit [OK]
Hint: Use AND with NOT mask to clear a bit [OK]
Common Mistakes:
Using OR instead of AND to clear
Using XOR which toggles instead of clears
Using left shift alone which changes number
3. What is the output of the following C# code?
int num = 10; // binary 1010
int pos = 1;
num ^= (1 << pos);
Console.WriteLine(num);
medium
A. 10
B. 14
C. 12
D. 8
Solution
Step 1: Understand initial number and bit position
Number 10 in binary is 1010. Position 1 means the second bit from right (0-based). Bit at position 1 is 1.
Step 2: Apply XOR to toggle bit at position 1
XOR toggles the bit. Since bit at position 1 is 1, toggling it changes it to 0. New binary is 1000 which is decimal 8.