Rotate Left in an 8-bit Register

Code

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

uint8_t rotate_left(uint8_t reg, uint8_t n) {
    if (!n) return reg;
    // Your code here
    n %= 8;
    uint16_t tmp = reg << n;
    uint8_t high = (tmp & 0xFF00) >> 8;
    switch (n) {
    case 1:
        high &= 0x1;
        break;
    case 2:
        high &= 0x3;
        break;
    case 3:
        high &= 0x7;
        break;
    case 4:
        high &= 0xF;
        break;
    case 5:
        high &= 0x1F;
        break;
    case 6:
        high &= 0x3F;
        break;
    case 7:
        high &= 0x7F;
        break;
    }
    return (uint8_t)(tmp | high);
}

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

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

176 1

Expected Output

97