Validate Configuration Register Layout

Code

#include <stdio.h>
#include<stdint.h>

typedef struct {
    unsigned short reg;
} ConfigRegister;

int validate_config(ConfigRegister *cfg) {
    uint16_t res = cfg->reg;

    // Bit 0: Enable
    if ((res & 1) == 0)
        return 0;

    // Bits 2–3: Priority must NOT be 11
    if (((res >> 2) & 0x3) == 0x3)
        return 0;

    // Bits 4–15 must be zero
    if (((res >> 4) & 0xFFF) != 0)
        return 0;

    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