Introducing Binary Arithmetic
Mathematics using only 0's and 1's might seem bizarre at first, but as we will see in this section, the principals are quite straight forward. But why would we need to do maths in binary if we want to program in Python? This is a good question. The answer is two fold: firstly, many programmers make errors when it comes to constructing conditions (we will consider conditions in a future section), and being able to think about conditions as boolean algebraic problems can avoid many of these problems; secondly, any operation, mathematical, logical, or otherwise, which you perform in Python will be deconstructed by the interpreter when running your code and executed in the CPU using binary arithmetic, thus understanding how binary arithmetic works will give you a deeper understanding of what is happening when your code is running.
Basic Binary Arithmetic
With Binary Arithmetic, it is useful to remember that we are dealing, on a physical level, with wires along which we either have current (1) or do not have current (0). In this dimension, we have three primary boolean arithmetic operations: And, Or, and Not.
The AND operator, also referred to as an Intersection, requires that both values on either side of the operator are true (meaning there is a current (1)). So, for example,
1 AND 1 = 1
This results in 1 (true) as both the values on either side of the AND operator are true. If one of them were to be false, the result would be false, as we can see here:
1 AND 0 = 0 0 AND 1 = 0
For convenience, here is the complete set of possible scenarios for AND:
1 AND 1 = 1 1 AND 0 = 0 0 AND 1 = 0 0 AND 0 = 0
Some might find it more intuitive to visualise the AND operator as an Intersection in the following way:
If one of the terms is 0 and the other 1, then they are mutually exclusive and no intersection occurs.
The OR operator, also referred to as a Union, only requires one of the values on either side of it to be true. So, for OR, here are the possible scenarios:
1 OR 1 = 1 1 OR 0 = 1 0 OR 1 = 1 0 OR 0 = 0
Some might find it more intuitive to visualise the OR operator as a Union in the following way:
As long as one of items is true, then the union, or combination of the two, will be true.
The NOT operator simply switches the state of the value. So it will turn 0 into 1, and 1 into 0.
Here are the two scenarios for the NOT operator:
NOT 1 = 0 NOT 0 = 1
Order of Operations
In arithmetic, we have an order of operations (sometimes referred to as BODMAS), which dictates in which order we evaluate mathematical expressions. For example, if we were presented with the following arithmetic expression
1 + 2 * 4 / 3
There are a number of ways we could interpret this. For example:
(1 + 2) * (4 / 3) = 3 * 4/3 = 4
[1 + (2 * 4)] / 3 = 9 / 3 = 3
1 + [(2 * 4) / 3] = 1 + (8 / 3) = 3.6
In the examples above, one of the answers was 4, the other, 3 and one of the answers was 3.6 . So which method was correct? Well, if we apply the principal of BODMAS (Brackets, Of, Division, Multiplication, Addition, and Subtration), we see that we should first Divide the 4 by the 3, then Multiply that by 2 and finally add the 1.
So the correct procedure for simplifying this expression would be:
1 + [2 * (4 / 3)] = 1 + (2 * 1.3) = 1 + 2.6 = 3.6
Often, we string boolean operators together into a complex statement, such as:
a AND b OR c OR b AND NOT c
And in these situations, we have the same problem as above, where we need to decide on the order of operations.
In the case of binary algebra, the following order of operations applies:
So, for the above boolean algebraic expression, we would first apply the NOT to the c, then compute the two AND expressions, and finally apply the two OR's. To better understand, let's plug in some values to the above expression:
a AND b OR c OR b AND NOT c let a = 1 let b = 1 let c = 0 Therefore, 1 AND 1 OR 0 OR 1 AND NOT 0 = 1 AND 1 OR 0 OR 1 AND 1 = 1 OR 0 OR 1 = 1 OR 1 = 1
Application in Programming
Open up the Python interactive interpreter, and follow along with the code. We'll start with individual AND, OR and NOT operations:
print 1 and 0 [Output: 0] print 1 or 0 [Output: 1] print not 0 [Output: True]
Let's now see how we string them together:
a = 1 b = 0 c = 1 answer = a or b and not c and b or a print answer [Output: 1]
Given a = 1, b = 1, c = 0, and d = 0. Evaluate the following:
- a AND [b OR NOT a OR NOT b] AND c
- b AND NOT c
- b AND NOT b
- b OR NOT b
- c OR NOT c
- c AND NOT c