All submissions

Validate Configuration Register Layout

 

#include <stdio.h>

typedef struct {
    unsigned short reg;
} ConfigRegister;

int validate_config(ConfigRegister *cfg) {
    // Write logic using pointer access
    unsigned short reg_value = cfg->reg;

    // Check if Enable bit (bit 0) is set
    int is_enabled = (reg_value & 0x0001) != 0;

    // Check if Priority bits (2-3) are not the invalid '11' pattern (0x0C)
    int is_priority_valid = (reg_value & 0x000C) != 0x000C;

    // Check if Reserved bits (4-15) are all zero
    int are_reserved_zero = (reg_value & 0xFFF0) == 0;

    // Return 1 only if all conditions are true
    return is_enabled && is_priority_valid && are_reserved_zero;

}

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

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

    return 0;
}

 

 

 

 

Loading...

Input

0005

Expected Output

1