Hi,
How do I rotate 8 bits to the right or to the left by 1 bit?
1011 1110
0111 1101
Thank you!
Rotates 8 bits
-
- Valued Contributor
- Posts: 1208
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 440 times
Re: Rotates 8 bits
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
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