All submissions

Extract and Modify Field in a 32-bit Register

Code

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

int increment(int x) {
    int mask = 1;  // start with LSB
    while (x & mask) {   // while bit is 1
        x = x ^ mask;    // flip 1 to 0
        mask <<= 1;      // move carry left
    }
    x = x ^ mask;        // flip the first 0 to 1
    return x;
}


uint32_t update_register(uint32_t reg) {
    // Your logic here
    uint32_t extracted_bits = 0;

    extracted_bits = (reg & 0x7C00) >> 10;

    if (extracted_bits < 31) {
        extracted_bits = increment(extracted_bits);
    }
    

    reg  &= ~0x7C00;
    reg |= (extracted_bits << 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