All submissions

Macro-Based Register Config Helper

Code

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

// Define macros here
#define SET_ENABLE_BIT(data, val)       ((data) | (val<<0))   
#define SET_MODE_BITS(data, val)       ((data) | (val<<1))
#define SET_SPEED_BITS(data, val)      ((data) | (val<<3))
#define SET_RESERVED_BITS(data, val)      ((data) | (val<<6)) 

uint16_t build_register(uint8_t enable, uint8_t mode, uint8_t speed) {
    // Use macros to set fields
    uint16_t data = 0;
    data = SET_ENABLE_BIT(data, enable);
    data = SET_MODE_BITS(data, mode);
    data = SET_SPEED_BITS(data, speed);
    data = SET_RESERVED_BITS(data, 0x00);

    return data;
}

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