All submissions

Macro-Based Register Config Helper

Code

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

// Define macros here

// shift to the position for each part 
#define SET_ENABLE(x) ((x & 0x1) << 0) //0x1 is 1 bit , shifted to pos 0 
#define SET_MODE(x)   ((x & 0x3) << 1) //0x3 is 2 bits, shifted to pos 1  
#define SET_SPEED(x)  ((x & 0x7) << 3) //0x7 is 3 bits, shifted to pos 3 

uint16_t build_register(uint8_t enable, uint8_t mode, uint8_t speed) {
    // Use macros to set fields 
    uint16_t reg = 0;

    reg |= SET_ENABLE(enable); 
    reg |= SET_MODE(mode); 
    reg |= SET_SPEED(speed);
    
    return reg;
}

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