Hint: XOR flips bits where mask is 1, calculate bitwise [OK]
Common Mistakes:
Adding values instead of XORing
Misreading binary bits
Confusing hex output format
4. The following code is intended to toggle bit 1 of status. What is the error?
unsigned int status = 0x01;
status = status & (1 << 1);
medium
A. The code should use OR operator to toggle bits.
B. The mask should be shifted by 2, not 1.
C. The variable should be unsigned char, not unsigned int.
D. Using AND instead of XOR will clear bits instead of toggling.
Solution
Step 1: Analyze the operator used
The code uses AND (&) with mask, which clears bits not set in mask instead of toggling.
Step 2: Identify correct operator for toggling
XOR (^) is the correct operator to toggle bits, not AND (&).
Final Answer:
Using AND instead of XOR will clear bits instead of toggling. -> Option D
Quick Check:
Toggle needs XOR, not AND [OK]
Hint: Toggle bits only with XOR, AND clears bits [OK]
Common Mistakes:
Confusing AND with XOR for toggling
Incorrect bit shift amount
Wrong data type assumption
5. You have a 8-bit register reg initially set to 0b10101010. You want to toggle bits 1, 3, and 5 simultaneously using XOR. Which mask should you use and what will be the final value of reg after toggling?
hard
A. Mask: 0b00101010; Final reg: 0b10000000
B. Mask: 0b01010101; Final reg: 0b11111111
C. Mask: 0b00101010; Final reg: 0b10000010
D. Mask: 0b00010101; Final reg: 0b10111111
Solution
Step 1: Create mask for bits 1, 3, and 5
Bits are zero-indexed from right. Bit 1 = 0b00000010, bit 3 = 0b00001000, bit 5 = 0b00100000. OR them: 0b00101010.
Step 2: XOR reg with mask
reg = 0b10101010 ^ 0b00101010 = 0b10000000.
Final Answer:
Mask: 0b00101010; Final reg: 0b10000000 -> Option A
Quick Check:
Toggle bits 1,3,5 with mask 0x2A = 0x80 [OK]
Hint: Combine bits with OR for mask, XOR to toggle all at once [OK]
Common Mistakes:
Wrong mask bits set
Miscounting bit positions
Incorrect XOR result calculation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.