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) {
    // Your code here
    const uint32_t pos = 8U;
    const uint32_t len = 4U;

    uint32_t field_mask =((1U << len) - 1U) << pos;

    reg &= ~field_mask;
    reg |= ((baud & 0xFU) << pos);

    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

 

 

 

Upvote
Downvote
Loading...

Input

0 10

Expected Output

2560