Java Reference
In-Depth Information
The weight of the bit immediately to the left of the rightmost bit is 1, the weight of the
bit immediately to the left of it is 2, and so on. Consider the binary number 1001101.
The weight of each bit is as follows:
Weight
6
5 43210
1001101
We use the weight of each bit to find the equivalent decimal number. For each bit, we
multiply the bit by 2 to the power of its weight and then we add all of the numbers. For
the above binary number, the equivalent decimal number is:
1 2 6 þ 0 2 5 þ 0 2 4 þ 1 2 3 þ 1 2 2 þ 0 2 1 þ 1 2 0
¼ 64 þ 0 þ 0 þ 8 þ 4 þ 0 þ 1
¼ 77 :
Converting a Binary Number (Base 2) to Octal (Base 8)
and Hexadecimal (Base 16)
The previous sections described how to convert a binary number to a decimal number (base 2).
Even though the language of a computer is binary, if the binary number is too long, then it will
be hard to manipulate it manually. To effectively deal with binary numbers, two more number
systems, octal (base 8) and hexadecimal (base 16), are of interest to computer scientists.
The digits in the octal number system are 0, 1, 2, 3, 4, 5, 6, and 7. The digits in the
hexadecimal number system are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F. So A in
hexadecimal is 10 in decimal, B in hexadecimal is 11 in decimal, and so on.
The algorithm to convert a binary number into an equivalent number in octal (or
hexadecimal) is quite simple. Before we describe the method to do so, let us review
some notations. Suppose a b represents the number a to the base b. For example, 2A0 16
means 2A0 to the base 16, and 63 8 means 63 to the base 8.
First, we describe how to convert a binary number into an equivalent octal number and
vice versa. Table D-1 describes the first 8 octal numbers.
TABLE D-1 Binary Representation of First 8 Octal Numbers
Binary
Octal
Binary
Octal
000
0
100
4
001
1
101
5
010
2
110
6
011
3
111
7
 
Search WWH ::




Custom Search