All submissions

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) {
    // Step 1: Create a mask for bits 8 to 11
    uint32_t mask = 0xF << 8;  // 0xF = 0b1111   
    // Step 2: Clear the baud rate field in reg
    reg &= ~mask;    
    // Step 3: Mask and shift the new baud value into position
    uint32_t baud_shifted = (baud & 0xF) << 8;
    // Step 4: Set the new baud rate
    return reg | baud_shifted;
}
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