Xors 2 byte arrays. - Java java.lang

Java examples for java.lang:byte Array Bit Operation

Description

Xors 2 byte arrays.

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(XOR(a, b)));
    }/*from  w ww . j  a  v  a  2s. co m*/

    /**
     * Xors 2 byte arrays.
     * @param a
     * @param b
     * @return
     */
    public static byte[] XOR(byte[] a, byte[] b) {
        byte[] result = new byte[Math.min(a.length, b.length)];

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

        return result;
    }
}

Related Tutorials