All submissions

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
    // Step 1: create mask with the fields interested, and extract the field
    uint32_t mask = 0x1F << 10;
    uint32_t ext_field = (reg & mask) >> 10;

    //Step 2: check if it is less than 31 and increment by 1
    if(ext_field < 31){
        ext_field++;
    }

    //Step 3: clear the bits from 10 to 14
    reg &= ~mask;

    //Step 4: write the updated bits
    reg |= ext_field << 10;

    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