All submissions

Validate Configuration Register Layout

Code

#include <stdio.h>

typedef struct {
    unsigned short reg;
} ConfigRegister;

int validate_config(ConfigRegister *cfg) {
    // Write logic using pointer access
    // check bit 0
    if((cfg->reg & 0x0001) == 0) return 0;
    // check priority(bits 2-3)
    unsigned char priority = (cfg->reg & 0x000C) >> 2;
    if(priority == 3) return 0;
    // Check reserved (bits 4 to 15)
    if((cfg->reg & 0xFFF0) != 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

/*
Plan:
- configReg to reg 
- 1st position will be 0 (check if this is one)
- move to position 2 and then collect till position 3 (should not be 11)
- move to position 4 and collect till position 15 (should be 0)
*/

 

 

Loading...

Input

0005

Expected Output

1