简体   繁体   中英

16 bit barrel shift in Java

I'm trying to do a rotate right (barrel shift) on an int in Java, eg

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM