Java的XOR超过两个数组 [英] Java XOR over two arrays
本文介绍了Java的XOR超过两个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要过两个数组一样,让我们说,我有一个应用异:
I have to apply an xor over two arrays like let's say I have :
array_1: 1 0 1 0 1 1
array_2: 1 0 0 1 0 1
我想有一个接受两个数组并返回应用XOR阵列功能,
所以在这种情况下,我想这个函数返回:
I would like to have a function that accepts two arrays and returns an array applying the XOR, so in this case I would like this function to return:
returned_array: 0 0 1 1 1 0
请帮我的算法..谢谢!
Please help me with an algorithm .. Thanks !
推荐答案
如果你是存储在字节数组这些数字,用这个简单的解决方案:
If you are storing these numbers in byte arrays, use this straightforward solution:
byte[] array_1 = new byte[] { 1, 0, 1, 0, 1, 1 };
byte[] array_2 = new byte[] { 1, 0, 0, 1, 0, 1 };
byte[] array_3 = new byte[6];
int i = 0;
for (byte b : array_1)
array_3[i] = b ^ array_2[i++];
输出数组:
0 0 1 1 1 0
这篇关于Java的XOR超过两个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文