Java Reference

In-Depth Information

Note that by using just 7 bits, you can represent all the decimal numbers from 0 to 127, which is a total

of 2
7
, or 128 numbers; and using all 8 bits you get 256, which corresponds to 2
8
numbers. In general, if you

have
n
bits available, you can represent 2
n
positive integers with values from 0 to 2
n
−
1
. Of course, I am only

talking in the context positive numbers so far. If you also need the same number of negative numbers, you

need more bits. I get to negative numbers in a moment.

Hexadecimal Numbers

When you get to work with larger binary numbers — for example, numbers with 24 bits:

1111 0101 1011 1001 1110 0001

the notation starts to be a little cumbersome, particularly when you consider that if you apply the method you

saw in the previous section to work out what this is in decimal notation, it's only 16,103,905, a miserable 8

decimal digits. You can sit more angels on a pinhead than that. Well, as it happens, you have an excellent

alternative.

Arithmetic to base 16 is a very convenient option. Numbers to base 16 are
hexadecimal
numbers. Each

digit can have values from 0 to 15 (the digits from 10 to 15 being represented by the letters
A
to
F
as shown

in
Table B-2
,
or by
a
to
f
if you're averse to capitalization) and values from 0 to 15 correspond quite nicely

with the range of values that four binary digits can represent.

0

0000

1

1

0001

2

2

0010

3

4

0011

4

4

0100

5

5

0101

6

6

0110

7

7

0111

8

8

1000

9

9

1001

A

10

1010