Page 1 of 1

Rotates 8 bits

Posted: Mon Jan 29, 2024 4:14 pm
by samtin
Hi,

How do I rotate 8 bits to the right or to the left by 1 bit?

1011 1110
0111 1101

Thank you!

Re: Rotates 8 bits

Posted: Mon Jan 29, 2024 5:39 pm
by mnf
You need to shift and then add the shifted 'out' bit back in.

so assuming 8 bit values (.x and .msb and .lsb)

.msb = .x & 0x80
.x = (.x << 1) | (.msb != 0) // Or use (.msb >> 7) not sure which is quicker?

Will do a left rotate - note you could probably cram it into one line and do without the extra variable)

.lsb = .x & 1
.x = (.x >> 1) | (.lsb << 7)

Will rotate 'right'

Martin

Re: Rotates 8 bits

Posted: Mon Jan 29, 2024 6:44 pm
by samtin
Hi Martin,

Thank you very much for the answer. :D

All the best!