Rotate Right in a 32-bit Register

 

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

uint32_t rotate_right(uint32_t reg, uint8_t n) {
    // Your code here
    //reg >> n shifts bits to the rigth
    reg = (reg >> n) | (reg << (32 - n)) & 0xFFFFFFFF; //reg << (32 - n) brings the bits back to the left side.
    // 0xFFFFFFFF is to make sure the reg is limited to 32 bits 
    return reg;
}

int main() {
    uint32_t reg;
    uint8_t n;
    scanf("%u %hhu", &reg, &n);
    printf("%u", rotate_right(reg, n));
    return 0;
}

 

 

 

 

Loading...

Input

2147483648 1

Expected Output

1073741824