Java xor xor(byte lhs[], byte rhs[])

Here you can find the source of xor(byte lhs[], byte rhs[])

Description

xor

License

Open Source License

Return

null if either arg is null or the args are not equal length

Declaration

public final static byte[] xor(byte lhs[], byte rhs[]) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**// w  w w .ja v a2 s  . c  om
     *  @return null if either arg is null or the args are not equal length
     */
    public final static byte[] xor(byte lhs[], byte rhs[]) {
        if ((lhs == null) || (rhs == null) || (lhs.length != rhs.length))
            return null;
        byte diff[] = new byte[lhs.length];
        xor(lhs, 0, rhs, 0, diff, 0, lhs.length);
        return diff;
    }

    /**
     * xor the lhs with the rhs, storing the result in out.  
     *
     * @param lhs one of the source arrays
     * @param startLeft starting index in the lhs array to begin the xor
     * @param rhs the other source array
     * @param startRight starting index in the rhs array to begin the xor
     * @param out output array
     * @param startOut starting index in the out array to store the result
     * @param len how many bytes into the various arrays to xor
     */
    public final static void xor(byte lhs[], int startLeft, byte rhs[], int startRight, byte out[], int startOut,
            int len) {
        if ((lhs == null) || (rhs == null) || (out == null))
            throw new NullPointerException("Null params to xor");
        if (lhs.length < startLeft + len)
            throw new IllegalArgumentException("Left hand side is too short");
        if (rhs.length < startRight + len)
            throw new IllegalArgumentException("Right hand side is too short");
        if (out.length < startOut + len)
            throw new IllegalArgumentException("Result is too short");

        for (int i = 0; i < len; i++)
            out[startOut + i] = (byte) (lhs[startLeft + i] ^ rhs[startRight + i]);
    }
}

Related

  1. xor(boolean... bools)
  2. xor(boolean[] array)
  3. xor(boolean[][] b1, boolean[][] b2)
  4. xor(byte abyte0[])
  5. xor(byte data[], byte key[])
  6. xor(byte[] a, byte[] b)
  7. xor(byte[] a, byte[] b)
  8. xor(byte[] a, byte[] b)
  9. xor(byte[] a, byte[] b)