All submissions

Macro-Based Register Config Helper

Code

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

#define set_enable_bit(n) (n|1);
#define set_mode_field(n,val) (n|(val<<1)); //we are shifting it by 1 time because so that it exactly comes undet pos:1 and pso:2
#define set_speed_field(n,val) (n|(val<<3)); //similarly to get at pos:3,4,5, we need to shift by 3 times.

uint16_t build_register(uint8_t enable, uint8_t mode, uint8_t speed) 
{
    uint16_t ans=0x0000;
    if(enable==1)
      ans=set_enable_bit(ans);
    
    if(mode)
      ans=set_mode_field(ans,mode);

    if(speed)
      ans=set_speed_field(ans,speed);

    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

 

 

 

Loading...

Input

1 2 4

Expected Output

37