1.6. Java, the World Wide Web, and Beyond

 
[Page 11 ( continued )]

1.5. (Optional) Number Systems

Note

You can skip this section and use it as reference when you have questions regarding binary and hexadecimal numbers .


Computers use binary numbers internally because storage devices like memory and disk are made to store 0s and 1s. A number or a character inside a computer is stored as a sequence of 0s and 1s. Each 0 or 1 is called a bit . The binary number system has two digits, 0 and 1.

Since we use decimal numbers in our daily life, binary numbers are not intuitive. When you write a number like 20 in a program, it is assumed to be a decimal number. Internally, computer software is used to convert decimal numbers into binary numbers, and vice versa.

You write programs using decimal number systems. However, if you write programs to deal with a system like an operating system, you need to use binary numbers to reach down to the "machine-level." Binary numbers tend to be very long and cumbersome. Hexadecimal numbers are often used to abbreviate binary numbers, with each hexadecimal digit representing exactly four binary digits. The hexadecimal number system has sixteen digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F. The letters A, B, C, D, E, and F correspond to the decimal numbers 10, 11, 12, 13, 14, and 15.

The digits in the decimal number system are 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. A decimal number is represented using a sequence of one or more of these digits. The value that each digit in the sequence represents depends on its position. A position in a sequence has a value that is an integral power of 10. For example, the digits 7, 4, 2, and 3 in decimal number 7423 represent 7000, 400, 20, and 3, respectively, as shown below:


The decimal number system has ten digits, and the position values are integral powers of 10. We say that 10 is the base or radix of the decimal number system. Similarly, the base of the binary number system is 2 since the binary number system has two digits, and the base of the hex number system is 16 since the hex number system has sixteen digits.

1.5.1. Conversions Between Binary Numbers and Decimal Numbers

Given a binary number b n b n - 1 b n-2 b 2 b 1 b , the equivalent decimal value is

b n x 2 n + b n-1 x 2 n-1 + b n-2 x 2 n-2 + ... + b 2 x 2 2 + b 1 x 2 1 + b x 2

The following are examples of converting binary numbers to decimals:

Binary Conversion Formula Decimal
10 1 x 2 1 + 0 x 2 2
1000 1 x 2 3 + 0 x 2 2 + 0 x 2 1 + 0 x 2 8
10101011 1 x 2 7 + 0 x 2 6 + 1 x 2 5 + 0 x 2 4 + 1 x 2 3 + 0 x 2 2 + 1 x 2 1 + 1 x 2 171


[Page 12]

To convert a decimal number d to a binary number is to find the bits b n , b n-1 , b n-2 , ..., b 2 , b 1 , and b such that

d = b n x 2 n + b n-1 x 2 n-1 + b n-2 x 2 n-2 + ... + b 2 x 2 2 + b 1 x 2 1 + b x 2

These bits can be found by successively dividing d by 2 until the quotient is 0. The remainders are b , b 1 , b 2 , ..., b n-2 , b n-1 , and b n .

For example, the decimal number 123 is 1111011 in binary. The conversion is done as follows :


Tip

The Windows Calculator, as shown in Figure 1.7, is a useful tool for performing number conversions. To run it, choose Programs , Accessories , and Calculator from the Start button.


Figure 1.7. You can perform number conversions using the Windows Calculator.

1.5.2. Conversions Between Hexadecimal Numbers and Decimal Numbers

Given a hexadecimal number h n h n-1 h n-2 ... h 2 h 1 h , the equivalent decimal value is

h n x 16 n + h n-1 x 16 n-1 + h n-2 x 16 n-2 + ... + h 2 x 16 2 + h 1 x 16 1 + h x 16

The following are examples of converting hexadecimal numbers to decimals:

Hexadecimal Conversion Formula Decimal
7F 7 x 16 1 + 15 x 16 127
FFFF 15 x 16 3 + 15 x 16 2 + 15 x 16 1 + 15 x 16 65535
431 4 x 16 2 + 3 x 16 1 + 1 x 16 1073


[Page 13]

To convert a decimal number d to a hexadecimal number is to find the hexadecimal digits h n , h n-1 , h n-2 , ..., h 2 , h 1 , and h such that

d = h n x 16 n + h n-1 x 16 n-1 + h n-2 x 16 n-2 + ... + h 2 x 16 2 + h 1 x 16 1 + h x 16

These numbers can be found by successively dividing d by 16 until the quotient is 0. The remainders are h , h 1 , h 2 , ..., h n-2 , h n-1 , and h n .

For example, the decimal number 123 is 7B in hexadecimal. The conversion is done as follows:


1.5.3. Conversions Between Binary Numbers and Hexadecimal Numbers

To convert a hexadecimal number to a binary number, simply convert each digit in the hexadecimal number into a four-digit binary number using Table 1.1.

Table 1.1. Converting Hexadecimal to Binary
Hexadecimal Binary Decimal
1 1 1
2 10 2
3 11 3
4 100 4
5 101 5
6 110 6
7 111 7
8 1000 8
9 1001 9
A 1010 10
B 1011 11
C 1100 12
D 1101 13
E 1110 14
F 1111 15

For example, the hexadecimal number 7B is 1111011, where 7 is 111 in binary, and B is 1011 in binary.


[Page 14]

To convert a binary number to a hexadecimal, convert every four binary digits from right to left in the binary number into a hexadecimal number.

For example, the binary number 1110001101 is 38D, since 1101 is D, 1000 is 8, and 11 is 3, as shown below.


Note

Octal numbers are also useful. The octal number system has eight digits, 0 to 7. A decimal number 8 is represented as 10 in the octal system.


 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net