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 = 0x0F << 8;
    reg &= ~mask;
    reg |= ((baud & 0x0F) << 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

  • Identify which bits to modify (bits 8–11).
  • Create a mask for that field (0xF << 8).
  • Clear those bits in the register (reg &= ~mask).
  • Insert the new baud value shifted into position.
  • Return the updated register.

 

 

Upvote
Downvote
Loading...

Input

0 10

Expected Output

2560