Macro-Based Register Config Helper

Code

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

// Define macros here
#define SET_BIT(data,val,pos,mask) \
        (data |= ((val & mask)<<pos))

uint16_t build_register(uint8_t enable, uint8_t mode, uint8_t speed) {
    // Use macros to set fields
    uint16_t data = 0;
    SET_BIT(data,enable,0,1);
    SET_BIT(data,mode,1,0x3);
    SET_BIT(data,speed,3,0x7);
    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

 

 

 

Upvote
Downvote
Loading...

Input

1 2 4

Expected Output

37