In this lesson, we introduce the key number systems that Java programmers use, especially when they’re working on software
projects that require close interaction with machine-level hardware. Projects like this include operating systems, computer
networking software, compilers, database systems and applications requiring high performance. Numbers can be represented in a
variety of ways. The representation depends on what is called the BASE. The following are the four most common
representations.
1. Decimal
We normally represent numbers in their decimal form. Numbers in decimal form are in base 10. This means that the only
digits that appear are 0-9. Here are examples of numbers written in decimal form:
12610 (normally written as just 126)
1110 (normally written as just 11)
2. Binary
Numbers in binary form are in base 2. This means that the only legal digits are 0 and 1. The subscript 2 is needed to
indicate that the number is a binary number. Here are examples of numbers written in binary form:
11111102
10112
3. Octal
Numbers in octal form are in base 8. This means that the only legal digits are 0-7. The subscript 8 is needed to indicate
that the number is an octal number. Here are examples of numbers written in octal form:
1768
138
4. Hexadecimal
Numbers in hexadecimal form are in base 16. The subscript 16 is needed to indicate that the number is a hexadecimal
number. 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.Here are examples of numbers
written in hexadecimal form:
7E16
B16
Conversions of Number System
1. Decimal to Binary / Binary to Decimal
To convert a decimal number to binary, continuously divide the number by 2 and get the remainder (which is either 0 or
1), and get that number as a digit of the binary form of the number. Get the quotient and divide that number again by 2 and
repeat the whole process until the quotient reaches 0 or 1. Get all the remainders starting from the last remainder, and the
result is the binary form of the number.
NOTE: For the last digit which is already less than the divisor (which is 2) just copy the value to the remainder portion.
Example: Convert 12610 = ____ 2.
Writing the remainders from the bottom up, we get the binary number 1111110 2.
To convert a binary number to decimal, multiply the binary digit to "2 raised to the position of the binary number", then
add all the products to get the resulting decimal number.