Java XOR two arrays [closed]
•
Java
I have to apply one XOR two arrays as if I had the following:
array_1: 1 0 1 0 1 1 array_2: 1 0 0 1 0 1
I want a function that accepts two arrays and returns an array with XOR applied, so in this case, I want this function to return:
returned_array: 0 0 1 1 1 0
Please help me with an algorithm thank you!
Solution
If you store these numbers in a byte array, use the following simple solution:
byte[] array_1 = new byte[] { 1,1,1 }; byte[] array_2 = new byte[] { 1,1 }; byte[] array_3 = new byte[6]; int i = 0; for (byte b : array_1) array_3[i] = b ^ array_2[i++];
Output array:
0 0 1 1 1 0
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码