Java Reference
In-Depth Information
Table 3-5. Decimal-to-Binary Conversion
Number
Divided by 2
Result
Remainder
13
13/2
6
1
6
6/2
3
0
3
3/2
1
1
1
1/2
0
1
The binary representation of decimal number 13 is 1101. A byte variable in Java occupies one byte. The value 13
in a byte variable is stored as 00001101. Note that four zeros are added in front of the binary representation 1101
because a byte variable always occupies 8 bits irrespective of the value that it contains. The rightmost bit in a byte or
a word is known as the least significant bit (LSB) and the leftmost bit as the most significant bit (MSB). The MSB and
LSB for the binary representation of 13 are shown in Figure 3-5 .
Figure 3-5. MSB and LSB in a binary number
Each bit in a binary number is assigned a weight, which is a power of 2. A binary number can be converted to its
decimal equivalent by multiplying each bit in the binary number by its weight and adding them up. For example, 1101
in binary can be converted to its decimal equivalent as follows:
(1101) 2 = 1 x 2 0 + 0 x 2 1 + 1 x 2 2 + 1 x 2 3
= 1 + 0 + 4 + 8
= (13) 10
Java stores the negative integral numbers in 2's complement form. Let's discuss the complement of a number in a
given number system. Every number system has a base, also known radix. For example, 10 is the radix for the decimal
number system, 2 is for the binary number system, and 8 is for the octal number system. We will use the symbol R for
radix. Every number system defines two types of complements:
Diminished radix complement (also known as (R-1)'s complement)
Radix complement (also known as R's complement)
Therefore, for the decimal number system, we have 9's complement and 10's complement; for the octal number
system, we have 7's and 8's complements, and for the binary number system, we have 1's and 2's complements.
Diminished Radix Complement
Let N be a number in a number system with radix R and n is the total number of digits in the number N. The diminished
radix complement or (R-1)'s complement of the number N is defined as (R n -1) - N .
In the decimal number system, the 9's complement of a number is (10 n -1) - N . Since 10 n consists of 1 followed by
n zeros, (10 n -1) consists of n 9s. Therefore, 9's complement of a number can be computed simply by subtracting each
digit in the number from 9. For example, 9's complement of 5678 is 4321 and 9's complement of 894542 is 105457.
 
Search WWH ::




Custom Search