Lesson 8.2: Arithmetic and Operators – Integer and Boolean
In our last lesson we looked at some basics of arithmetic and took a look at examples involving decimal arithmetic. In this lesson we’ll finish up by looking at integer arithmetic and boolean arithmetic.
Integer Arithmetic
Integer arithmetic differs from decimal arithmetic only in the fact that we truncate the decimal portion of the number after each step. For example, if we look at 10 / 4, in decimal arithmetic we get 2.5. In integer arithmetic, we simply ignore the decimal, so we get just 2 as our answer.
Let’s look at a few more examples:
Integer Arithmetic
value = 10 * 3 + (50 / 4) + (50 + 3) - 10 / 4 + 5 * 3 value = 10 * 3 + 12 + (50 + 3) - 10 / 4 + 5 * 3 (We did 50/4=12.5, and simply dropped the .5) value = 10 * 3 + 12 + 53 - 10 / 4 + 5 * 3 value = 30 + 12 + 53 - 10 / 4 + 5 * 3 value = 30 + 12 + 53 - 2 + 5 * 3 (We did 10/4 = 2.5, and simply dropped the .5) value = 30 + 12 + 53 - 2 + 15 value = 42 + 53 - 2 + 15 value = 95 - 2 + 15 value = 93 + 15 value = 108
Ironically, we ended up with the same answer as decimal arithmetic since the two times we dropped a decimal were the same and they subtracted out. Here is a shorter example where the values are different. First we’ll look at the decimal version, then the integer version:
Decimal Arithmetic
value = 50 / 4 + 10 / 3 value = 12.5 + 10 / 3 value = 12.5 + 3.33333 value = 15.833333
Integer Arithmetic
value = 50 / 4 + 10 / 3 value = 12 + 10 /3 value = 12 + 3 value = 15
Everything else about Integer arithmetic is the same as decimal arithmetic, including the order of operations. The one thing that integer arithmetic sometimes has that decimal arithmetic doesn’t is called the modulus operator. Some languages don’t have this operator, others use the word MOD, and others use the % sign.
Modulus is simply the remainder from division. So, say you do 10/3. Integer arithmetic says this equals 3. If you do 10%3 you get 1, the remainder. If a language you are using doesn’t have a modulus operator, you can write it with a bit of math. The following equations are equivalent (for integer arithmetic):
Modulus
valueA % valueB valueA - ((valueA / valueB) * valueB)
The second one you can use if the modulus function isn’t available. Here is an example using numbers so you can see how it works:
Modulus
10 % 3 1 10 - ((10/3) * 3) (10/3 is 3.33333, so just 3 in integer) 10 - (3 * 3) 10 - 9 1
Why use Integer Arithmetic?
There are several reasons you may want to use integer arithmetic. For example, say a person in a game gets a rank based on their score. Say for every million points they get, their rank goes up by one. So, you can simply divide their score by one million and get their rank. If you use decimal arithmetic, you have to do work to get the decimal to come off, which can often be a pain.
Most typed programming languages (like C++ and Java) will automatically do integer arithmetic if you do math with integer values. Other typeless languages like Javascript will require that you explicitly state that it is integer math in some way or another.
Boolean Arithmetic
Boolean arithmetic is math done with boolean (true/false) values. Since you can only have true and false (1 and 0), it doesn’t make sense to use our add, subtract, divide, and multiply operators since we’d get values that aren’t always 1 or 0. Instead we use the boolean operators AND, OR, or XOR.
In some languages these operators are written as words (such as in MySQL and Visual Basic), but in others you use symbols (such as C++ and PHP).
Boolean Operators
The three operators each give a different value depending on the other values. These values are always either true or false.
AND returns true if both values are true. False otherwise.
OR returns true if one or both values are true. False otherwise.
XOR returns true if one and only one value is true. False otherwise.
Here are a few examples:
Boolean Arithmetic
a = true b = false a AND b = false a OR b = true a XOR b = true
Here is a table with the results of each value:
| First Value | Second Value | AND | OR | XOR |
| false | false | false | false | false | false | true | false | true | true |
| true | false | false | true | true |
| true | true | true | true | false |
As you can see with the second and third rows, it doesn’t matter the order of the values, the result is the same.
XOR is Missing?!
In many programming languages, AND and OR are almost always present. However, often is the case that XOR is missing as an operator (in fact, I can’t think of a language that does. XOR is more commonly used in electronic circuitry than programming itself. However, if you ever do need an XOR, the follow is equivalent:
a OR b AND NOT(a AND b)
which is equivalent to:
a XOR b
A bit more typing, but simple enough to understand.
Boolean Order of Operations
The order of operations is fairly simple with boolean arithmetic. The three operators all have an equal order of precedence, meaning we do each of them as we see them. The only other operator we consider is parentheses, which work just like in decimal arithmetic: do the inside of the parentheses before moving forward. Here are a few examples:
Boolean Arithmetic
false AND (true OR false) false AND true false true AND (false OR (true AND false)) true AND (false or false) true AND false false
Why Boolean Arithmetic?
Boolean arithmetic can be extremely useful when you use conditional statements, which are chunks of code that you only run if certain conditions are met, which we’ll cover in the next lesson. In fact, this is essentially all boolean arithmetic is used for in programming, but it is very important. Without boolean arithmetic, it would be very difficult to control the flow of control (that is, the order the code is run in) within a program.
In this lesson we rounded out our arithmetic lesson with integer and boolean arithmetic. In our next lesson we’ll put that boolean arithmetic to use with conditional statements.




