All submissions

Macro-Based Register Config Helper

Code

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

// Bit positions
#define ENABLE_POS   0
#define MODE_POS     1
#define SPEED_POS    3

// Masks
#define ENABLE_MASK  (0x1u << ENABLE_POS)
#define MODE_MASK    (0x3u << MODE_POS)   // 2 bits
#define SPEED_MASK   (0x7u << SPEED_POS)  // 3 bits

// Macros to set fields
#define SET_ENABLE(val)   (((val) & 0x1u) << ENABLE_POS)
#define SET_MODE(val)     (((val) & 0x3u) << MODE_POS)
#define SET_SPEED(val)    (((val) & 0x7u) << SPEED_POS)

uint16_t build_register(uint8_t enable, uint8_t mode, uint8_t speed) {
    uint16_t reg = 0;
    reg |= SET_ENABLE(enable);
    reg |= SET_MODE(mode);
    reg |= SET_SPEED(speed);
    // RESERVED (bits 6–7) automatically stay 0
    return reg;
}

int main() {
    uint8_t enable, mode, speed;
    scanf("%hhu %hhu %hhu", &enable, &mode, &speed);

    uint16_t reg = build_register(enable, mode, speed);
    printf("%u", reg);
    return 0;
}

Solving Approach

 

 

 

Loading...

Input

1 2 4

Expected Output

37