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) {
    // Clear the existing baud rate bits (8-11)
    // Create a mask with 0s at positions 8-11 and 1s elsewhere
    // 0xF is 1111 in binary. Left shift it by 8 positions to target bits 8-11.
    // Invert the mask with ~ to get 0s at bits 8-11 and 1s elsewhere.
    reg &= ~((uint32_t)0xF << 8); 

    // Set the new baud rate value
    // Mask the new baud rate to ensure it's a 4-bit value.
    // Shift it left by 8 positions to place it at the correct location (bits 8-11).
    // Use bitwise OR to set these bits in the register without affecting others.
    reg |= ((uint32_t)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;
}

Solving Approach

 

 

 

Loading...

Input

0 10

Expected Output

2560