All submissions

Validate Configuration Register Layout

Code

#include <stdio.h>
#include <stdbool.h>

#define MASK(BITS) ((1U << BITS)-1)
#define GET_BITS(REG, POS, BITS) \
(((REG) >> (POS)) & MASK(BITS))

typedef struct {
    unsigned short reg;
} ConfigRegister;

int validate_config(ConfigRegister *cfg) {
    // Write logic using pointer access
    bool is_valid_enable = GET_BITS(cfg->reg, 0, 1) == 1;
    bool is_valid_priority = GET_BITS(cfg->reg, 2, 2) != 3;
    bool is_valid_reserve = GET_BITS(cfg->reg, 4, 12) == 0;
    if(is_valid_enable && is_valid_priority && is_valid_reserve)
    {
        return 1;
    }
    return 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