Advertisement
STANAANDREY

left rot bits

Jan 23rd, 2023
741
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3.  
  4. void printBin(uint8_t x) {
  5.   for (int i = sizeof(x) * 8 - 1; i >= 0; i--) {
  6.     printf("%d", 1 & (x >> i));
  7.     if (i % 4 == 0) {
  8.       putchar(' ');
  9.     }
  10.   }
  11.   puts("");
  12. }
  13.  
  14. int leftRot(uint8_t v, int n) {
  15.   return (v << n) | (v >> (sizeof(v) * 8 - n));
  16. }
  17.  
  18. int main(void) {
  19.   uint8_t v;
  20.   int n;
  21.   printf("v="); scanf("%hhu", &v);
  22.   printf("n="); scanf("%d", &n);
  23.   printBin(v);
  24.   printBin(leftRot(v, n));
  25.   return 0;
  26. }
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement