简体   繁体   English

在Java中对128位进行按位运算

[英]Bitwise operations on 128 bit in java

I have binary representation of two IPv6 addresses 我有两个IPv6地址的二进制表示形式

For example: First String is binary representation of 例如:第一个字符串是的二进制表示形式

'2001:4E8:0:4000:0:0:0:0'
'00100000000000010000010011101000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000'

Second string binary representation 第二字符串二进制表示

 '0:0:0:0:ffff:ffff:ffff:ffff'
 '00000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111'

Now I want to do a biwise 'AND' operation on the IPv6 Address and its mask. 现在,我想对IPv6地址及其掩码进行双向“与”操作。 What would be a good way to achieve this in java? 用Java实现此目标的一种好方法是什么?

P:S: Integer.parseInt supports only 32 bit operations P:S:Integer.parseInt仅支持32位操作

You can use BigInteger 's and() : 您可以使用BigIntegerand()

BigInteger first = new BigInteger("00100000000000010000010011101000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000",2);
BigInteger second = new BigInteger("00000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111",2);
BigInteger and = first.and(second);

You can use BitSet for this. 您可以为此使用BitSet。 But for this you will need to convert String to array of bytes 但是为此,您将需要将String转换为字节数组

public static byte[] toByteArray(String bytes){
    byte[] bb = new byte[bytes.length()/8];
    byte m1 = (byte) (1 << 7);
    for(int i = 0, j = 0; i < bb.length; i++, j=i*8){
        byte b = bytes.charAt(j) == '1' ? m1 : 0;
        b |= Byte.valueOf(bytes.substring(j + 1, j + 8), 2);
        System.out.println(Integer.toBinaryString(b));
        bb[i] = b;
    }
    return bb;
}

Now you can use it in BitSet 现在您可以在BitSet中使用它

BitSet bs1 = BitSet.valueOf(toByteArray("00000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111"));
BitSet bs2 = BitSet.valueOf(toByteArray("00100000000000010000010011101000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000"));
BitSet bs3 = bs1.and(bs2);

I'm assuming that you started out with the IPv6 address and netmask in standard IPv6 notation ... not in a binary string representation consisting of '0' and '1' characters. 我假设您以标准IPv6表示法开始使用IPv6地址和网络掩码...而不是由'0''1'字符组成的二进制字符串表示形式。

If you use InetAddress.getByName(String) on an IPv6 address literal, you will get an Inet6Address object. 如果在IPv6地址文字上使用InetAddress.getByName(String) ,则将获得一个Inet6Address对象。 If you call getAddress() on this, you will get the raw address as a byte[] . 如果对此调用getAddress() ,则将原始地址作为byte[] You can do the same to get a byte[] from a netmask. 您可以执行相同的byte[]从网络掩码中获取byte[]

Then you can loop over the respective byte arrays and use bitwise operations on them. 然后,您可以遍历各个字节数组,并对它们使用按位运算。

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

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