#include <stdio.h>
#include <stdint.h>
#define Bit(n) (1U << (n))
void decode_status(uint8_t status_reg) {
if(status_reg & Bit(0)) printf("Power On\n");
if(status_reg & Bit(1)) printf("Error\n");
if(status_reg & Bit(2)) printf("Tx Ready\n");
if(status_reg & Bit(3)) printf("Rx Ready\n");
if(status_reg & Bit(4)) printf("Overheat\n");
if(status_reg & Bit(5)) printf("Undervoltage\n");
if(status_reg & Bit(6)) printf("Timeout\n");
if(status_reg & Bit(7)) printf("Reserved\n");
}
int main() {
uint8_t reg;
scanf("%hhu", ®);
decode_status(reg);
return 0;
}Solving Approach
Perfect 🔥
Below is your submission-ready Solving Approach for the Decode Status Register into Human-Readable Flags problem, written cleanly and professionally — exactly in coding-platform format.
Perfect 🔥
Let’s explain this clearly and professionally — like in an interview.
We’ll go in this order:
1️⃣ Direct Bitwise Checking Approach
2️⃣ Scalable Loop-Based Approach
#define Bit(n) (1U << (n))
void decode_status(uint8_t status_reg) {
if(status_reg & Bit(0)) printf("Power On\n");
if(status_reg & Bit(1)) printf("Error\n");
if(status_reg & Bit(2)) printf("Tx Ready\n");
if(status_reg & Bit(3)) printf("Rx Ready\n");
if(status_reg & Bit(4)) printf("Overheat\n");
if(status_reg & Bit(5)) printf("Undervoltage\n");
if(status_reg & Bit(6)) printf("Timeout\n");
if(status_reg & Bit(7)) printf("Reserved\n");
}
Each bit in the 8-bit register represents a specific hardware flag.
We:
1U << bit_positionExample:
if(status_reg & Bit(3))
If bit = 3:
00000001 << 3
=
00001000
status_reg & 00001000
If result ≠ 0 → Bit is set
If result = 0 → Bit is not set
✔ Very clear
✔ Easy to understand
✔ Direct mapping of bit → flag
✔ Good for small fixed registers
✔ Deterministic execution
Now let’s look at a cleaner and scalable abstraction:
#define Bit(n) (1U << (n))
void decode_status(uint8_t status_reg) {
const char *flags[8] = {
"Power On",
"Error",
"Tx Ready",
"Rx Ready",
"Overheat",
"Undervoltage",
"Timeout",
"Reserved"
};
for(int i = 0; i < 8; i++) {
if(status_reg & Bit(i))
printf("%s\n", flags[i]);
}
}
Instead of writing multiple if statements:
Because:
| Feature | Direct If Version | Loop Version |
|---|---|---|
| Beginner Friendly | ✅ Yes | Medium |
| Less Code | ❌ Slightly more | ✅ Less |
| More Flexible | Medium | ✅ High |
| Industry Style | Good | ⭐ Excellent |
| Easy to Extend | ❌ No | ✅ Yes |
Professional embedded systems typically prefer the loop + array approach, because:
If asked:
Which approach would you prefer?
Strong answer:
I prefer the loop-based approach because it improves scalability and maintainability, especially when dealing with larger status registers or frequently changing flag definitions.
That’s a strong embedded firmware answer 🔥
Direct Bitwise Checking → Clear and educational
Loop-Based Version → Cleaner, scalable, production-ready
You’ve now demonstrated understanding of:
✔ Bit masking
✔ Flag decoding
✔ Register abstraction
✔ Maintainable embedded design
Input
13
Expected Output
Power On Tx Ready Rx Ready