Boolean algebra laws with C#
A beginner guide to programming with .NET 5 and C#
This article explores multiple Boolean algebra laws in a programmer-oriented way, leaving the mathematic notation aside. Those laws can be beneficial when working with boolean logic to simplify complex conditions. This article is very light in explanation and exposes the laws using C#. Don’t worry, I’m not recycling myself as a math teacher.
This article is part of a learn programming series where you need no prior knowledge of programming. If you want to learn how to program and want to learn it using .NET/C#, this is the right place. I suggest reading the whole series in order, starting with Creating your first .NET/C# program, but that’s not mandatory.
This article is part of a sub-series, starting with Introduction to Boolean algebra and logical operators. It is not mandatory to read all articles in order, but I strongly recommend it, especially if you are a beginner. If you are already reading the whole series in order, please discard this word of advice.
Let’s explore Boolean algebra laws in alphabetical order. Some are very simple, while some may seem more complex, but all are very useful tools.
In the examples, the variables (
C) are all booleans, like this:
bool A, B, C;
The absorption law goes as follow:
A && (A || B)always equals
A || A && Balways equals
The annulment law goes as follow:
A && falseis always
A || trueis always
The associative law says that no matter the order or priority of the OR comparisons, the result will always be the same. The following conditions yield the same result:
A || (B || C)
(A || B) || C
(A || C) || B
A || B || C
The complement law goes as follow:
A && !Ais always
A || !Ais always
The commutative law goes as follow:
A && Bis the same as
B && A
A || Bis the same as
B || A
The consensus law goes as follow:
(A || B) && (!A || C) && (B || C)is equivalent to
(A || B) && (!A || C)
A && B || A && C || B && Cis equivalent to
A && B || A && C
These two might be harder to grasp, so here’s my take at a quick explanation:
- In the first two comparisons of
(A || B) && (!A || C) && (B || C),
!Aare almost cancelling themselves, leaving the outcome of the comparison to
C. Based on that fact, no matter the values, the last comparison
&& (B || C)becomes useless (already evaluated).
- The second one is similar. In the first two comparisons of
A && B || A && C || B && C, we test
C, making the last comparison void
|| B && C.
Word of advice: it is not mandatory to remember the consensus law, so feel free to skip this one if you think it’s too complicated for today. You can always come back to it later.
De Morgan’s laws
This one is very interesting and can be handy from time to time. In plain English, De Morgan’s laws are:
conjunctionmeans AND (
&&), and the logical
disjunctionmeans OR (
||). Unless you are into mathematics, you don't have to remember that.
Now to the part that interests us, in C#, De Morgan’s laws are:
!(A || B)is equivalent to
!A && !B
!(A && B)is equivalent to
!A || !B
The distributive law goes as follow:
A && B || A && Cis equivalent to
A && (B || C)
(A || B) && (A || C)is equivalent to
A || B && C
Double negation law
The double negation law says that two negations negate themselves. In C# this looks like:
!!Ais equivalent to
The identity law goes as follow:
A && truealways equals
A || falsealways equals
The idempotent law goes as follow:
A && Aalways equals
A || Aalways equals
The redundancy law goes as follow:
(A || B) && (A || !B)always equals
A && B || A && !Balways equals
(A || !B) && Bis equivalent to
A && B
A && !B || Bis equivalent to
A || B
In this article, we explored a bunch of Boolean algebra laws from a C# programmer perspective. You can find information on them, including mathematic proofs, online or in books if you are into maths. Personally, I prefer the plain C# version, so that’s why I translated them to this here. Even if some of those laws might seem a bit too complicated to remember, don’t be discouraged; even the simplest ones are helpful: start there.
Programming is like playing LEGO ®blocks: we can combine all of those laws, which are logic patterns. For example,
!(!A || !B) looks complicated, but after applying De Morgan's law, it becomes equivalent to
!(!(A && B)). By removing the useless parenthesis we end up having
!!(A && B), which exposes a double negation. Applying the double negation law leads to
A && B. That simplified version of the original condition looks way simpler, doesn't it?
Learning the basics is helpful in the long run. If you have a good memory, feel free to memorize all of this as a starting point. If you don’t, don’t worry, learn them one by one. The idea is to simplify your code, so it reaches a more maintainable state. With a bit of time, you will most likely know and apply many (if not all) of them without thinking about it. Just start with the simplest ones, bookmark the article, and learn the others later. Understanding rules, laws, and concepts should get you further than just remembering them; but that takes more time.
Fun fact: I’ve run many interviews in 2021, and one of my favorite technical questions is based on complicated conditions that can be simplified using some of those laws. And no, I’m not looking for candidates that know the name of the laws, just if they can resolve a complex if-statement and how.
Please leave your questions or comments below or drop me a Tweet.
Stay tuned by following me on Medium, Twitter, or other places you can find me. You can look at my blog contact page for more info.