Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.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_Bytearray_Bit Manipulation - Fatal编程技术网

Java按位运算符未按预期工作

Java按位运算符未按预期工作,java,bytearray,bit-manipulation,Java,Bytearray,Bit Manipulation,下面的代码尝试在字节数组中存储4个long。随机访问很重要,这就是我不使用字节流的原因。为什么下面的代码不起作用?你能想出一个更有效的方法吗 public static void storeLong(long value, byte[] buf, int offset) { buf[offset] = (byte) (value & 0xFFL); buf[offset+1] = (byte) ((value >>> 8) & 0xFFL);

下面的代码尝试在字节数组中存储4个long。随机访问很重要,这就是我不使用字节流的原因。为什么下面的代码不起作用?你能想出一个更有效的方法吗

public static void storeLong(long value, byte[] buf, int offset) {
    buf[offset] = (byte) (value & 0xFFL);
    buf[offset+1] = (byte) ((value >>> 8) & 0xFFL);
    buf[offset+2] = (byte) ((value >>> 16) & 0xFFL);
    buf[offset+3] = (byte) ((value >>> 24) & 0xFFL);
    buf[offset+4] = (byte) ((value >>> 32) & 0xFFL);
    buf[offset+5] = (byte) ((value >>> 40) & 0xFFL);
    buf[offset+6] = (byte) ((value >>> 48) & 0xFFL);
    buf[offset+7] = (byte) ((value >>> 56) & 0xFFL);
}

public static long retrieveLong(byte[] buf, int offset) {
    return ((long)buf[offset]) 
        + (((long)buf[offset+1])<<8) 
        + (((long)buf[offset+2])<<16) 
        + (((long)buf[offset+3])<<24)
        + (((long)buf[offset+4])<<32) 
        + (((long)buf[offset+5])<<40) 
        + (((long)buf[offset+6])<<48) 
        + (((long)buf[offset+7])<<56);
}


public static void main(String[] args) {
    byte[] buf = new byte[32];
    storeLong(-1, buf, 0);
    storeLong(1, buf, 8);
    storeLong(Long.MAX_VALUE, buf, 16);
    storeLong(Long.MIN_VALUE, buf, 24);

    System.out.println(-1);
    System.out.println(1);
    System.out.println(Long.MAX_VALUE);
    System.out.println(Long.MIN_VALUE);

    System.out.println(retrieveLong(buf, 0));
    System.out.println(retrieveLong(buf, 8));
    System.out.println(retrieveLong(buf, 16));
    System.out.println(retrieveLong(buf, 24));

}

不要使用
+
并且
字节
已签名:

public static long retrieveLong(byte[] buf, int offset) {
    return ((long)buf[offset]     & 255) 
        | (((long)buf[offset + 1] & 255) << 8) 
        | (((long)buf[offset + 2] & 255) << 16) 
        | (((long)buf[offset + 3] & 255) << 24)
        | (((long)buf[offset + 4] & 255) << 32) 
        | (((long)buf[offset + 5] & 255) << 40) 
        | (((long)buf[offset + 6] & 255) << 48) 
        | (((long)buf[offset + 7] & 255) << 56);
}
公共静态长检索(字节[]buf,int偏移量){
返回((长)buf[偏移量]&255)

|((long)buf[offset+1]&255)我做了一些测试,发现使用java.nio.LongBuffer的速度是我的代码的两倍

    ByteBuffer bb = ByteBuffer.allocate(4*8);
    LongBuffer lb = bb.asLongBuffer();

    lb.put(0, -1);
    lb.put(1, 1);
    lb.put(2, Long.MAX_VALUE);
    lb.put(3, Long.MIN_VALUE);

    System.out.println(lb.get(0));
    System.out.println(lb.get(1));
    System.out.println(lb.get(2));
    System.out.println(lb.get(3));
然后我可以使用bb.array()获取字节数组


感谢Louis Wasserman和Rene Jeschke的努力

什么不起作用?你得到了什么结果,你期望得到什么结果?你应该能够在这样的行中使用循环:
buf[offset+2]=(byte)((value>>16)和0xFFL)
编译错误是什么消息?我已经用输出更新了帖子。@Adel我不认为循环会增加任何效率。正确的方法是使用。
    ByteBuffer bb = ByteBuffer.allocate(4*8);
    LongBuffer lb = bb.asLongBuffer();

    lb.put(0, -1);
    lb.put(1, 1);
    lb.put(2, Long.MAX_VALUE);
    lb.put(3, Long.MIN_VALUE);

    System.out.println(lb.get(0));
    System.out.println(lb.get(1));
    System.out.println(lb.get(2));
    System.out.println(lb.get(3));