Android Bit Set xor(byte[]... arrays)

Here you can find the source of xor(byte[]... arrays)

Description

XORs the bits in an arbitary number of byte arrays.

Return

The result of XORing the bits in the byte arrays.

Declaration

public static byte[] xor(byte[]... arrays) 

Method Source Code

//package com.java2s;
import java.util.Arrays;
import java.util.Comparator;

public class Main {
    /**//from  ww w. j  a  v  a 2  s .c  om
     * XORs the bits in an arbitary number of byte arrays.
     *
     * @return The result of XORing the bits in the byte arrays.
     */
    public static byte[] xor(byte[]... arrays) {
        if (arrays.length == 0)
            return null;

        sortDecendingSize(arrays);

        byte[] result = new byte[arrays[0].length];

        for (byte[] array : arrays) {
            for (int i = 0; i < array.length; i++) {
                result[i] ^= array[i];
            }
        }

        return result;
    }

    public static void sortDecendingSize(byte[]... arrays) {
        Arrays.sort(arrays, new Comparator<byte[]>() {
            @Override
            public int compare(byte[] lhs, byte[] rhs) {
                if (lhs.length > rhs.length)
                    return -1;
                if (lhs.length < rhs.length)
                    return 1;
                else
                    return 0;
            }
        });
    }
}

Related

  1. resetBit(byte b, int bit)
  2. setBit(byte[] arr, int bit)
  3. setBit(byte[] bytes, int bitNr, int bit)
  4. reverseBits(byte[] bits)
  5. bitwiseNot(int number)
  6. xorArray(byte[] array, byte[] mask)