Calculates the byte wise XOR of two arrays of bytes - Java java.lang

Java examples for java.lang:byte Array Bit Operation

Description

Calculates the byte wise XOR of two arrays of bytes

Demo Code

/*******************************************************************************
 * Copyright (c) 2008 JCrypTool Team and Contributors
 * /*from   ww  w.j a  va  2s. c  o  m*/
 * All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse
 * Public License v1.0 which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] x1 = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        byte[] x2 = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(java.util.Arrays.toString(xor(x1, x2)));
    }

    /**
     * Calculates the byte wise XOR of two arrays of bytes
     * 
     * @param x1 - the first array
     * @param x2 - the second array
     * @return x1 XOR x2
     */
    public static byte[] xor(byte[] x1, byte[] x2) {
        byte[] out = new byte[x1.length];

        for (int i = 0; i < x1.length; i++) {
            out[i] = (byte) (x1[i] ^ x2[i]);
        }
        return out;
    }
}

Related Tutorials