在下面的代码中,我到底在哪里做错了什么?将数据旋转回左侧时,我得到了意外的值。解决方法是什么?
public class RotateExample {
public static byte rotateRight(byte bits, int shift) {
return (byte)((bits >>> shift) | (bits << (8 - shift)));
}
public static byte rotateLeft(byte bits, int shift) {
return (byte)((bits << shift) | (bits >>> (8 - shift)));
}
public static void main(String[] args) {
//test 1 failed
byte a = (byte)1;
byte b = rotateRight(a,1);
byte c = rotateLeft(b,1);
System.out.println(a+" "+b+" "+c);
//test 2 passed
a = (byte)1;
b = rotateRight(a,2);
c = rotateLeft(b,2);
System.out.println(a+" "+b+" "+c);
//test 3 failed
a = (byte)2;
b = rotateRight(a,2);
c = rotateLeft(b,2);
System.out.println(a+" "+b+" "+c);
//test 4 passed
a = (byte)2;
b = rotateRight(a,3);
c = rotateLeft(b,3);
System.out.println(a+" "+b+" "+c);
}
}