Rotate Left in an 8-bit Register

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>

uint8_t rotate_register(uint8_t reg, uint8_t n){
    uint8_t back = (n > 8) ? (8 - n + 8) : (8 - n);
    uint8_t right = (reg >> back) & 0xFF; 
    reg = reg << n; 
    reg = reg | right; 
    return reg; 
}

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

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

176 1

Expected Output

97