All submissions

Rotate Left in an 8-bit Register

Code

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

uint8_t rotate_left(uint8_t reg, uint8_t n) 
{
    uint8_t mask=(reg>>(8-n)) & ((1<<n)-1);

    return (reg<<n)|mask;

}

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

Solving Approach

1: We need to extract n bits from MSB, Hence we are shifting by 8-n times times.

2: Based on n value, we need to generate ((1<<n)-1), to extract information corresponding to those bits.

3: Implement leftshift operation on 

 

 

Loading...

Input

176 1

Expected Output

97