Decode Status Register into Human-Readable Flags

Code

#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", &reg);
    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.

Solving Approach

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

✅ 1️⃣ Direct Bitwise Checking Approach (Explicit Bit Checks)

#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");
}

🧠 What This Version Does

Each bit in the 8-bit register represents a specific hardware flag.

We:

  • Generate a mask using 1U << bit_position
  • Use bitwise AND to check if that bit is set
  • Print the corresponding flag if the result is non-zero

🔍 Breaking Down One Check

Example:

if(status_reg & Bit(3))

Step 1 — Create Mask

If bit = 3:

00000001 << 3
=
00001000

Step 2 — AND Operation

status_reg & 00001000

If result ≠ 0 → Bit is set
If result = 0 → Bit is not set

🧠 Why This Version Is Good

✔ Very clear
✔ Easy to understand
✔ Direct mapping of bit → flag
✔ Good for small fixed registers
✔ Deterministic execution

⚠ Limitation

  • Repetitive code
  • Not scalable for large registers
  • Harder to maintain if flags increase

✅ 2️⃣ Scalable Loop-Based Approach (More Professional)

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]);
    }
}

🧠 What This Version Does

Instead of writing multiple if statements:

  • Store flag names in an array
  • Loop from bit 0 to bit 7
  • Check each bit dynamically
  • Print corresponding string

🧠 Why This Version Is More Reusable

Because:

  • Easily extendable to 16-bit or 32-bit registers
  • Less repetitive code
  • Cleaner and maintainable
  • Preferred in real firmware projects

🔥 Comparison

FeatureDirect If VersionLoop Version
Beginner Friendly✅ YesMedium
Less Code❌ Slightly more✅ Less
More FlexibleMedium✅ High
Industry StyleGood⭐ Excellent
Easy to Extend❌ No✅ Yes

🧠 Which One Is Used in Real Firmware?

Professional embedded systems typically prefer the loop + array approach, because:

  • Cleaner
  • Flexible
  • Scalable
  • Easier to maintain
  • Used in driver-level code

🎯 Interview Insight

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 🔥

🚀 Final Takeaway

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

 

 

Upvote
Downvote
Loading...

Input

13

Expected Output

Power On Tx Ready Rx Ready