All submissions

Validate Configuration Register Layout

Code

#include <stdio.h>

typedef struct {
    unsigned short reg;
} ConfigRegister;

int validate_config(ConfigRegister *cfg) 
{
   char a=(cfg->reg)&1; //expected to be high for enabel bit.

   char b=(cfg->reg)>>2; //shifting right two times to access bit2 and bit3
   b=b&3; //performing AND operation to extract data of Bit2, Bit3.

   char c=(cfg->reg)>>4; //shifting right by 4 times to access 12bits from MSB.


   if( a && b<=2 && !c)
     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