Macro-Based Register Config Helper

Code

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

// Define macros here

uint16_t build_register(uint8_t enable, uint8_t mode, uint8_t speed) {
    // Use macros to set fields
    uint16_t reg=0;
    if(enable==1){
        reg|=0x0001;
    }
    else{
        reg|=0x0000;
    }


    if(mode==0){
        reg|=0x0000;
    }
    else if(mode==1){
        reg|=0x0002;
    }
    else if(mode==2){
        reg|=0x0004;
    }
    else{
        reg|=0x0006;
    }

    if(speed==0){
        reg|=0x0000;
    }
    else if(speed==1){
        reg|=0x008;
    }
    else if(speed==2){
        reg|=0x0010;
    }
    else if(speed==3){
        reg|=0x0018;
    }
    else if(speed==4){
        reg|=0x0020;
    }
    else if(speed==5){
        reg|=0x0028;
    }
    else if(speed==6){
        reg|=0x0030;
    }
    else{
        reg|=0x0038;
    }
    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

 

 

 

Upvote
Downvote
Loading...

Input

1 2 4

Expected Output

37