Set Baud Rate Field in Control Register

Code

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

// The baud rate is controlled by 4 bits 
// located at position 8 (i.e., bits 8 to 11). 
uint32_t set_baud_rate(uint32_t reg, uint8_t baud){
    // Clear 4 bits at position 8
    reg &= ~(0xF << 8);
    //Set new baud rate 
    reg |= ((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

 

 

 

Upvote
Downvote
Loading...

Input

0 10

Expected Output

2560