Set Baud Rate Field in Control Register

Code

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

uint32_t replace_field(uint32_t reg, uint32_t val, uint8_t pos, uint8_t len) {
    // Your code here
     for(uint8_t index = 0; index <= len-1; index++)
    {
        if(0 < (val & (0x01 << index)))
        {
            reg |= (1 << (pos+index));
        }
        else
        {
           reg &= ~(1 << (pos + index)); 
        }
        
    }
    return reg;
}

uint32_t set_baud_rate(uint32_t reg, uint8_t baud) {
    
    return replace_field(reg, baud, 8, 4);
}

int main() {
    uint32_t reg;
    uint8_t baud;
    scanf("%u %hhu", &reg, &baud);
    printf("%u", set_baud_rate(reg, baud));
    return 0;
}

Solving Approach

 

 

 

Loading...

Input

0 10

Expected Output

2560