When doing a shift whether left or right, I want to add back the lost bits with wraparound. How can I do this?
Asked
Active
Viewed 363 times
1
-
1a wraparound shift is called a [rotation](https://en.wikipedia.org/wiki/Bitwise_operation#Circular_shift), and there are no such thing as logical or arithmetic rotations – phuclv Nov 22 '21 at 06:07
-
duplicates: [Rotating bits in RISC-V](https://stackoverflow.com/q/57949406/995714), [How do I write rotation Operation for the Risc-V(Assembly Language) Do we have any command for it like we have have in 8086?](https://stackoverflow.com/q/55394123/995714) – phuclv Nov 22 '21 at 06:09
-
Does this answer your question? [Rotating bits in RISC-V](https://stackoverflow.com/questions/57949406/rotating-bits-in-risc-v) – phuclv Nov 22 '21 at 06:09
-
In short, shift part of the register to the right using `srl`, part of it to the left using `sll` and then combine the result using `or`. – Lindydancer Nov 22 '21 at 07:41
-
1Try it in C, which doesn't have these as primitive operators. The approach will be about rhe same. – Erik Eidt Nov 22 '21 at 12:31
-
Rotation operations were very popular back in the days when processors had few or only one register, so you could shift w/o loosing any bits. Nowadays with lots of registers, we usually just send the shifted result to a different register if the original value is still needed, instead of rotating. – Erik Eidt Nov 22 '21 at 14:41