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) {
    // Your code here
    // 1. Clear bits 8-11
    for (int i = 8; i < 12; i++) {
        reg = reg & ~(1 << i);
    }

    // printf("After clearing bits 8-11: ");
    // displayBits(reg);
    // printf( "\n" );

    // 2. Set the new baud rate value in bits 8-11
    reg = reg | ((baud & 0xF) << 8);  // Mask baud to 4 bits and shift to position 8

    // printf("After setting baud rate: ");
    // displayBits(reg);
    // printf( "\n" );

    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

Please read comments

 

 

Loading...

Input

0 10

Expected Output

2560