Java xor xor(boolean b1, boolean b2)

Here you can find the source of xor(boolean b1, boolean b2)

Description

Concatenate two booleans with the operation "XOR"

License

Open Source License

Parameter

Parameter Description
b1 - the first boolean
b2 - the second boolean

Return

the result (b1 != b2)

Declaration

public static boolean xor(boolean b1, boolean b2) 

Method Source Code

//package com.java2s;
/**//from w w w.j  a va 2 s .c  om
 * Syncnapsis Framework - Copyright (c) 2012-2014 ultimate
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either version
 * 3 of the License, or any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MECHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Plublic License along with this program;
 * if not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Concatenate two booleans with the operation "XOR"<br>
     * 
     * @param b1 - the first boolean
     * @param b2 - the second boolean
     * @return the result (b1 != b2)
     */
    public static boolean xor(boolean b1, boolean b2) {
        return (b1 != b2);
    }

    /**
     * Concatenate two booleans with the operation "XOR"<br>
     * 
     * @param b1 - the first boolean
     * @param b2 - the second boolean
     * @return the result (b1 != b2)
     */
    public static boolean xor(Boolean b1, Boolean b2) {
        if (b1 == null)
            throw new IllegalArgumentException("The given Boolean b1 is null.");
        if (b2 == null)
            throw new IllegalArgumentException("The given Boolean b2 is null.");
        return (b1 != b2);
    }

    /**
     * Concatenate more than two booleans with the operation "XOR"<br>
     * 
     * @param b - the array of booleans
     * @return the result (exactly one boolean is true)
     */
    public static boolean xor(boolean[] b) {
        if (b == null)
            throw new IllegalArgumentException("The given Array of Boolean b is null.");
        if (b.length == 0)
            throw new IllegalArgumentException("The given Array of Boolean contains no elements.");
        int numberOfTrues = 0;
        for (int i = 0; i < b.length; i++) {
            if (b[i])
                numberOfTrues++;
        }
        return (numberOfTrues == 1);
    }

    /**
     * Concatenate more than two booleans with the operation "XOR"<br>
     * 
     * @param b - the array of booleans
     * @return the result (exactly one boolean is true)
     */
    public static boolean xor(Boolean... b) {
        if (b == null)
            throw new IllegalArgumentException("The given Array of Boolean b is null.");
        if (b.length == 0)
            throw new IllegalArgumentException("The given Array of Boolean contains no elements.");
        int numberOfTrues = 0;
        for (int i = 0; i < b.length; i++) {
            if (b[i] == null)
                throw new IllegalArgumentException(
                        "The given Array of Boolean contains a null element: index " + i + ".");
            if (b[i])
                numberOfTrues++;
        }
        return (numberOfTrues == 1);
    }
}

Related

  1. xor(boolean a, boolean b)
  2. xor(boolean a[], boolean b[])
  3. xor(boolean b1, boolean b2)
  4. xor(boolean o, boolean t)
  5. xor(boolean val1, boolean val2)
  6. xOr(boolean x, boolean y)
  7. xor(boolean... bools)