xor two byte array - Java java.lang

Java examples for java.lang:byte Array Bit Operation

Description

xor two byte array

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 w w.j a  v a2s  .co m*/

    public static byte[] xor(byte[] a, byte[] b) {
        int aLen = a.length;
        int bLen = b.length;
        int min = 0;
        int size = aLen > bLen ? aLen : bLen;
        byte[] c = new byte[size];

        if (size == aLen) {
            min = bLen;
            System.arraycopy(a, min, c, min, size - min);
        } else {
            min = aLen;
            System.arraycopy(b, min, c, min, size - min);
        }
        for (int i = 0; i < min; i++) {
            c[i] = (byte) (a[i] ^ b[i]);
        }
        return c;
    }
}

Related Tutorials