fork download
  1. #include <stdio.h>
  2.  
  3. unsigned int rightrot(unsigned int x, unsigned int n)
  4. {
  5. unsigned int i, rmb;
  6.  
  7. for(i = 0; i < n; ++i)
  8. {
  9. // get right-most bit
  10. rmb = x & 1;
  11.  
  12. // shift 1 to right
  13. x = x >> 1;
  14.  
  15. // if right-most bit is set, set left-most bit
  16.  
  17. if (rmb == 1)
  18. x = x | (~0u ^ (~0u >> 1) );
  19. }
  20.  
  21. return x;
  22. }
  23.  
  24. int main(void) {
  25. printf("%u\n", rightrot(122, 2));
  26. return 0;
  27. }
  28.  
Success #stdin #stdout 0s 4764KB
stdin
Standard input is empty
stdout
2147483678