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
    const int BAUD_RATE_START_BIT = 8;
    const int BAUD_RATE_FIELD_SIZE = 4;
 uint32_t clear_mask = ~(((1U << BAUD_RATE_FIELD_SIZE) - 1) << BAUD_RATE_START_BIT);
 uint32_t cleared_reg = reg & clear_mask;
uint32_t new_baud_shifted = ((uint32_t)baud & ((1U << BAUD_RATE_FIELD_SIZE) - 1)) << BAUD_RATE_START_BIT;
uint32_t result_reg = cleared_reg | new_baud_shifted;
return result_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