Logical Operators Cheat Sheet
public class LogicalOperators {
/*
* TRUTH LOGIC
*
* X & Y
* (AND)
*
* AND is Only True IF BOTH OPERANDS ARE TRUE
* ---------------------------------
* |X = TRUE & Y = TRUE :: TRUE |
* |X = TRUE & Y = FALSE :: FALSE |
* |X = FALSE & Y = TRUE :: FALSE |
* |X = FALSE & Y = FALSE :: FALSE |
* ---------------------------------
*
* X | Y
* (INCUSIVE OR)
*
* Inclusive OR is Only False IF BOTH OPERANDS ARE FALSE
* ---------------------------------
* |X = TRUE & Y = TRUE :: TRUE |
* |X = TRUE & Y = FALSE :: TRUE |
* |X = FALSE & Y = TRUE :: TRUE |
* |X = FALSE & Y = FALSE :: FALSE |
* ---------------------------------
*
* X ^ Y
* (EXCLUSIVE OR)
*
* Exclusive OR is Only True IF BOTH OPERANDS ARE DIFFERENT
* ---------------------------------
* |X = TRUE & Y = TRUE :: FALSE |
* |X = TRUE & Y = FALSE :: TRUE |
* |X = FALSE & Y = TRUE :: TRUE |
* |X = FALSE & Y = FALSE :: FALSE |
* ---------------------------------
**/
public static void main(String[] args) {
boolean x, y, and, or, xor;
x = false;
y = false;
and = x & y;
or = x | y;
xor = x ^ y;
System.out.println("====================");
System.out.println("X = FALSE, Y = FALSE");
System.out.println("====================");
System.out.println("X & Y = " + and);
System.out.println("X | Y = " + or);
System.out.println("X ^ Y = " + xor);
x = false;
y = true;
and = x & y;
or = x | y;
xor = x ^ y;
System.out.println("===================");
System.out.println("X = FALSE, Y = TRUE");
System.out.println("===================");
System.out.println("X & Y = " + and);
System.out.println("X | Y = " + or);
System.out.println("X ^ Y = " + xor);
x = true;
y = true;
and = x & y;
or = x | y;
xor = x ^ y;
System.out.println("==================");
System.out.println("X = TRUE, Y = TRUE");
System.out.println("==================");
System.out.println("X & Y = " + and);
System.out.println("X | Y = " + or);
System.out.println("X ^ Y = " + xor);
}
}