HTML Blox

Providing the building blox of the web.

  • HTML Blox Home
  • Blog
    • Design
    • Flash
    • General
    • HTML
    • Javascript
    • PHP
    • Programming
    • Web Servers
  • Forums
  • Where to Start?
  • Lessons
    • CSS
    • Design
    • HTML
    • Javascript
    • Programming
  • References
    • HTML
  • Resources
  • About
  • Contact
  • Go Green!

Programming Lessons

  1. Lesson 1: The Basics
  2. Lesson 2: A Brief History of Computers
  3. Lesson 3: The Workings of Computers
  4. Lesson 4: A Brief History of Programming
  5. Lesson 5: High-Level vs. Low-Level
  6. Lesson 6: Compiled vs. Interpreted
  7. Lesson 7: Variables
  8. Lesson 8.1: Arithmetic and Operators - Decimal
  9. Lesson 8.2: Arithmetic and Operators - Integer and Boolean
  10. Lesson 9.1: Conditional Statements - The Basics
  11. Lesson 9.2: Conditional Statements - Branching Conditionals
  12. Lesson 10.1: Loops - The Basics
  13. Lesson 10.2: Types of Loop
  14. Lesson 11: Functions
  15. Lesson 12: Strings
  16. Lesson 13: Procedural vs. Object-Oriented

Programming Reference

  1. No reference yet.

Links

  • W3C HTML 4.01 Strict Compliant
  • W3C CSS 2.1 Compliant
  • Web Standards Group Member
  • Follow us on Twitter
  • Number Overflow - Free Advanced Scripts and Tutorials
  • Azure Ronin Web Design - Professional Web Design and Development
  • XML Sitemap
  • U Comment, I Follow

Feeds

  • RSS 2.0 Feed
  • RDF/RSS 1.0 Feed
  • RSS 0.9 Feed
  • Atom Feed

Lesson 8.2: Arithmetic and Operators – Integer and Boolean

del.icio.us Digg Blinklist reddit

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.

This entry was posted on Wednesday, December 3rd, 2008 at 5:17 pm and is filed under Programming Lessons. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

Please Login or Register or fill out the following to leave a reply:

If you haven't posted an approved comment before, your comment will not show up until approved.

  • HTML Blox Home
  • Blog
  • Design Blog
  • Flash Blog
  • General Blog
  • HTML Blog
  • Javascript Blog
  • PHP Blog
  • Programming Blog
  • Web Servers Blog
  • Forums
  • Where to Start?
  • Lessons
  • CSS Lessons
  • Design Lessons
  • HTML Lessons
  • Javascript Lessons
  • Programming Lessons
  • References
  • HTMLReferences
  • About
  • Resources
  • Contact
  • Go Green!
  • Sitemap

Copyright © 2008 HTML Blox

Unless otherwise noted, all images created and copyright HTML Blox.

Operated by Azure Ronin Web Design