Validate Configuration Register Layout

Code

#include <stdio.h>
#include <stdint.h>
/* Enable (1=ON)
   Mode (0 = Normal, 1 = Safe)
   Priority(00 = Low, 01 = Medium, 10 = High, 11 = Invalid)
   Reserved(must be 0)
   return 1 if (enable on, bit2-3 valid, 4-15 are 0)
   All bits 4-15 are 0
*/

typedef struct{
    uint16_t enable:1; 
    uint16_t mode:1;
    uint16_t priority:2;
    uint16_t reserved:12; 
}RegisterControl; 

int valid_config(RegisterControl *ctrl){
    return (ctrl->enable == 1)&&(ctrl->priority != 3)&&(ctrl->reserved == 0); 
}

int main(){
    uint16_t x;
    scanf("%hx",&x);
    RegisterControl *reg = (RegisterControl*)&x; 
    printf("%d",valid_config(reg));
    return 0;
} 

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

0005

Expected Output

1