Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 循环旋转:左旋转问题_Java_Data Structures - Fatal编程技术网

Java 循环旋转:左旋转问题

Java 循环旋转:左旋转问题,java,data-structures,Java,Data Structures,在下面的代码中,我到底做错了什么?将数据向左旋转时,我得到了意外值。这有什么办法 public class RotateExample { public static byte rotateRight(byte bits, int shift) { return (byte)((bits >>> shift) | (bits << (8 - shift))); } public static byte rotateLeft(

在下面的代码中,我到底做错了什么?将数据向左旋转时,我得到了意外值。这有什么办法

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);
    }
}
public类循环示例{
公共静态字节旋转右(字节位,int移位){
返回(字节)((位>>>移位)|(位>(8移位));
} 
公共静态void main(字符串[]args){
//测试1失败
字节a=(字节)1;
字节b=旋转右(a,1);
字节c=旋转英尺(b,1);
系统输出打印项次(a+“”+b+“”+c);
//测试2通过
a=(字节)1;
b=旋转右(a,2);
c=旋转英尺(b,2);
系统输出打印项次(a+“”+b+“”+c);
//测试3失败
a=(字节)2;
b=旋转右(a,2);
c=旋转英尺(b,2);
系统输出打印项次(a+“”+b+“”+c);
//测试4通过
a=(字节)2;
b=旋转右(a,3);
c=旋转英尺(b,3);
系统输出打印项次(a+“”+b+“”+c);
}
}
以下工作

public static byte rotateRight(byte bits, int shift)
{
     return (byte)(((bits & 0xff)  >>> shift) | ((bits & 0xff) << (8 - shift)));
}
public static byte rotateLeft(byte bits, int shift)
{
    return (byte)(((bits & 0xff) << shift) | ((bits & 0xff) >>> (8 - shift)));
}
公共静态字节旋转右(字节位,int移位)
{
返回(字节)((位&0xff)>>>移位)|((位&0xff)>(8移位));
}
参考这个问题

之所以会发生这种情况,是因为在执行移位操作之前,字节被转换为有符号整数。

以下操作有效

public static byte rotateRight(byte bits, int shift)
{
     return (byte)(((bits & 0xff)  >>> shift) | ((bits & 0xff) << (8 - shift)));
}
public static byte rotateLeft(byte bits, int shift)
{
    return (byte)(((bits & 0xff) << shift) | ((bits & 0xff) >>> (8 - shift)));
}
公共静态字节旋转右(字节位,int移位)
{
返回(字节)((位&0xff)>>>移位)|((位&0xff)>(8移位));
}
参考这个问题


之所以会发生这种情况,是因为在执行移位操作之前,字节被转换为有符号整数。

我知道了。。。感谢您与我分享的其他讨论主题。谢谢!我不知道为什么,但它似乎有效:)我只需要在每个方法的第一行添加以下内容:
shift=shift%8
。我认为该方法假设
int
将在一个合理的范围内(0到8),我得到了它。。。感谢您与我分享的其他讨论主题。谢谢!我不知道为什么,但它似乎有效:)我只需要在每个方法的第一行添加以下内容:
shift=shift%8
。我认为该方法假设
int
将在一个合理的范围内(0到8)