Set Baud Rate Field in Control Register

Code

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

uint32_t set_baud_rate(uint32_t reg, uint8_t baud) {
    
    uint32_t mask = ~(((1<<4)-1)<<8);   // create a mask to only modify those bits
    reg &= mask;                        // clear the desired bits in the register
    reg |= (baud<<8);                   // set the masked bits to the requested baud
    return reg;
}

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