Computes array-wise XOR. - Java Collection Framework

Java examples for Collection Framework:Array Algorithm

Description

Computes array-wise XOR.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] a = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        byte[] b = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(java.util.Arrays.toString(xorArrays(a, b)));
    }// w w  w  .j ava2s  . co m

    /**
     * Computes array-wise XOR.
     * 
     * @param a
     *            the first array.
     * @param b
     *            the second array.
     * @return the XOR-ed array.
     */
    public static byte[] xorArrays(byte[] a, byte[] b) {
        byte[] xor = new byte[a.length];

        for (int i = 0; i < a.length; i++) {
            xor[i] = (byte) (a[i] ^ b[i]);
        }

        return xor;
    }
}

Related Tutorials