Validate Configuration Register Layout

Code

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

typedef struct {
    uint16_t reg;
} ConfigRegister;
// Bit Position	Meaning
// 0	Enable (1 = ON)
// 1	Mode (0 = Normal, 1 = Safe)
// 2–3	Priority (00 = Low, 01 = Medium, 10 = High, 11 = Invalid)
// 4–15	Reserved (must be 0)
int validate_config(ConfigRegister *cfg) {
    // Write logic using pointer access
    int check_enable = (cfg->reg) & 0x01;
    int check_priority = (cfg->reg >> 2) & 0x03;
    int check_other = (cfg->reg) & 0xFFF0;
    
    if(check_enable == 1 && check_priority != 3 && check_other == 0) return 1;
    else return 0;
}


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