Validate Configuration Register Layout

Code

#include <stdio.h>

typedef struct {
    unsigned short reg;
} ConfigRegister;

int validate_config(ConfigRegister *cfg) {
    // Write logic using pointer access
    unsigned short val = cfg->reg; 
    unsigned short bit_2_3 = (val >> 2); 
    unsigned short bit_4_15 = (val >> 4); 
    // printf("%b \n", bit_2_3);
    if (val == 0){ 
        // printf("here \n");
        return 0; 
    }
    if ((bit_2_3 & 0x03) == 3){ 
        // printf("%b \n", bit_2_3);
        // printf("test %b \n", 0b10 & 0b11);
        // printf("here 2 \n");

        return 0; 
    }  

    if (bit_4_15 & 0xFFF){
        // printf("here 3\n");
        return 0; 
    }

    return 1; 
}

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