Rotate Left in an 8-bit Register

Code

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

uint8_t rotate_left(uint8_t reg, uint8_t n) {
    // Ensure n is within the 0-7 range
    n = n % 8;
    
    // If n is 0, no rotation is needed
    if (n == 0) return reg;

    // Shift left and OR it with the bits shifted right (wrap around)
    return (reg << n) | (reg >> (8 - n));
}

int main() {
    uint8_t reg, n;
    // Using %hhu for 8-bit unsigned char (uint8_t)
    if (scanf("%hhu %hhu", &reg, &n) == 2) {
        printf("%u\n", rotate_left(reg, n));
    }
    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

176 1

Expected Output

97