DEV Community

loading...
Cover image for Boolean Algebra for Dummies

Boolean Algebra for Dummies

stefandurlesteanu profile image Stefan Durlesteanu ・2 min read

There are times when I find it hard to write a conditional and get the right result, so I decided to make some research and find out more about this fancy Boolean Algebra.

There is no rocket science, only basics. Hope to help some beginners to find the light at the end of tunnel. 😄

There is a kind of cheat sheet structured by levels with just a little theory.

❗ DISCLAIMER: This information is quite new for me as well, I encourage you to double check everything.

Level 0:

There is a data type called bool or boolean in computer
science with 2 possible values => True (1) and False (0)

Also, there are some boolean operators that are used in
conditional statements: Not (!), AND (&&), OR (||), XOR (^)

Level 1:

x y x AND y x OR y x XOR y
False False FALSE FALSE FALSE
False True FALSE TRUE TRUE
True False FALSE TRUE TRUE
True True TRUE TRUE FALSE
!True == False (NOT True equals False)
!False == True (NOT False equal True)
Enter fullscreen mode Exit fullscreen mode

Level 2:

Some simple rules of boolean algebra (x is "something"):

False OR x == x 
True OR x == True
False AND x == False
True AND x == x
Enter fullscreen mode Exit fullscreen mode
Level 2.1:

For the rest of the examples I have used short operators
(&& and ||) and constants A, B, C (these could be either true
or false).

A && A == A
A || A == A
A || !A == True
A && !A == False
Enter fullscreen mode Exit fullscreen mode

Level 3:

Remember that ! => high precedence, && => medium precedence
|| => low precedence

!!A = A

A && (A || B) == A
A || A && B == A

(A && B) && C == (A && B) && C == A && B && C
(A || B) || C == (A || B) || C == A || B || C

A && B == B && A
A || B == B || A

A && (B || C) == A && B || A && C
A || B && C == (A || B) && (A|| C)
A && (B || C) == (A && B) || (A && C)
A || (B && C) == (A || B) && (B || C)
Enter fullscreen mode Exit fullscreen mode

All of the above have some fancy law names, but I don't think that you'll ever need to know them. 😃

Level 4 (De Morgan's laws):

The name of this laws is too cool not to write it down 😁

!(A || B) == !A && !B
!(A && B) == !A || !B
Enter fullscreen mode Exit fullscreen mode

Thank you! 🙏

Discussion (2)

pic
Editor guide
Collapse
kallmanation profile image
Nathan Kallman

Well done!

In "Level 3", where you have A && B == B && A (and same for ||) that's true in Boolean Logic; but in languages like Javascript because the "boolean" operator can be applied to more than just boolean values, switching the inputs might not have the same results for those uses.

And yes; DeMorgan's Law is super cool!

Collapse
stefandurlesteanu profile image
Stefan Durlesteanu Author

Thank you, Nathan! Very good point 👍