Extract and Modify Field in a 32-bit Register

Code

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

uint32_t update_register(uint32_t reg){
    // Extract the 10th bits to 14th bit
    uint32_t field = (reg >> 10) & 0x1F;
    
    //Check conditions
    if (field < 31) field += 1;

    reg &= ~(0x1F << 10); // Clear the bits 10 to 14
    reg |= (field << 10); // Set the updated field back
    return reg; 
}

int main(){
    uint32_t reg;
    scanf("%u", &reg);
    printf("%u", update_register(reg));
    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

15360

Expected Output

16384