Values
Goals
- Understand the distinction between values and representations.
- Understand the difference between base 2 (binary), base 10 (decimal), and base 16 (hexadecimal); and be able to convert values between these bases using a calculator.
- Be able to indicate to Java which number base is being used.
- Gain an understanding of the importance of binary bits and bytes.
Concepts
- binary
- bit
- byte
- decimal
- hexadecimal
- literal value representation
- octal
- representation
- symbol
- value
Language
0b
0x
Lesson
Representations
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:
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:
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
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.
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
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.
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:
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 |
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
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 0
– 9
. Base 16 had the numbers 0
– F
(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:
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
- If you put a
0
in front of a decimal number, Java will think it is base 8, or octal, producing a different value than you intended.
In the Real World
- Nobody uses octal, which is base 8. You can forget about octal, as long as you remember not to accidentally put a
0
prefix on a number. - Since the different number bases are all different ways to represent the same value, choose the representation that matches most closely the thing you are trying to represent. If you want to represent two bytes with all bits turned on, you might use
0b1111111111111111
or0xFFFF
to make this clear. Otherwise, if you were to write65535
, the significance of its relationship to the underlying bits would be lost.
Think About It
- Did I use the correct prefix so that Java will understand which number base I am using?
Self Evaluation
- What is the binary representation of the hexadecimal number
FACE
? - What base is being used in the representation
0b1111
? - What is the decimal representation of
0x123
? - If there were a base 5 number system, what would the value
12
in base 5 represent in base 10?
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
- Binary and hexadecimal number systems (Khan Academy)
- conversion of decimal number to binary/hexadecimal/octal number and vice versa using a calculator (YouTube - Achla Agarwal)
Acknowledgments
- Images of hands showing fingers by Icons8 licensed under Creative Commons Attribution-NoDerivs 3.0 Unported.
- Cow Quantity and Island Symbols cartoons drawn by Jason J. Patterson.
- Combination disc image modified from illustration of a simple combination lock by Wapcaplet and licensed under Creative Commons Attribution-Share Alike 3.0 Unported.