All submissions

Macro-Based Register Config Helper

Code

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

// Define macros here
#define ENABLE_SHIFT  0
#define MODE_SHIFT    1
#define SPEED_SHIFT   3

uint16_t build_register(uint8_t enable, uint8_t mode, uint8_t speed) {
    // Use macros to set fields
    uint16_t ans = 0x0000;
    ans |= ((enable & 0x1)<<ENABLE_SHIFT);
    ans |= ((mode & 0x3)<<MODE_SHIFT);
    ans |= ((speed & 0x7)<<SPEED_SHIFT);
    return ans;
}

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

Define the macros to perform the shifting operation, not the masking operation

 

 

Loading...

Input

1 2 4

Expected Output

37