All submissions

Extract and Modify Field in a 32-bit Register

Code

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

#define MASK ((1U<<5)-1)
#define POS (10)
uint32_t update_register(uint32_t reg) {
    // pos 10-14
    // 5-bit field
    uint32_t temp = (reg >> POS) & MASK;
    if (temp < 31){
        ++temp;
    }

    reg &= ~(MASK<<POS);
    reg |= temp << POS;
    return reg;
}

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

Solving Approach

 

 

 

Loading...

Input

15360

Expected Output

16384