简体   繁体   English

如何在Java中使用AND字节数组?

[英]How to AND byte array in Java?

So I was wondering if/how one might use the AND operation on a byte array in Java? 所以我想知道是否/如何在Java中的字节数组上使用AND操作?

I've seen samples of how to use the AND operation with ints like so: 我已经看过如何使用AND操作的示例,如下所示:

int bitmask = 0x000F;
int val = 0x2222;

// prints "2"
System.out.println(val & bitmask);

But say I have a byte Array like... 但是说我有一个字节数组像......

byte[] byteArray = new byte[1];

and I want to AND it so that I remove the leftmost/fist bit in the array. 我想要它,以便我删除数组中最左边/第一位。 I figure I'd use the mask 0x7F but how would I AND that with the byte array? 我想我会使用掩码0x7F但我和那个字节数组怎么样?

The bitwise and operator would do the trick, it is simply & 按位和运算符可以做到这一点,它只是&

This is the demo they present for masking: 这是他们为掩盖而呈现的演示:

class BitDemo {
    public static void main(String[] args) {
        int bitmask = 0x000F;
        int val = 0x2222;
        // prints "2"
        System.out.println(val & bitmask);
    }
}

I want to AND it so that I remove the leftmost/fist bit in the array 我想要它,以便我删除数组中最左边/第一位

I assume with remove you mean unset , because you can't remove the first bit. 我假设删除你的意思是未设置 ,因为你不能删除第一位。 It will always be there. 它将永远存在。 If I am right, you can do this: 如果我是对的,你可以这样做:

    byteArray[0] &= 127;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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