All submissions

Validate Configuration Register Layout

Code

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

typedef struct {
    unsigned short reg;
} ConfigRegister;

int validate_config(ConfigRegister *cfg) {
    uint8_t valid_sum = 0; 

    if((cfg->reg & 1)){
        valid_sum++;
    }
    if(cfg->reg & (3 << 2) != 3){
        valid_sum++;
    }
    if((cfg->reg >> 4) == 0){
        valid_sum++;
    }

    return (valid_sum == 3) ? 1 : 0;
}

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

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

    return 0;
}

Solving Approach

 

 

 

Loading...

Input

0005

Expected Output

1