Extract and Modify Field in a 32-bit Register

Code

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

uint32_t update_register(uint32_t reg) {
    // Your logic here

    // Extract the 5 bit bitfield (bits 10-14):
    uint8_t tempreg = (reg & 0x7C00) >> 10;

    // Increment if extracted bitfield is less than 31
    if(tempreg < 31){
        tempreg += 1;
    }

    // Clear bits 10-14 in reg:
    reg &= ~(0x7C00);

    // OR the updated tempreg into reg at previous position:
    reg |= (tempreg << 10);

    return reg;
}

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