Rotate Left in an 8-bit Register

Code

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

uint8_t rotate_left(uint8_t reg, uint8_t n) {
    // Your code here
    uint8_t count = 0;
    for(uint8_t i = 0; i < (sizeof(reg) * 8); i++)
    {
        if(((reg >> i) & 0x01) == 1)
        {
            count++;
        }
    }
    for(uint8_t i = 0; i < n; i++)
    {
        uint8_t buff_count = 0;
        reg <<= 1;
        for(uint8_t a = 0; a < (sizeof(reg) * 8); a++)
        {
            if(((reg >> a) & 0x01) == 1)
            {
                buff_count++;
            }
        }
        if(buff_count != count)
        {
            reg++;
        }
    }
    return reg;
}

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