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);

    ControlRegister new_reg;
    new_reg.bits.enable = e;
    new_reg.bits.mode = m;
    new_reg.bits.interrupt = i;

    printf("%d", new_reg);
}

Solving Approach

 

 

 

Loading...

Input

1 2 1

Expected Output

13