#include <stdio.h>
#include <stdint.h>
uint8_t rotate_one_right(uint8_t value)
{
unsigned saved_bit = value & 1; // Save the LSB
value >>= 1; // Shift right
value |= saved_bit << 3; // Make the saved bit the nibble MSB
return value;
}
int main(void)
{
uint8_t value = 0x08; // Set the high bit in the low nibble
printf("%02hhx\n", value
); // Will print 08 value = rotate_one_right(value);
printf("%02hhx\n", value
); // Will print 04 value = rotate_one_right(value);
printf("%02hhx\n", value
); // Will print 02 value = rotate_one_right(value);
printf("%02hhx\n", value
); // Will print 01 value = rotate_one_right(value);
printf("%02hhx\n", value
); // Will print 08 again
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRpbnQuaD4KCnVpbnQ4X3Qgcm90YXRlX29uZV9yaWdodCh1aW50OF90IHZhbHVlKQp7CiAgICB1bnNpZ25lZCBzYXZlZF9iaXQgPSB2YWx1ZSAmIDE7ICAvLyBTYXZlIHRoZSBMU0IKICAgIHZhbHVlID4+PSAxOyAgLy8gU2hpZnQgcmlnaHQKICAgIHZhbHVlIHw9IHNhdmVkX2JpdCA8PCAzOyAgLy8gTWFrZSB0aGUgc2F2ZWQgYml0IHRoZSBuaWJibGUgTVNCCiAgICByZXR1cm4gdmFsdWU7Cn0KCmludCBtYWluKHZvaWQpCnsKICAgIHVpbnQ4X3QgdmFsdWUgPSAweDA4OyAgLy8gU2V0IHRoZSBoaWdoIGJpdCBpbiB0aGUgbG93IG5pYmJsZQogICAgcHJpbnRmKCIlMDJoaHhcbiIsIHZhbHVlKTsgIC8vIFdpbGwgcHJpbnQgMDgKICAgIHZhbHVlID0gcm90YXRlX29uZV9yaWdodCh2YWx1ZSk7CiAgICBwcmludGYoIiUwMmhoeFxuIiwgdmFsdWUpOyAgLy8gV2lsbCBwcmludCAwNAogICAgdmFsdWUgPSByb3RhdGVfb25lX3JpZ2h0KHZhbHVlKTsKICAgIHByaW50ZigiJTAyaGh4XG4iLCB2YWx1ZSk7ICAvLyBXaWxsIHByaW50IDAyCiAgICB2YWx1ZSA9IHJvdGF0ZV9vbmVfcmlnaHQodmFsdWUpOwogICAgcHJpbnRmKCIlMDJoaHhcbiIsIHZhbHVlKTsgIC8vIFdpbGwgcHJpbnQgMDEKICAgIHZhbHVlID0gcm90YXRlX29uZV9yaWdodCh2YWx1ZSk7CiAgICBwcmludGYoIiUwMmhoeFxuIiwgdmFsdWUpOyAgLy8gV2lsbCBwcmludCAwOCBhZ2FpbgoKICAgIHJldHVybiAwOwp9Cg==