Validate Configuration Register Layout

Code

#include <stdio.h>
#include<stdint.h>
typedef struct {
    unsigned short reg;
} ConfigRegister;

int validate_config(ConfigRegister *cfg) {
    // Write logic using pointer access
    uint16_t value = cfg->reg;

    /* 1️⃣ Check Bit 0: Enable must be ON */
    if ((value & (1U << 0)) == 0)
        return 0;

    /* 2️⃣ Check Priority (Bits 2–3 must NOT be 11) */
    uint16_t priority = (value >> 2) & 0x03;
    if (priority == 0x03)
        return 0;

    /* 3️⃣ Check Reserved Bits (4–15 must be 0) */
    if ((value & 0xFFF0) != 0)
        return 0;

    /* All conditions satisfied */
    return 1;
}

int main() {
    ConfigRegister cfg;
    scanf("%hx", &cfg.reg);

    int result = validate_config(&cfg);
    printf("%d", result);

    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

0005

Expected Output

1