It compares each bit of two numbers and returns 1 only if both bits are 1; otherwise, it returns 0.
Click to reveal answer
beginner
How do you set (turn on) the nth bit of a number using bit manipulation?
Use the OR operator (|) with a mask that has only the nth bit set: number |= (1 << n);
Click to reveal answer
intermediate
What is the purpose of the bitwise XOR operator (^) in bit manipulation?
It returns 1 for bits that are different and 0 for bits that are the same. It can be used to toggle bits.
Click to reveal answer
beginner
How can you check if the nth bit of a number is set (1)?
Use the AND operator with a mask and check if the result is not zero: if (number & (1 << n)) { /* nth bit is set */ }
Click to reveal answer
beginner
What does the left shift operator (<<) do in bit manipulation?
It shifts all bits in a number to the left by a specified number of positions, adding zeros on the right. This multiplies the number by 2 for each shift.
Click to reveal answer
Which operator is used to clear (turn off) the nth bit of a number in C?
AAND (&) with the complement of (1 << n)
BOR (|) with (1 << n)
CXOR (^) with (1 << n)
DLeft shift (<<) by n
✗ Incorrect
To clear the nth bit, use AND with the mask that has all bits set except the nth bit: number &= ~(1 << n);
What is the result of 5 & 3 in binary?
A7
B6
C1
D0
✗ Incorrect
5 in binary is 0101, 3 is 0011; AND gives 0001 which is 1.
How do you toggle the 2nd bit of a number?
Anumber ^= (1 << 2);
Bnumber &= ~(1 << 2);
Cnumber |= (1 << 2);
Dnumber << 2;
✗ Incorrect
XOR (^) with (1 << 2) flips the 2nd bit.
What does the expression (number >> 1) do?
ASets the 1st bit of number
BShifts bits of number to the right by 1, dividing by 2
CShifts bits of number to the left by 1, multiplying by 2
DClears the 1st bit of number
✗ Incorrect
Right shift by 1 moves bits right, effectively dividing by 2.
Which operator can be used to check if a number is even or odd?
ALeft shift (<<) by 1
BOR (|) with 1
CXOR (^) with 1
DAND (&) with 1
✗ Incorrect
Checking number & 1 returns 0 if even, 1 if odd.
Explain how to set, clear, and toggle a specific bit in a number using bit manipulation.
Think about how masks with a single bit set can change bits using AND, OR, XOR.
You got /3 concepts.
Describe how left shift and right shift operators affect a number's value and bits.
Consider how shifting bits changes the number in binary.
You got /3 concepts.
Practice
(1/5)
1. What does the bitwise AND operator & do when applied between two bits?
easy
A. Returns 1 if either bit is 1
B. Returns 0 always
C. Flips the bits
D. Returns 1 only if both bits are 1
Solution
Step 1: Understand bitwise AND operation
The bitwise AND operator compares each bit of two numbers and returns 1 only if both bits are 1.
Step 2: Apply to bits
For example, 1 & 1 = 1, but 1 & 0 = 0 and 0 & 0 = 0.
Final Answer:
Returns 1 only if both bits are 1 -> Option D
Quick Check:
AND = 1 if both bits 1 [OK]
Hint: AND returns 1 only when both bits are 1 [OK]
Common Mistakes:
Confusing AND with OR operator
Thinking AND flips bits
Assuming AND always returns 1
2. Which of the following is the correct syntax to set the 3rd bit (0-indexed) of an unsigned int variable num in C?
easy
A. num = num | (1 << 3);
B. num = num & (1 << 3);
C. num = num ^ (1 << 3);
D. num = num << 3;
Solution
Step 1: Understand setting a bit
To set a bit means to make it 1 without changing other bits. We use OR with a mask that has 1 at the target bit.
Step 2: Use bit shift and OR
The mask is created by shifting 1 left by 3 (1 << 3). OR-ing num with this mask sets the 3rd bit.
Final Answer:
num = num | (1 << 3); -> Option A
Quick Check:
Set bit = OR with shifted 1 [OK]
Hint: Use OR with (1 << bit_position) to set a bit [OK]
Common Mistakes:
Using AND instead of OR to set bits
Using XOR which toggles bits
Shifting num instead of 1
3. What is the output of the following C code?
unsigned int x = 5; // binary 0101
x = x ^ (1 << 2);
printf("%u", x);
medium
A. 4
B. 1
C. 5
D. 7
Solution
Step 1: Understand initial value and mask
x = 5 in binary is 0101. The mask is (1 << 2) = 0100 (decimal 4).
Step 2: Apply XOR operation
XOR toggles bits where mask has 1. 0101 ^ 0100 = 1101 (decimal 7). Let's do bitwise XOR: Bits (3 2 1 0): 5 = 0 1 0 1 Mask = 0 1 0 0 XOR = 0 0 0 1 is incorrect. Actually, bits are: Bit 3: 0 ^ 0 = 0 Bit 2: 1 ^ 1 = 0 Bit 1: 0 ^ 0 = 0 Bit 0: 1 ^ 0 = 1 Result: 0001 (decimal 1) is incorrect for toggling bit 2. Wait, bit 2 is the third bit from right (0-indexed). So bits are: Bit 0: least significant bit Bit 1: next Bit 2: next So 5 (0101) bits: Bit 3: 0 Bit 2: 1 Bit 1: 0 Bit 0: 1 XOR with mask 0100: Bit 3: 0 ^ 0 = 0 Bit 2: 1 ^ 1 = 0 Bit 1: 0 ^ 0 = 0 Bit 0: 1 ^ 0 = 1 Result: 0001 (decimal 1). So output is 1. Therefore, correct answer is 1 -> 1.
Final Answer:
1 -> Option B
Quick Check:
XOR toggles bit 2: 5 ^ 4 = 1 [OK]
Hint: XOR toggles bits where mask is 1 [OK]
Common Mistakes:
Miscounting bit positions (0-indexed from right)
Confusing XOR with OR or AND
Incorrect binary to decimal conversion
4. Identify the error in this C code snippet that tries to clear the 1st bit of num:
unsigned int num = 14; // binary 1110
num = num & (1 << 1);
printf("%u", num);
medium
A. Mask should be inverted before AND
B. Should use OR instead of AND
C. Bit shift is incorrect, should be (1 << 0)
D. No error, code is correct
Solution
Step 1: Understand clearing a bit
To clear a bit, we AND with a mask that has 0 at the target bit and 1s elsewhere.
Step 2: Analyze given mask
The mask (1 << 1) is 00010 (binary), which has 1 only at bit 1. AND-ing num with this mask keeps only bit 1 and clears others, not what we want.
Step 3: Correct mask usage
We should invert the mask: ~ (1 << 1), so bit 1 is 0 and others 1, then AND with num to clear bit 1.
Final Answer:
Mask should be inverted before AND -> Option A
Quick Check:
Clear bit = AND with inverted mask [OK]
Hint: Clear bit by AND with ~(1 << bit_position) [OK]
Common Mistakes:
Using mask without inversion to clear bits
Using OR instead of AND to clear bits
Confusing bit positions
5. You want to check if the 4th bit (0-indexed) of an unsigned int flags is set (1). Which code snippet correctly does this and prints "Set" if true, otherwise "Not set"?
hard
A. if ((flags ^ (1 << 4)) == 1) printf("Set"); else printf("Not set");
B. if ((flags | (1 << 4)) == 0) printf("Set"); else printf("Not set");
C. if ((flags & (1 << 4)) != 0) printf("Set"); else printf("Not set");
D. if ((flags & (1 << 3)) != 0) printf("Set"); else printf("Not set");
Solution
Step 1: Understand checking a bit
To check if a bit is set, AND the number with a mask having 1 at that bit. If result is not zero, bit is set.
Step 2: Analyze options
if ((flags & (1 << 4)) != 0) printf("Set"); else printf("Not set"); uses (flags & (1 << 4)) != 0 which correctly checks the 4th bit. if ((flags | (1 << 4)) == 0) printf("Set"); else printf("Not set"); uses OR and compares to zero, which is incorrect. if ((flags ^ (1 << 4)) == 1) printf("Set"); else printf("Not set"); uses XOR and compares to 1, which is incorrect. if ((flags & (1 << 3)) != 0) printf("Set"); else printf("Not set"); checks bit 3 instead of 4, so wrong bit.
Final Answer:
if ((flags & (1 << 4)) != 0) printf("Set"); else printf("Not set"); -> Option C
Quick Check:
Check bit = AND with mask != 0 [OK]
Hint: Check bit with AND and compare to zero [OK]
Common Mistakes:
Using OR or XOR to check bits
Checking wrong bit position
Comparing AND result incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.