32. Rotate Right in a 32-bit Register

Back To All Submissions
Previous Submission
Next Submission

Code

// You are given an 8-bit register and a number of positions n.
//  Rotate the register to the left by n bit 
#include <stdio.h>
#include <stdint.h>

uint32_t rotate_register(uint32_t reg, uint8_t n){
    uint8_t back = 32 - n; 
    uint32_t right = (reg << back) & 0xFFFFFFFF; 
    reg = reg >> n; 
    reg =  right | reg; 
    return reg; 
}

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

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote