0
0
Embedded Cprogramming~20 mins

GPIO port-wide operations in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
GPIO Port Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
A0x15
B0x1A
C0x14
D0x16
Attempts:
2 left
💡 Hint
Remember that (1 << n) sets bit n to 1. Combine bits with OR.
Predict Output
intermediate
2: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);
A0xAB
B0xD5
C0xBF
D0x7F
Attempts:
2 left
💡 Hint
Use bitwise AND with the negation of the bits to clear.
🔧 Debug
advanced
3: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);
AThe code toggles all bits instead of just bits 0 and 7.
BThe bitwise AND has higher precedence than XOR, so only bit 0 is toggled incorrectly.
CThe code causes a syntax error due to missing parentheses.
DThe code toggles bits 0 and 7 correctly, output is 0x2A.
Attempts:
2 left
💡 Hint
Check operator precedence between ^ and &.
Predict Output
advanced
2: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);
A0x6B
B0x4A
C0x5A
D0x7B
Attempts:
2 left
💡 Hint
Apply set bits first, then clear bits.
🧠 Conceptual
expert
3: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
A6
B8
C0
D4
Attempts:
2 left
💡 Hint
XOR with 0xFF flips all bits.