Values

Goals

Concepts

Language

Lesson

Representations

Four cows.
Cow Quantity: Representations of the number of cows in a field.

Imagine that you are driving down a country road. You see a farmer in a field, so you stop and ask him, Hey, friend. How many cows do you have? The farmer holds up his hand like this:

hand showing four fingers

You decide to make a note of it. You pull out your pen and notepad (your mobile phone needs charging) and write the following: 🐄 🐄 🐄 🐄. No, let's be serious. You probably wouldn't draw little cows. You would probably write down some representation of the number of cows. Maybe you would write this:

hand showing four fingers

But that's probably not what you would write either. Would you write IIII? What in reality you would probably write is:

4

So how many cows did the farmer really have? Of the course the number of cows the farmer had didn't change; he had this many cows: IIII (count them). There are simply various ways humans can use to represent that number.

You continue down the road, and another farmer happens to be in the process of moving his cows from one field to another. You decide to stop and count the number of cows as they enter the new pasture. As they pass through the little gate, you count them, still using digits on your little pad of paper. Because the new pasture is empty before any cows walk in, you decide to be clever and start with zero cows:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, …

You decide that this is much better than using your fingers to count. Instead, you are using a different digit to represent each of the values!

Nine cows have walked through the gate. Here comes one more; what do you write down? Did you say 10? Why? Weren't you using a different symbol for each of the values? Why not keep using a single symbol for each value after 9? You don't have any more symbols you say? Well, why not use letters from the alphabet—those are symbols, too! You could keep counting like this:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, …

Couldn't you do it this way? Couldn't you make up a different symbol to represent each value? But we don't do it that way, you protest. And indeed we don't do it that way.

Symbols

Using one of four seashell shapes to represent nights passed.
Night 0
Night 1
Night 2
Night 3

A sailor named Jane was marooned on an island, and she wanted to keep track of how many days she had been there. She finds four type of seashells: . She decides each day that she will hang a different seashell at the front of her cave to indicate how many nights she has spent on the island.

The day she arrives she hangs out the seashell , which indicates that she hasn't spent any nights on the island. The next day she hangs out , the next day , and the next day . The seashell represents she has spent three nights on the island.

Using two columns of seashells to represent nights passed.
Night 0 ❂❂
Night 1 ❂✩
Night 2 ❂❀
Night 3 ❂✺
Night 4 ✩❂
Night 5 ✩✩
Night 6 ✩❀
Night 7 ✩✺
Night 8 ❀❂
Night 9 ❀✩
Night 10 ❀❀
Night 11 ❀✺
Night 12 ✺❂
Night 13 ✺✩
Night 14 ✺❀
Night 15 ✺✺

With four different symbols to work with, Jane can indicate four different things. If she uses the first symbol ❂ to indicate “no nights”, then with four symbols she can indicate up to 4 - 1 = 3 nights on the island.

But now Jane has no more types of seashells. She decides to use two seashells every day. When she runs out of seashells on one side, she changes the seashell on other side and starts over.

When Jane has four symbols but she uses two of them at a time, she has 4 × 4 = 16 combinations. Using the first combination ❂❂ to indicate the first day (having not spent any nights on the island), she can indicate a total of (4 × 4) - 1 = 15 days.

Number Bases

Island symbols.
Island Symbols: Each person is representing 11 nights using different symbols; the woman is using the symbols “❀✺”, while the man is using the symbols “23” in place of seashells.

Soon another sailor named John is shipwrecked on the same island. John also wants to keep track of how many nights that he has stayed on the island, and he decides to use the same system that Jane uses. Unfortunately Jane won't let John use any of her seashell, so John decides to use the digits 0 = , 1 = , 2 = , 3 = . After 16 days and 15 nights on the island, John's sequence looks exactly like Jane's sequence, except that John is using digits instead of shells.

Using two columns of digits instead of seashells to represent nights passed.
Night 0 00
Night 1 01
Night 2 02
Night 3 03
Night 4 10
Night 5 11
Night 6 12
Night 7 13
Night 8 20
Night 9 21
Night 10 22
Night 11 23
Night 12 30
Night 13 31
Night 14 32
Night 15 33

When John reaches the symbols “33”, does this mean that John has spent thirty-three nights on the island? No, he has only spent fifteen nights on the island. John is merely using the symbols 0, 1, 2, 3; so the representation for the value fifteen is different than you might expect. Because John only uses four symbols, we say he is counting in “number base 4”.

Base 10

The way you are probably accustomed to counting is that you start at symbol 0 and then go up to 9. But then you go back to 0, putting a symbol 1 in the next column. Once you're up to 9 again, you go back to 0 and put a symbol 2 in the next column. You can picture it like this:

Counting in Base 10.
0 0
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
0 9
1 0
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
2 0
combination discs
Exploded view of rotating discs of a simple combination lock.

In reality the number system you are accustomed to is like one of those rotating cylinder combination locks, in which each position has a rotating dial from 0 to 9. Count the number of digits on each dial, from 0 to 9. How many are there? There are 10. Now count how many fingers and thumbs you have on both hands. How many are there? There are 10.

Why do we use a counting system with “rotating dials” with 10 symbols on each “dial”? Why don't we have 5 symbols, or may 12 symbols on each “dial”? Our counting system uses 10 symbols in each position because humans started counting on their 10 fingers. This numbering system is referred to as “base 10” We call it decimal. It's what you use to count every day.

Base 16

Counting in Base 16.
0 0
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
0 9
0 A
0 B
0 C
0 D
0 E
0 F
1 0
1 1
1 2
1 3
1 4

Now let's consider what would happen if humans each had 16 fingers instead of 10. What if each “dial” on the cylinder had 16 positions, counting from 0 to 15? Actually let's use letters to represent values over 9 as in the example above. The figure shows how the dials on the cylinder would turn.

This numbering system is called “base 16” because there are 16 numbers on each “dial” of the cylinder. You can count them: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. The other name for base 16 is hexadecimal or just “hex” for short.

Base 2

There is one other numbering system that is important when discussing computers—perhaps the most important numbering system for computers: base 2

Think back to base 10. If you were to imagine base 10 as a series of turning dials on a cylinder, each dial has the numbers 09. Base 16 had the numbers 0F (with F representing 15). So for each base, the number of digits is 0 – base-1 (that is, one minus the base). So if we were to have a cylinder of numbers representing base 2, what symbols would appear on each dial? Each dial would contain 0 - 1. What? Yes, in base 2 each “dial” (or each column) only contains the digits 0 and 1. But it's not difficult to count in base 2 if you think about a cylinder with turning dials, each dialog containing a 0 and a 1. It would look like this:

Counting in Base 2.
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0

As you can see, each “dial” or column contains so few numbers that they return rapidly to 0 and we need another column. Even small values will quickly need many columns.

Base 2 is the fundamental number base used by computers, and soon you will see why. Base 2 is also known as binary.

Working with the Bases

It might be helpful to see all the number bases counting together. Remember that each row represents a single value, it's just that we're representing that value using different number bases.

Base 10 (Decimal) Base 2 (Binary) Base 16 (Hexadecimal)
00 00000000 00
01 00000001 01
02 00000010 02
03 00000011 03
04 00000100 04
05 00000101 05
06 00000110 06
07 00000111 07
08 00001000 08
09 00001001 09
10 00001010 0A
11 00001011 0B
12 00001100 0C
13 00001101 0D
14 00001110 0E
15 00001111 0F
16 00010000 10
17 00010001 11
18 00010010 12

Bits and Bytes

The reason that base 2, or binary, is so important to computers, is that most digital computers represent values simply by turning an electrical current on or off. Binary comes in handy; an electrical current can represent a 1, while the absence of an electrical current can represent a 0. To represent the decimal value 12, the computer needs the equivalent of four wires in the pattern ON, ON, OFF, OFF to represent the same number in binary, 1100. In fact all values in a computer are ultimately represented conceptually by a series of “wires” with currents on or off. Each 0 or 1 is referred to as a bit, and modern computers address them in groups of eight bits called a byte.

Literal Values in Java

A representation of some actual value such as 5 in a Java program is called a literal value representation or simply a “literal”. If you present a number with no other indication of base, it is usually understood to represent a value using base 10. Java allows you to indicate a hexadecimal number by using the prefix 0x and a binary number by using the prefix 0b. Thus the numbers 11, 0b1011, and 0xB all represent the same value.

Review

Gotchas

In the Real World

Think About It

Self Evaluation

Task

Write a program that, using simple println() statements, prints out the values 1 to 33; first in hexadecimal, and then in binary below. Do not use any counters or variables; type in each line manually. Continue using the same base Java package name you chose for the previous lesson, but use a different subpackage appropriate for this lesson.

Add this new project to an archive and send it to your teacher.

See Also

Acknowledgments