G.1. Number Systems

Binary, Octal, and Hexadecimal Number System

Table G.1 lists the integers from 0 to 16, showing their equivalents in the binary (base 2), octal (base 8), and hexadecimal (base 16) number systems. The shaded cells in each column show the digits in each number system.

Table G.1. Number Systems
Decimal (base 10)Binary (base 2)Octal (base 8)Hexadecimal (base 16)
0000
1111
21022
31133
410044
510155
611066
711177
81000108
91001119
10101012a
11101113b
12110014c
13110115d
14111016e
15111117f
16100002010

In addition to the decimal literals, Java also allows integer literals to be specified in octal and hexadecimal number systems, but not in the binary number system. Octal and hexadecimal numbers are specified with 0 and 0x prefix, respectively. The prefix 0X can also be used for hexadecimal numbers. Note that the leading 0 (zero) digit is not the uppercase letter O. The hexadecimal digits from a to f can also be specified with the corresponding uppercase forms (A to F). Negative integers (e.g., -90) can be specified by prefixing the minus sign (-) to the magnitude, regardless of number system (e.g., -0132 or -0X5A). The actual memory representation of the integer values is discussed in Section on page 598.

Converting Binary Numbers to Decimals

A binary number can be converted to its equivalent decimal value by computing the positional values of its digits. Each digit in the binary number contributes to the final decimal value by virtue of its position, starting with position 0 (units) for the right-most digit in the number. The positional value of each digit is given by


digit × base position

The number 1010012 corresponds to 4110 in the decimal number system:

1010012= 1×25 + 0×24 + 1×23 + 0×22 + 0×21 + 1×20
 = 32 + 0 + 8 + 0 + 0 + 1
 = 4110

Converting Octal and Hexadecimal Numbers to Decimals

Similarly, octal (base 8) and hexadecimal (base 16) numbers can be converted to their decimal equivalents:

0132 = 1328= 1×82 + 3×81 + 2×80= 64 + 24 + 2= 9010Octal → Decimal
0x5a = 5a16= 5×162 + a×160= 80 + 10= 9010Hex → Decimal

The same technique can be used to convert a number from any base to its equivalent representation in the decimal number system.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.46.69