Java Reference
In-Depth Information
HEXADECIMAL DECIMAL BINARY
B
11
1011
C
12
1100
D
13
1101
E
14
1110
F
15
1111
Because a hexadecimal digit corresponds exactly to four binary bits, you can represent a binary number
as a hexadecimal number just by taking successive groups of four binary digits starting from the right, and
writing the equivalent base 16 digit for each group. Look as this binary number:
1111 0101 1011 1001 1110 0001
If you replace each group of four bits with the equivalent hexadecimal digit, you get:
F5B9E1
You have six hexadecimal digits corresponding to the six groups of four binary digits. Just to show it all
really works out with no cheating, you can convert this number directly from hexadecimal to decimal, by
again using the analogy with the meaning of a decimal number, as follows:
F5B9E1 is:
15 × (16 × 16 × 16 × 16 × 16) +
5 × (16 × 16 × 16 × 16) +
11 × (16 × 16 × 16) +
9 × (16 × 16) + 14 × (16) + 1
This in turn turns out to be:
15,728,640 + 327,680 + 45,056 + 2304 + 224 + 1
which fortunately totals to the same number that you got when you converted the original binary number to
a decimal value.
Octal Numbers
In Chapter 2 you read that you can define octal integer literals, which are integers to base 8. These were
invented by Charles XII of Sweden in 1717, along with the idea of representing numbers to base 64, but this
is not the reason they appear in Java. The octal representation is there because historically computers stored
binary integers in units that were multiples of 3 bits, the early-1960's vintage IBM 7090 with 36-bit words
to store integers being just one example where 12 octal digits could be used to specify a 36-bit binary value.
Some of the programming languages of the time offered the economy of writing binary values as octal. As
programming languages evolved, the octal representation was carried along down through the years from
one language to the next bigger, better, and more powerful language, right up to today. There they are, octal
numbers, still lurking in C, C++, and now Java, even though they have very little purpose.
Octal digits can have the values from 0 to 7 and computation is exactly analogous to hexadecimal arith-
metic, with 8 instead of 16 as the base. Octal literals are easily confused with decimal integers. The only dif-
ferentiation is the leading 0 (zero) in the representation of octal literals in Java. Unless you have discovered
a situation where they are essential, or you enjoy a perverse approach to programming, octal literals are best
avoided.
Search WWH ::




Custom Search