Advertisement
STANAANDREY

k&r 2-8

Oct 23rd, 2022 (edited)
880
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.43 KB | None | 0 0
  1. #include <stdio.h>
  2. #define bits(x) sizeof(x) * 8
  3.  
  4. void showBits(unsigned x) {
  5.   for (int i = bits(x) - 1; i >= 0; i--) {
  6.     printf("%d", 1 & (x >> i));
  7.   }
  8.   puts("");
  9. }
  10.  
  11. unsigned rightrot(unsigned x, unsigned n) {
  12.   return (x >> n) | (x << (bits(x) - n));
  13. }
  14.  
  15. int main() {
  16.   unsigned x, n;
  17.   printf("x="); scanf("%u", &x);
  18.   printf("n="); scanf("%u", &n);
  19.   showBits(x);
  20.   showBits(rightrot(x, n));
  21.   return 0;
  22. }
  23.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement