Here's the situation. You're a programmer for MegaBucks Insurance, Inc. and the actuaries have come up with a brand new product they want to sell to people. But they can only sell it to people who ...
Fit in at least one of these categories:
- Are a preferred policy holder
- Do not live in the home state of MegaBucks
- Do not have multiple policies with Megabucks
... and at the same time ...
Fit in one of these categories
- Are not a preferred policy holder
- Both do not live in the home state of Megabucks and have multiple policies
Being an expert VB.NET programmer, you quickly whip out this code:
Public Function Eligible( ByVal PreferredPolicyHolder As Boolean, ByVal InState As Boolean, ByVal MultiplePolicies As Boolean ) As Boolean Eligible = (PreferredPolicyHolder Or InState Or MultiplePolicies) And (PreferredPolicyHolder Or ( InState And MultiplePolicies)) End Function
But after you finish, you start to wonder, "Isn't there a simpler way to code that? Maybe the people they hire in the actuarial department can't figure it out, but programmers live for logic so I ought to be able do better than that!"
And you can! With just a little Boolean algebra, you conclude that the original actuarial specification is logically the same as:
Eligible = PreferredPolicyHolder Or ( Not InState And MultiplePolicies)
Since the corporate executives have decided to include a check for eligibility every time a customer record is accessed in the database, the check will be made about 5,000,000 times a day and the savings in machine time alone is justification for tripling your salary. (Of course, only executives get their salary tripled for things like that. But you can dream, can't you?)
Already, I hear you saying, "The chance of having that kind of programming problem is so close to zero that I'll pass."
Not so fast! Maybe you won't run into this specific problem, but little logical riddles happen all the time. And while you're learning how to blow them away in the actuarial department with your much simpler formulation, you'll also gain a much better understanding of logical expressions overall. And that will make you a better programmer.
This article is all about how you can manipulate logical expressions to figure out things like that. The whole thing is based on rules of formal logic that the English mathematician George Boole is given primary credit for. We won't try to cover all of Boole's rules; just enough to solve your programming problems.
To give you just an idea about what Boolean alregra is, here's Boole's "Associative Law":
(A Or B) Or C is equal to A Or (B Or C)
(A And B) And C is equal to A And (B And C)
You might be saying, "I knew that! That's just VB syntax." Yes, that's right. And the other laws are too. That's because VB syntax is based on formal logic rules. But, as we'll see, there's more to it than that. To prove it, let's see how we can prove that the first logical expression above is the same as the second one.
To begin with, let's rephrase the VB code with symbols that are easier to work with. Using A, B, and C instead of the Boolean variables:
(A or NotB or NotC) and (A or (NotB and C))
It turns out that we can consider "or" to be like the arithmetic "+" and "and" to be like the arithmetic "*" (multiply). I've also used "^" to mean "Not" to keep things shorter. Boolean algebra is a little different than normal algebra, but see if you can follow this example:
Multiplying the two grouped expressions gives us:
A*A + A*^B*^C + ^B*A + ^B*^B*C + ^C*A + ^C*^B*C
Rearranging terms and factoring out A:
A*(1 + ^B*^C + ^B + ^C) + ^B*^B*C + ^C*^B*C
A + ^B*C
You do, actually, have to know the rules of Boolean algebra to work with these formulas. For example, it might confuse you to try to figure out why ...
A*A + A*^B*^C + ^B*A + ^C*A
... is equal to ...
A*(1 + ^B*^C + ^B + ^C)
The goal of this article is to give you some practical advice on how to do things, not make you into a Doctor of Boolean Logic. So lets's look at some tools that you can use. The first is "truth tables". They're just tables that list all the possible values of True or False and the result. For example, here's the traditional Or and And truth tables:
A B A + B --------------------- 0 0 0 0 1 1 1 0 1 1 1 1
A B A * B --------------------- 0 0 0 0 1 0 1 0 0 1 1 1
Pretty simple, right? Well, you can use exactly the same technique to check out a more complex formula. In the example above, first realize that the basic problem is to prove that:
A*A + A*X
... is equal to ...
A*(1 + X)
Where "X" could be anything, including "^B*^C + ^B*A + ^C*A". Here's the truth table:
A X A*A + A*X A(1 + X) 0 0 0 0 0 1 0 0 1 0 1 1 1 1 1 1
Circuit design is probably a much more important use of Boolean algebra than programming and some people relate to a diagram better than they do to formulas. If you're one of those people, draw one! Here's a circuit diagram that describes the formula.
Click Here to display the illustration
There's at least one other popular graphical method: Karnaugh Maps. A practical look of those starts on the next page.