Skip to content

Instantly share code, notes, and snippets.

@edwardbeckett
Created March 28, 2018 03:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edwardbeckett/d17336ed10896ea3a7cf6456d6443761 to your computer and use it in GitHub Desktop.
Save edwardbeckett/d17336ed10896ea3a7cf6456d6443761 to your computer and use it in GitHub Desktop.
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment