fork(1) download
  1. #include <stdio.h>
  2. #include <stdint.h>
  3.  
  4. uint8_t rotate_one_right(uint8_t value)
  5. {
  6. unsigned saved_bit = value & 1; // Save the LSB
  7. value >>= 1; // Shift right
  8. value |= saved_bit << 3; // Make the saved bit the nibble MSB
  9. return value;
  10. }
  11.  
  12. int main(void)
  13. {
  14. uint8_t value = 0x08; // Set the high bit in the low nibble
  15. printf("%02hhx\n", value); // Will print 08
  16. value = rotate_one_right(value);
  17. printf("%02hhx\n", value); // Will print 04
  18. value = rotate_one_right(value);
  19. printf("%02hhx\n", value); // Will print 02
  20. value = rotate_one_right(value);
  21. printf("%02hhx\n", value); // Will print 01
  22. value = rotate_one_right(value);
  23. printf("%02hhx\n", value); // Will print 08 again
  24.  
  25. return 0;
  26. }
  27.  
Success #stdin #stdout 0s 2168KB
stdin
Standard input is empty
stdout
08
04
02
01
08