Macro-Based Register Config Helper

Code

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

#define SET_ENABLE(REG)  (REG & 0x01) <<0
#define SET_MODE(REG)    (REG & 0x03) << 1
#define SET_SPEED(REG)   (REG & 0x07) << 3

uint16_t build_register(uint8_t enable, uint8_t mode, uint8_t speed) {
    uint16_t E = SET_ENABLE(enable);
    uint16_t M = SET_MODE(mode);
    uint16_t S = SET_SPEED(speed);
    uint16_t value = E | M |S ;
    return value;
}

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