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) {
    // Create a Base Mask of 'len' 1s (e.g., len=3 -> 000...0111)
    uint32_t base_mask = (1<<4) - 1;

    // 2. Clear the bits at the target position
    // Shift the mask to the position, invert it (~), and AND.
    reg &= ~(base_mask << 8);

    reg |= (baud << 8);
    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