All submissions

Register Bitfields Using Struct Overlay

Code

#include <stdio.h>
#include <stdint.h>

typedef union {
    struct {
        uint8_t enable    : 1;
        uint8_t mode      : 2;
        uint8_t interrupt : 1;
        uint8_t reserved  : 4;
    } bits;
    uint8_t reg;
} ControlRegister;

int main() {
    uint8_t e, m, i;
    scanf("%hhu %hhu %hhu", &e, &m, &i);

    // Construct and print register value
    uint8_t reg=0;
    reg|=(e&0x1)<<0;
    reg|=(m & 0x3)<<1;
    reg|=(i & 0x1)<<3;
    reg|=(0 & 0xF);
    printf("%d",reg);
    return 0;
}

Solving Approach

 

simple bit shifting and resgietr concepts helped me to the problem 

 

Loading...

Input

1 2 1

Expected Output

13