16 bit barrel shift in Java
I'm trying to do a rotate right (barrel shift) on an int in Java, e.g.
Input: 0000 0000 开发者_运维技巧0110 1001
Output: 1000 0000 0011 0100
I know I can do a right shift (>>
), however I can't work out how to combine this to create a rotate (I'm pretty sure it's possible!).
I think there is a method in java.lang.Math
but I'm looking to work out how to use shifts only.
Any ideas?
I'm not sure there's a single operation for this. But something like:
int x = (x >> 1) | (x << 31) // or 15 if you really did mean 16-bit
would do the trick.
int rotated_by_one = ((value & 1)<<15) | (value >> 1)
精彩评论