What does it mean to set a specific bit in a number?
Setting a bit means changing that bit to 1, regardless of its current value.
Click to reveal answer
beginner
How do you clear a specific bit in a number?
Clearing a bit means changing that bit to 0, regardless of its current value.
Click to reveal answer
beginner
What does it mean to toggle a specific bit in a number?
Toggling a bit means changing it from 0 to 1 or from 1 to 0 (flipping its value).
Click to reveal answer
intermediate
In C#, how do you set the 3rd bit (0-based) of an integer variable num?
Use num = num | (1 << 3); which shifts 1 left by 3 and ORs it with num to set the bit.
Click to reveal answer
intermediate
How do you clear the 2nd bit of an integer num in C#?
Use num = num & ~(1 << 2); which shifts 1 left by 2, negates it, and ANDs with num to clear the bit.
Click to reveal answer
Which operator is used to set a specific bit in C#?
ABitwise OR (|)
BBitwise AND (&)
CBitwise XOR (^)
DBitwise NOT (~)
✗ Incorrect
Setting a bit uses OR (|) to ensure the bit is 1 without changing others.
What does the expression 1 << 4 do?
ASets the 4th bit to 1
BShifts 1 left by 4 bits, resulting in 16
CClears the 4th bit
DToggles the 4th bit
✗ Incorrect
It shifts the number 1 left by 4 bits, which equals 16 in decimal.
How do you toggle the 1st bit of a number num in C#?
Anum = ~num;
Bnum = num & ~(1 << 1);
Cnum = num | (1 << 1);
Dnum = num ^ (1 << 1);
✗ Incorrect
XOR (^) flips the bit at position 1, toggling it.
What is the result of clearing a bit that is already 0?
AThe bit becomes 1
BThe entire number becomes 0
CThe bit remains 0
DAn error occurs
✗ Incorrect
Clearing a bit that is already 0 keeps it 0 without changing other bits.
Which operator is used to clear a bit in C#?
ABitwise AND (&) with negation
BBitwise OR (|)
CBitwise XOR (^)
DBitwise NOT (~)
✗ Incorrect
Clearing a bit uses AND (&) with the negated mask to set that bit to 0.
Explain how to set, clear, and toggle a specific bit in an integer using bitwise operators.
Think about how each operator affects bits at the target position.
You got /3 concepts.
Describe what happens when you toggle a bit twice in a row.
Toggling twice cancels out the change.
You got /3 concepts.
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.