All submissions

Macro-Based Register Config Helper

Code

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

// Define macros here
#define SET_ENABLE(x,y) (y? (x |= y) : (x &= y))
#define SET_MODE(x,y) {x &= ~(0b11 << 1);\
                        x |= y << 1;}
#define SET_SPEED(x,y) {x &= ~(0b111 << 3);\
                        x |= y << 3;}

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

    return temp;
}

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