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
    const uint32_t MASK  = 0x1F << 10;     // bits 10–14
    const uint32_t SHIFT = 10;

    // 1. Extract field (normalize to 0–31)
    uint32_t field = (reg & MASK) >> SHIFT;

    // 2. If < 31, increment
    if (field < 31) {
        field++;
    }

    // 3. Clear old field bits
    reg &= ~MASK;

    // 4. Write updated field back
    reg |= (field & 0x1F) << SHIFT;

    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