Challenge - 5 Problems
GPIO Port Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this GPIO port write operation?
Given a 8-bit GPIO port initially set to 0x00, what will be the port value after executing the following code?
Embedded C
uint8_t GPIO_PORT = 0x00; // Set pins 0, 2, and 4 high GPIO_PORT |= (1 << 0) | (1 << 2) | (1 << 4); printf("0x%02X", GPIO_PORT);
Attempts:
2 left
💡 Hint
Remember that (1 << n) sets bit n to 1. Combine bits with OR.
✗ Incorrect
Bits 0, 2, and 4 correspond to values 1, 4, and 16 respectively. ORing them gives 1 + 4 + 16 = 21 decimal, which is 0x15 in hex.
❓ Predict Output
intermediate2:00remaining
What is the value of GPIO_PORT after clearing bits?
Starting with GPIO_PORT = 0xFF, what is the value after clearing bits 1, 3, and 5 using this code?
Embedded C
uint8_t GPIO_PORT = 0xFF; // Clear bits 1, 3, and 5 GPIO_PORT &= ~((1 << 1) | (1 << 3) | (1 << 5)); printf("0x%02X", GPIO_PORT);
Attempts:
2 left
💡 Hint
Use bitwise AND with the negation of the bits to clear.
✗ Incorrect
Bits 1, 3, and 5 correspond to 0x02, 0x08, and 0x20. Clearing these bits from 0xFF (all ones) results in 0xFF & ~(0x2A) = 0xFF & 0xD5 = 0xAB.
🔧 Debug
advanced3:00remaining
Why does this GPIO port toggle code not work as expected?
This code is intended to toggle bits 0 and 7 of GPIO_PORT. What is the problem?
Embedded C
uint8_t GPIO_PORT = 0xAA; // 0b10101010 // Toggle bits 0 and 7 GPIO_PORT ^= 1 << 0 & 1 << 7; printf("0x%02X", GPIO_PORT);
Attempts:
2 left
💡 Hint
Check operator precedence between ^ and &.
✗ Incorrect
The expression '1 << 0 & 1 << 7' is evaluated as '(1 << 0) & (1 << 7)' due to operator precedence, but in C, bitwise AND (&) has higher precedence than bitwise XOR (^), so the expression is evaluated as '1 << (0 & 1) << 7', which is incorrect. The bitwise AND has higher precedence than XOR, so only bit 0 is toggled incorrectly.
❓ Predict Output
advanced2:30remaining
What is the output after setting and clearing multiple GPIO port bits?
Given GPIO_PORT initially 0x55, what is the value after this sequence?
Embedded C
uint8_t GPIO_PORT = 0x55; // 0b01010101 // Set bits 1 and 3 GPIO_PORT |= (1 << 1) | (1 << 3); // Clear bits 0 and 4 GPIO_PORT &= ~((1 << 0) | (1 << 4)); printf("0x%02X", GPIO_PORT);
Attempts:
2 left
💡 Hint
Apply set bits first, then clear bits.
✗ Incorrect
Starting with 0x55 (01010101), setting bits 1 and 3 sets bits 1 and 3 to 1, resulting in 0x5F (01011111). Clearing bits 0 and 4 clears bits 0 and 4, resulting in 0x5A (01011010).
🧠 Conceptual
expert3:00remaining
How many bits are set in GPIO_PORT after this operation?
If GPIO_PORT is 0xF0 and you execute GPIO_PORT ^= 0xFF;, how many bits are set (1) in GPIO_PORT afterwards?
Embedded C
uint8_t GPIO_PORT = 0xF0; GPIO_PORT ^= 0xFF; // Count bits set in GPIO_PORT
Attempts:
2 left
💡 Hint
XOR with 0xFF flips all bits.
✗ Incorrect
0xF0 is 11110000 in binary. XOR with 0xFF flips all bits, resulting in 00001111, which has 4 bits set.