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
    // 0000 0000 0000 0000 0111 1100 0000 0000
    uint32_t pre = 0x00007C00;
    uint32_t kq = reg;
    kq = kq & pre;
    reg = reg & ~ pre;
    kq = (kq>>10);
    if(kq < 31){
        // kq = kq | 0x0000001 ; // này là cộng bitwise nó sẽ 1 + 1 = 1
        kq = kq + 1;// Nếu muốn 1 + 1 = 10 thì cộng cách này
        kq = (kq<<10);
        reg = reg | kq;
    }else if(kq >= 31){
        kq = (kq<<10);
        reg = reg | kq;
    }
    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