Java Reference
In-Depth Information
Numeric data
Numberscanberepresentedinacomputersystembymeansofthecorre-
spondingcharacters.IntheASCIIcharacterset,thenumber128isrepre-
sentedbythreebytesholdingthedecimalvalues49,50,and56.Youcan
confirmthesecodesintheASCIItablein Figure3-3 . Althoughnumericdata
is sometimes stored as characters, this representation of numbers is ineffi-
cient and awkward. Computers are binary machines and perform arithme-
tic operations only on binary numbers. This means that to multiply the
number 128, encoded in ASCII digits, by the number 2, also in ASCII, the
computer would first have to convert these text-like representations into
binary. A more reasonable approach is to store numbers directly in binary.
For example, the number 128 can be stored as the binary value
10000000
First note that in binary the number 128 is stored in one byte, while it
requires three bytes if it is represented in ASCII characters. More impor-
tantly, binary arithmetic can be performed directly on the encoding with-
out having to perform conversions.
Computer systems use straight binary representation for unsigned in-
teger numbers. However, how would you represent negative and positive
numbers in binary? One possible scheme is to devote a binary digit to rep-
resent the sign. By convention, a binary 0 represents a positive number,
and a binary 1 represents a negative number. Usually the leftmost bit,
called the high-order bit, is devoted to the sign. This arrangement is
sometimes called a sign-magnitude representation . For example, the
decimal numbers +93 and -93 are represented as follows:
01011101 = +93 decimal
11011101 = -93 decimal
Note that the leftmost bit is zero for +93 and it is 1 for -93. The remain-
ing binary digits are the same in both numbers.
Sign-magnitude representation, although simple and straightforward,
has its drawbacks. One of them is that there are two encoding for zero,
one negative and one positive, as follows:
00000000 = positive zero
10000000 = negative zero
The representation for negative zero is usually unnecessary, since zero
is neither positive nor negative. Furthermore, sign-magnitude representa-
tions make arithmetic complicated. To add two signed numbers you first
have to determine if they have the same sign or different signs. If the sign
Search WWH ::




Custom Search