Java Reference

In-Depth Information

(10110)
2

The base is
2
. The rightmost digit (or bit),
0
, is in the
units
position, the
1
to its

left is in the
twos
position, the
1
to its left is in the
fours
position, the
0
to its left

is in the
eights
position, and the leading
1
is in the
sixteens
position. Because the

base is
2
, this number is a representation for the integer that is the value of this

expression:

1*2
4
+ 0*2
3
+ 1*2
2
+ 1*2
1
+ 0*2
0

In general, a positive integer in the base
2
system has the form

(d
k-1
...d
1
d
0
)
2
,

where each
d
i
is either 0 or 1 and
d
k-1
=1
. Thus, k is the number of bits need-

ed to represent the integer without leading zeros. The value
(d
k-1
...d
1
d
0
)
2
is:

d
k-1
*2
k-1
+ ... + d
1
*2
1
+ d
0
*2
0,

i.e.

∑
i in 0..k-1
d
i
*2
i

In keeping with this representation, we represent zero by
k=0
, i.e. with
0
bits.

The connection between binary, octal, and hexadecimal

Below, we show the first
eight
integers in binary and, underneath them,

their representation in octal:

000 001 010 011 100 101 110 111
(binary)

0 1 2 3 4 5 6 7
(octal)

Based on this description, you can see that to produce the octal representa-

tion from the binary representation, just replace each sequence of three bits

(starting with the least significant) by its octal representation. For example:

(101011)
2
is (53)
8

(1110101111)
2
is (1657)
8

In the same way, we can translate from binary to hexadecimal by replacing

each four-bit segment (starting with the least-significant bit, i.e. starting at the

right) by its one-character hexadecimal equivalent. Here are examples:

(1011)
2
is (B)
16

(101011)
2
is (2B)
16

(1011001110101111)
2
is (B3AF)
16

We often write numbers in hexadecimal rather than binary notation, even

though they are maintained in binary in a computer, because the hexadecimal

notation is more compact and easy to read and because we can easily translate

from one notation to the other.

For example, in a Java program, we can use the Unicode literal
'\u0041'
to

Search WWH ::

Custom Search