31. Rotate Left in an 8-bit Register

Back To All Submissions
Previous Submission
Next Submission

Code

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

void rotate_(uint8_t* reg, uint8_t n) {
	uint8_t mask = (1UL << n) - 1;
	uint8_t val = ((*reg) >> (8 - n)) & mask;
	*reg <<= n;
	*reg |= val;
}

void rotate(uint8_t* reg, uint8_t n) {
    // n = n % 8; // Đảm bảo an toàn nếu n >= 8
    *reg = (*reg << n) | (*reg >> (8 - n));
}

int main() {
	uint8_t reg, n;
	scanf("%hhu%hhu", &reg, &n);
	rotate(&reg, n);
	printf("%hhu", reg);
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote