Set Baud Rate Field in Control Register

Code

#include <stdio.h>
#include <stdint.h>

#define BAUD_SIZE       ((1 << 4) - 1)
#define BAUD_POS        8
#define BAUD_MASK       (BAUD_SIZE << BAUD_POS) 


uint32_t set_baud_rate(uint32_t reg, uint8_t baud) {
    // Your code here
    reg &= ~(BAUD_MASK);// clear reg positions
    reg |= (baud & BAUD_SIZE) << BAUD_POS; // set new value in the cleared positions
    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

 

 

 

Upvote
Downvote
Loading...

Input

0 10

Expected Output

2560