Extract and Modify Field in a 32-bit Register

Code

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

uint32_t update_register(uint32_t reg) {
    int i;
    uint32_t a=0x1F,b;
    uint32_t r=reg;
    uint32_t res=0;
    for(i=10;i<=14;i++){
        res|=(1<<i);
    }
    r&=~(res);
    b=(reg>>10)&a;
    if(b>30){
        return reg;
    }
    else{
        b=b+1;
        r|=(b<<10);
        return r;
    }
}

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

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

15360

Expected Output

16384