Wednesday June 7, 2000
Quiz 2
1. What is the special relationship between the binary, octal, and
hexadecimal number sytems?
2. How many bytes are in a kilobyte?
3. What does ROM stand for?
4. What is the decimal equivalent of the binary number 11?
5. Does the following number make sense?
4552
Yesterday we discussed conversions between number systems.
Binary -> Decimal
The strategy used to compute the decimal equivalent of a binary number is
very straightforward. Consider a binary number with n digits.
bn-1 bn-2 bn-3
... b0
Each digit, bi, is either 0 or 1.
Notice that the leading digit is labeled bn-1. This is because
the right-most position is considered position 0. So if there are
n digits in a number, we label the leftmost digit as n-1.
This is a convention that you need to become familiar with because Java,
like C++, starts counting at 0 instead of 1.
The rule for converting this binary number to a decimal number is to form
a sum whose terms are the individual digits, bi, multiplied by
the base, 2, raised to the power of the position. That is to say, the
decimal equivalent of this number is
bn-1 * 2n-1 + bn-2*2n-2 +
bn-3 * 2n-3 + ... + b0 * 20
For example, suppose we want to convert the binary number 1010 to its
decimal equivalent. Notice that since there are four digits in this
number, the leftmost position is position 3. So the decimal equivalent is
1 * 23 + 0 * 22 + 1 * 21 + 0 *
20 = 10.
Whenever a number makes sense in more than one number system, then we
normally use a subscript to identify which number system we are using.
When we leave off a subscript we will assume that the number is in the
decimal number system.
So since 1010 also makes sense as a decimal number, to identify it as a
binary number, I would write 10102.
So 10102 = 1010 = 10.
Decimal -> Binary
The conversion from decimal to binary is not as straightforward. In order
to accomplish the conversion consider how the binary number was converted
to decimal. The right-most digit of the number is the highest power of 2
that took place in the sum. So to begin the conversion we determine what
is the highest power of 2 <= the number in question. After substracting
that power of 2 from the number we continue the process until we have
determined all of the binary digits in the equivalent.
For example, consider converting the decimal number 10 to its binary
equivalent.
The highest power of 2 <= 10 is 8. Since this is the third power of 2, we
know that there will be 4 digits in the binary equivalent of 10. After
substracting 8 from 10 we are left with 2. The second power of 2, 4, is
less that this number, so the coefficient in the next position of the
binary equivalent is 0. The next power of 2, 2, divides the remainder 1
time with 0 remainder. So the third digit in the equivalent is 1. The next
power of 2, 1, does not divide the remainder so in that position of the
equivalent we have a 0.
So the binary equivalent of 10 is 10102.
We can make this process easier by considering a sequence of divisions. We
begin with the highest power of 2 <= 10.
10 = 1*8 + 2. The quotient, 1, is the leftmost digit of the binary
equivalent.
Now we take the remainder two and consider the next smallest power of 2.
2 = 0*4 + 2. The quotient, 0, is the next digit in the binary equivalent.
We now consider the next smallest power of 2.
2 = 1*2 + 0. The quotient, 0, is the next digit in the binary equivalent.
Since there is only one power of 2 left, the remainder in the last
division is the final digit in the equivalent.
Now let us look at a slightly more difficult example.
Consider converting the decimal number 230 to its binary equivalent.
The highest power of 2 <= 230 is 128 which is 27. So there are
8 digits in the binary equivalent of 230.
230 = 1*128 + 102.
102 = 1*64 + 38.
38 = 1*32 + 6.
6 = 0*16 + 6.
6 = 0*8 + 6.
6 = 1*4 + 2.
2 = 1*2 + 0.
So reading off the coefficients, the binary equivalent is
111001102
Notice the remainder of the last division is the final digit in the
equivalent.
Binary -> Octal
The Binary to Octal conversion is very simple. First let us consider what
the binary equivalents of each of the octal digits is.
| 08 |
= |
0002 |
| 18 |
= |
0012 |
| 28 |
= |
0102 |
| 38 |
= |
0112 |
| 48 |
= |
1002 |
| 58 |
= |
1012 |
| 68 |
= |
1102 |
| 78 |
= |
1112 |
Notice that these digits are the same in any base in which they make
sense.
To convert a binary number to its octal equivalent, we begin at the right
of the number, and group the digits in sets of 3 each. If we reach the
left most digit and do not have enough digits to make a full set of 3,
then we left pad the number with 0's.
For example, in the binary number 1010, we would begin at the right and
group the digits in sets of 3. The first set would be 010. Notice that
since there are 4 digits, there aren't enough digits to make groups of 3,
so we left pad this binary number with two 0's. So the number would be
001010, and the two groups of three digits would be 001 and 010. We then
look at the table of the binary equivalents of the octal digits.
We replace each group of 3 digits with its octal equivalent.
So the octal equivalent of 1010 or 001010 would be 128.
Octal -> Binary
To convert octal to binary we again use the table. Take each octal digit
and replace it with its binary equivalent.
So 128 = 0010102.
Binary -> Hexadecimal
Let us consider the binary equivalents of each hexadecimal digit.
| 016 |
= |
00002 |
| 116 |
= |
00012 |
| 216 |
= |
00102 |
| 316 |
= |
00112 |
| 416 |
= |
01002 |
| 516 |
= |
01012 |
| 616 |
= |
01102 |
| 716 |
= |
01112 |
| 816 |
= |
10002 |
| 916 |
= |
10012 |
| A16 |
= |
10102 |
| B16 |
= |
10112 |
| C16 |
= |
11002 |
| D16 |
= |
11012 |
| E16 |
= |
11102 |
| F16 |
= |
11112 |
To convert binary to hexadecimal we follow exactly the same procedure as
converting binary to octal except we group the digits in sets of 4.
To convert 1111011 to it hexadecimal equivelent, we group the digits in
sets of 4. Since there are 7 digits, we left pad the number with 1 0. So
the two sets of 4 digits are 0111 1011. We then replace each group with
its hexadecimal equivalent.
So 11110112 = 7B16.
Hexadecimal -> Binary
The conversion from hexadecimal to binary is also straightforward. We
replace each hexadecimal digit with its binary equivalent.
So 7B16 = 011110112.
It is important that we understand these number systems because they occur
in many aspects of computers.
For example, a binary number with 2 digits can be used to represent 4
distinct states.
00
01
10
11
Likewise a binary number with 3 digits can be used to represent 8 distinct
cases.
000
001
010
011
100
101
110
111
In general, a binary number with n digits can be used to represent
2n distinct states.
Now returning to our beginning discussion of Java, let us again consider
the process we go through to create an executable program.
- We first create a file containing source code.
- We then run the program javac which compiles the code into Java
bytecode. This bytecode is platform independent.
- We then run the interpreter java which converts the Java bytecode into
executable machine code and executes it.
Notice here how the platform independence makes Java well suited for the
Internet. There are many different platforms that people around the world
use to access the Internet. However on any platform running Netscape you
can download a Java program, called a Java applet, and have it execute on
the client machine.
This is because a Java interpreter is built into the Netscape browser. The
interpreter is machine dependent because it must convert the bytecode into
code which will execute on that platform.
Let us consider the simple program we use in the laboratory.
public class Example1 {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Example1.java