Profile Pic

2GD4ME

“Haley Halcyon’s coding dump”

I design, make music, play rhythm games, and code. Here's a link to a bunch of stuff I coded.

Boolean cheat sheet

with math, C, and Python notation

About

This is a list of truth tables and boolean properties expressed as math, C code, and Python code.

I made this because I wanted to teach someone to code, but all the existing cheat sheets for boolean logic were not for programming C code.

NOTE: I use "true" and "false" in the C-like column, but note that C itself does not have these keywords. Replace them with 1 and 0 as necessary!

Table of contents

Truth tables

NOT

Math
A
¬A
C-like
Afalsetrue
!Atruefalse
Python
AFalseTrue
not ATrueFalse

AND

Math
A∧BA=⊥A=⊤
B=⊥
B=⊤
C-like
A && BA==falseA==true
B==falsefalsefalse
B==truefalsetrue
Python
A and BA==FalseA==True
B==FalseFalseFalse
B==TrueFalseTrue

OR

Math
A∨BA=⊥A=⊤
B=⊥
B=⊤
C-like
A || BA==falseA==true
B==falsefalsetrue
B==truetruetrue
Python
A or BA==FalseA==True
B==FalseFalseTrue
B==TrueTrueTrue

XOR

Math
A⊻BA=⊥A=⊤
B=⊥
B=⊤

XOR is the inverse of IFF (If and only if), notated by the ⇔ symbol. In other words, A⊻B ⇔ ¬(A⇔B).

C-like
*A==falseA==true
B==falsefalsetrue
B==truetruefalse

*There is no logical xor operator in C. Use A ^ B if you are sure that A and B are either 0 or 1; otherwise use !A != !B.

Python
*A==FalseA==True
B==FalseFalseTrue
B==TrueTrueFalse

*There is no logical xor operator in Python. Use A ^ B if you are sure that A and B are of type bool; otherwise use bool(A) != bool(B).

IMPLY (IF)

Math
A⇒BA=⊥A=⊤
B=⊥
B=⊤

*This is commonly used in boolean logic, but not commonly used in C code.

*This is commonly used in boolean logic, but not commonly used in Python code.

Identities

NameMathC-likePython
identity for OR⊥∨A=Afalse || A == AFalse or A == A
annihilator for AND⊥∧A=⊥false && A == falseFalse and A == False
annihilator for OR⊤∨A=⊤true || A == trueTrue or A == True
identity for AND⊤∧A=Atrue && A == ATrue and A == A
idempotenceA∨A=AA || A == AA or A == A
idempotenceA∧A=AA && A == AA and A == A
double negative¬(¬A)=A!!A == Anot not A == A

NOTE: Using the annihilator functions, something called "lazy evaluation" will skip evaluating the later parts of an expression if it's clear what the answer will be. For example, if your condition is "(username is not blank) AND (password is not blank) AND (username and password match)", you can skip checking if the password is not blank or if the username and password match if the username is blank.

Laws

NameMathC-likePython
CommutativeA∨B=B∨AA || B == B || AA or B == B or A/td>
CommutativeA∧B=B∧AA && B == B || AA and B == B and A
AssociativeA∨(B∨C)=(A∨B)∨CA || (B || C) == (A || B) || CA or (B or C) == (A or B) or C
AssociativeA∧(B∧C)=(A∧B)∧CA && (B && C) == (A && B) && CA and (B and C) == (A and B) and C
DistributiveA∧(B∨C)=(A∧B)∨(A∧C)A && (B || C) == (A && B) || (A && C)A and (B or C) == (A and B) or (A and C)
DistributiveA∨(B∧C)=(A∨B)∧(A∨C)A || (B && C) == (A || B) && (A || C)A or (B and C) == (A or B) and (A or C)

NOTE: These are only "equal" insofar as the final result is equal. Due to lazy evaluation, some ways to express the same condition in code are more efficient than others!

De Morgan's Laws

NameMathC-likePython
negation of disjunction¬(A∨B) = ¬A∧¬B!(A || B) == !A && !Bnot(A or B) == not A and not B
negation of conjunction¬(A∧B) = ¬A∨¬B!(A && B) == !A || !Bnot(A and B) == not A or not B
Donations
coded with ♥ by Haley Halcyon