Macro-Based Register Config Helper

Code

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

// Define macros here

// 0 or 1
#define SET_ENABLE(variable, bit) (variable |= bit)

// val should be between 0-3
#define SET_MODE(variable, val) (variable |= (val << 1))

// val should be between 0-7
#define SET_SPEED(variable, val) (variable |= (val << 3))

uint16_t build_register(uint8_t enable, uint8_t mode, uint8_t speed) {
    // Use macros to set fields
    uint16_t ret = 0;
    SET_ENABLE(ret, enable);
    SET_MODE(ret, mode);
    SET_SPEED(ret, speed);
    return ret;
}

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

 

 

 

Upvote
Downvote
Loading...

Input

1 2 4

Expected Output

37