#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", ®, &n); printf("%u", rotate_left(reg, n)); return 0; }
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
Test Cases
Test Results
Input
176 1
Expected Output
97