All submissions

Macro-Based Register Config Helper

Code

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

#define SET_ENABLE(reg, val)    ((reg) = ((reg) & ~0x0001U) | (((val) & 0x01U) << 0))
#define SET_MODE(reg, val)      ((reg) = ((reg) & ~0x0006U) | (((val) & 0x03U) << 1))
#define SET_SPEED(reg, val)     ((reg) = ((reg) & ~0x0038U) | (((val) & 0x07U) << 3))

uint16_t build_register(uint8_t enable, uint8_t mode, uint8_t speed) 
{
    uint16_t reg = 0;
    SET_ENABLE(reg, enable);
    SET_MODE(reg, mode);
    SET_SPEED(reg, speed);
    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