16. Set Baud Rate Field in Control Register

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

uint32_t set_baud_rate(uint32_t reg, uint8_t baud) {
    // Clear bits 8 to 11 (baud rate field)
    reg &= ~(0xF << 8);

    // Mask baud to 4 bits, shift it to position 8, and insert
    reg |= (baud & 0xF) << 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;
}

 

  • 0xF << 8 creates a mask for the baud rate field (4 bits at position 8).
  • The field is first cleared using &= ~mask.
  • The new baud value is inserted using bitwise OR after shifting to the correct position.

 

Loading...

Input

0 10

Expected Output

2560