示例:
package com.thb;
import java.util.BitSet;
public class BitSetDemo {
public static void main(String[] args) {
byte[] b1= {(byte)0b11001001, (byte)0b00011000};
byte[] b2= {(byte)0b10001001, (byte)0b10000001};
BitSet bitSet1 = BitSet.valueOf(b1);
BitSet bitSet2 = BitSet.valueOf(b2);
bitSet1.xor(bitSet2);
byte[] newBytes = bitSet1.toByteArray();
System.out.println("异或的结果:");
for (int i = 0; i < newBytes.length; i++) {
System.out.println("第" + ( i + 1) + "个" + "字节:" + Integer.toBinaryString(Byte.toUnsignedInt(newBytes[i])));
}
}
}
运行输出:
异或的结果:
第1个字节:1000000
第2个字节:10011001