32. Rotate Right in a 32-bit Register

Back To All Submissions
Previous Submission
Next Submission

Code

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

uint32_t rotate_right(uint32_t reg, uint8_t n) {
    uint32_t lsb = 0;

    while(n--) {
    lsb = reg & 0x1;

    reg = reg >> 1;

    reg |= lsb << 31;

    }
    // while(n)
    // {
    //     if((temp >> 1) & 0x1) {
    //         reg = reg >> 1;
    //         reg |= (1 << 31);
    //     }else {
    //         reg = reg >> 1;
    //         reg &= ~(1<<31);
    //     }
    //     n--;
    // }

    // Your code here
    return reg;
}

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

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote