DEV Community

Cover image for Boolean Algebra for Dummies
Stefan Drl
Stefan Drl

Posted on

10 1

Boolean Algebra for Dummies

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! 🙏

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (2)

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
 
stefandrl profile image
Stefan Drl

Thank you, Nathan! Very good point 👍

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay