FCPS Java Packets Computer Number Systems Based upon material developed by Sally Bellacqua, Mary Johnson, and Janet Mulloy Revised by Charles Brewer, August 2004 Revised by Shane Torbert, July 2006 Fairfax County Public Schools Fairfax, Virginia CN 1 1. INTRODUCTION Throughout history, different civilizations have used various devices to facilitate computations. The Babylonians, Chinese, and early Europeans used the abacus for calculations. The abacus consists of a divided frame with sliding beads on the top representing fives and sliding beads on the bottom representing ones. Today, devices such as calculators and computers perform a similar function using a combination of Random Access Memory (RAM), Read Only Memory (ROM), a Central Processing Unit (CPU), and a variety of support chips and storage, display, and input devices. 2. BINARY NUMBER SYSTEM Current computer chips have two states, ON or OFF, which conveniently can represent 0 and 1, the two digits in the binary number system. All instructions and data in current computers are represented as a series of 0’s and 1’s. While high-level languages shield the user from the need to input instructions and data in binary form, knowledge of binary arithmetic is useful in understanding how computers operate and their limitations. Since the binary representation of a decimal number can have 64 or more digits, the hexadecimal (base 16) system is sometimes used as a convenient way of expressing the binary number. The table to the right shows how numbers are represented in different bases. Base 10 (decimal) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Base 2 (binary) 0 1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111 10000 10001 10010 10011 Base16 (hexadecimal) 0 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 CN 2 A number in base b can be converted to its equivalent in base 10 by using place values. Place Value Formula b8 b 7 b 6 b5 b 4 b3b 2 b1b 0 .b 1b 2 b 3b 4 Given 1394210, what do the individual digits represent? 13942 10 = 1(10)4 + 3(10)3 + 9(10)2 + 4(10)1 + 2(10)0 = 10,000 + 3000 + 900 + 40 +2 Likewise, the digits in the base 2 number 110101 represent powers of the base 2. 110101 2 = 1(2)5 + 1(2)4 + 0(2)3 + 1(2)2 + 0(2)1 + 1(2)0 = 1(32) + 1(16) + 0(8) + 1(4) + 0(2) + 1(1) = 32 + 16 +0 +4 +0 +1 = 5310 Example: Convert 13F16 to base ten. 13F16 = 1(16)2 + 3(16)1 + F(16)0 = 1(256) + 3(16) + 15(1) = 256 + 48 + 15 = 319 10 Exercise 1 1. How many digits are there in base 8 (octal)? ______ digits, from ____ through ____ 2. Count to eighteen in octal. __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ In problems 3 – 10, convert the base b number to its equivalent in base 10. 3. 159 16 = ________ 10 4. 01101100 2 = ________ 10 5. 3A 16 = _________ 10 6. 10110 2 = _________ 10 7. DC 16 = __________ 10 8. 1010.101 2 = __________ 10 9. 5723 8 = _________ 10 10. 201 3 = ____________ 10 CN 3 2. ADDING IN DIFFERENT BASES Adding in different bases follows the familiar addition rules, including when to carry the number. You just need to pay attention to the base. For example, 7 8 + 38 has a carry operation, namely, 78 + 38 = (1 eight and 2 left over) = 128 As another example, B16 + 616 = 1116 Addition in base 2 is so simple that you only have to memorize three rules: 02 + 0 2 = 0 2 and 1 2 + 02 = 12 and 12 + 12 = 102 Exercise 2. Add in the indicated base. Leave your answers in that base. 1. 0010 1110 2 0011 1011 2 2. 342 8 517 8 3. 3A9 16 21C 16 4. 432 5 123 5 3. MOVING BETWEEN BINARY AND HEXADECIMAL As previously noted, the representation of a decimal number in binary form can require a significant number of digits. For example, 65535 10 = 11111111111111112 These sixteen binary digits can be conveniently expressed as four hexadecimal digits by grouping the binary digits in groups of four and using the equivalent values from the table at the right. Example: 1111 1111 1111 1111 2 = F F F F 16. Similarly, any hexadecimal number can be converted to binary by replacing each hexadecimal digit with its four binary digit equivalent. Example: 3B9 16 = 0011 1011 1001 2 BINARY 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 HEX 0 1 2 3 4 5 6 7 8 9 A B C D E F CN 4 Exercise 3 Convert between binary and hexadecimal. 1. 01011001 2 = _____ 16 2. 54 16 = _____________ 2 3. 01000001 2 = _____ 16 4. 3E 16 = _____________ 2 5. 00110100.0110 2 = _____ 16 6. 6A.5 16 = _______________ 2 4. CONVERTING BASE 10 NUMBERS TO ANOTHER BASE While we are primarily interested in being able to convert base 10 numbers to their binary (or hexadecimal) equivalents, there is a convenient algorithm for the conversion that works for all bases. The “Divide and Save” algorithm is simple to use and easy to implement in a computer program. The technique is to use successive integer division by the desired base until a zero quotient is obtained. The remainders from each division are recorded from right to left as the result. Example: Convert the base ten number 37 to binary. 0 R1 ( one 25 digit ) 21 R0 ( zero 24 digit ) 2 2 R0 ( zero 23 digit ) 2 4 R1 ( one 22 digit ) 29 R0 ( zero 21 digit ) 2 18 R1 ( one 20 digit ) 2 37 Therefore, 3710 1001012 CN 5 Example: Convert the base ten number 111 to hexadecimal 0 R 6 16 6 R (15) E 16 111 Therefore 11110 6 E16 EXERCISE 4 Convert the following base ten numbers to the indicated base number. 1. 25 10 = ___________ 2 2. 73 10 = ____________ 16 3. 65 10 = ___________ 4. 92 10 = ____________ 16 2 5. 32767 10 = _______________________ 2 = _______________________ 16 6. 7 10 = ______________ 3 5. REPRESENTATION OF NEGATIVE INTEGERS Different CPUs use 8, 16, or 32 bits to represent integers. If the integer is too small or too big, the CPU can't handle it. 8 bits or 1 byte 16 bits or 2 bytes 32 bits or 4 bytes Range Minimum integer Maximum integer 8 7 2 or 256 -2 or -128 27-1 or 127 216 or 65535 -215 or -32768 215 -1 or 32767 232 -231 231 -1 It is the programmer's responsibility to make sure that the code never goes outside those upper and lower limits. Most high-level languages, including Java, do not give error messages when the bytes "overflow"—they just keep 127 0111 1111 counting and suddenly the code is producing negative ... ... numbers! 3 0000 0011 The representation of negative integers varies with the CPU. The Intel family of CPUs uses the two’s-complement method to represent negative integers. Using this method a negative integer n is represented (in 8 bits) using the relationship 256 - | n |, where | n | is the absolute value of n. 2 1 0 -1 -2 -3 ... -128 0000 0010 0000 0001 0000 0000 1111 1111 1111 1110 1111 1100 ... 1000 0000 CN 6 For example, -6 10 = (256 - | -6 |)10 = 250 10 = 1111 1010 2 -128 10 = (256 -| -128 |) 10 = 128 10 = 1000 00002 Notice that in the two’s-complement system, all positive integers begin with 0 and negative integers begin with 1. In the binary arithmetic used by the computer, it is a three-step algorithm to express a negative number. For example, to represent –6 in binary: First, change the absolute value of the number to binary | - 6 10 | = 0000 0110 Second, form the one’s complement by interchanging 0s and 1s 1111 1001 Third, add 1 + 1 to form the two’s complement 1111 1010 Example: Convert -128 to its two-complement representation: Step 1 – Change | -128 | to binary Step 2 – Form the one’s complement Step 3 -- Add 1 to form the two’s complement 1000 0000 0111 1111 + 1 1000 0000 For convenience, 8-bit representation of signed integers will be used in the remainder of this unit instead of the 16-bit, 32-bit, or 64-bit representations actually used. The two’s-complement algorithm can of course be used to convert a signed binary number to its signed integer equivalent. Example: What base ten integer does the signed binary number, 10111010, represent? Since the leading bit is a 1, the number must be negative; therefore, the two’scomplement method must be used to find the absolute value. Given signed binary number Step 1 – Form one’s complement Step 2 – Add 1 to form the two’s complement 1011 1010 0100 0101 + 1 0100 0110 Step 3 – Convert the two’s complement to the base ten representation 0100 01102 = 7010 Step 4 – Insert the negative sign. Therefore, 1011 10102 = -7010. CN 7 EXERCISE 5 1 – 4. Form the one’s and two’s complements of the following binary numbers. BINARY NUMBER 1010 1011 ONE’S COMPLEMENT TWO’S COMPLEMENT 0111 0000 0000 0001 0000 0000 5 – 8. Represent each negative integer in 8-bit two’s complement form. DECIMAL INTEGER -1 ABSOLUTE VALUE IN BINARY ONE’S COMPLEMENT TWO’S COMPLEMENT -2 -30 -8 6. ADDING SIGNED BINARY NUMBERS Since the leading bit indicates the sign, would it be possible to use a system where, e.g. 410 = 0000 01002 and – 410 = 1000 01002? While such a system could be used, it is not used because addition in that system is hard. In contrast, the two’s-complement representation eases the performance of binary arithmetic, which is why it is used. Addition becomes relatively simple to perform and as you may have guessed, subtraction is merely the addition of two’s complement. Computers never actually subtract; they just add the two's complement. Example: What is the answer to 10 10 + (– 10 10) ? The 8-bit binary representation of 10 is The 8-bit binary representation of –10 is the two’s-complement Add the binary numbers and discard the overflow digit, the resulting being 0000 0000 2 or 0 10. 0000 1010 0000 1010 1111 0101 +1 1111 0110 1111 0110 1 0000 0000 CN 8 Example: Add these two 8-bit signed binary numbers and then check your work by converting all three numbers to base ten. 1110 1100 + 1101 0110 Add – Add the bits, column by column. If carrying creates an overflow, discard the overflow bit. 1110 1100 + 1101 0110 1 1100 0010 Check – Check the addition by converting the three numbers to base ten to. Since all three are negative in this example, the two’s complement method will be used to find their absolute values. BINARY (1’s comp + 1 = 2’s comp) DECIMAL 1110 1100 0001 0011 1 0001 0100 20 1101 0110 0010 1001 1 0010 1010 42 1100 0010 0011 1101 1 0011 1110 62 Exercise 6 Add these 8-bit signed binary numbers. Check your work by converting all three numbers to base ten. (The check is more work than the original addition problem.) 1. 0000 1001 1111 1100 2. 0001 1100 1011 0101 Represent these decimal numbers as 8-bit signed binary numbers. Add the binary number and then convert each answer back to base ten as a check. 3. -7 + 9 4. -17 + -13 CN 9 6. REPRESENTATION OF REAL NUMBERS Real numbers are represented using one of the standard formats, either float or double. The Institute of Electrical and Electronic Engineers (IEEE) standard for 32-bit or 4-byte representation is used for floats. The standard consists of 3 parts: 1 bit is used for sign information, 8 bits for weighted exponent, and 23 bits for normalized mantissa. Example: Express 7.5 as a float. First, convert the decimal to binary. 7.5 10 = 0111.12 = 1.111 2 * 2 2 Second, determine the weighted exponent by adding 127 to the power of 2 resulting in 129 or in binary form ( 1000 0010 ). Third, create the normalized mantissa (fraction) by dropping the 1 to the left of the binary point and adding zeroes to the right to give 23 digits. The resulting float representation is: 0100 0001 0111 0000 0000 0000 0000 0000 Normalized Mantissa Weighted Exponent Sign Special Values If the exponent (E) is 255 and the fraction (F) is nonzero, the value is NaN for Not a Number. If E=255 and F=0 the value is Infinity or -Infinity, depending on the sign bit. If E=0 and F=0 the value if 0 or -0, depending on the sign bit. If E=0 and F is nonzero the fraction is considered unnormalized (i.e., we assume a 0 in front of the binary point instead of a 1). The largest positive value is when E=254, so the exponent is E-127=127. This is on the order of 2^127=(2^10)^12*2^7=(10^3)^12*128=10^36*10^2=10^38, approximately. The smallest positive value is when E=0 and the fraction is unnormalized. If F is as small as possible we get an exponent of -126 and a fraction of 2^-23. Multiplying this is 2^-149=(2^10)^-15=(10^3)^-15=10^-45, approximately. Positive values smaller than this cannot be represented in a float variable. Underflow occurs, and the resulting value stored is exactly equal to zero. Float values are denser closer to zero, and generally the number of distinct floats between consecutive powers of two is constant. This means that very large values have very large gaps between them, and swamping is possible. CN 10 Double A double uses twice the bits of a float. It has 1 sign bit, 11 exponent bits, and 52 fraction bits. If E=2047 and F is nonzero, the value is NaN for Not a Number. If E=2047 and F=0 the value is Infinity or -Infinity, depending on the sign bit. If E=0 and F=0 the value if 0 or -0, depending on the sign bit. If E=0 and F is nonzero the fraction is considered unnormalized. Its largest value is 2^1023=(2^10)^102*2^3=(10^3)^102*10=10^307, approximately. Its smallest positive value is 2^-1022*2^-52=10^-324, approximately. Code Peculiarities in Java double x = Math.sqrt(-1); System.out.println( x +" " + (x == x) ); NaN double y = 1.0 / 0.0; System.out.println( y +" " + (y == y+ 1) ); Infinity y *= -1.0; System.out.println( y +" " + (y == y + 1) ); -Infinity false true true Evidently not-a-number is not equal to itself. It seems that infinity-plus-one equals one. Notice that NaN, Infinity, and Negative Infinity are actually stored in double variables. Precision The fractional part of the scientific notation determines precision. A double’s fraction is accurate to 2^-52=10^-16. This is why you see 16 digits after the decimal point when you print out, say, a random number from Math.random. Precision determines the spread of values that can be represented in a double, which in turn determines when swamping occurs. One important value is given the special name machine epsilon. This is the smallest number that can be added to 1 and yield a result that is different from 1. For doubles in 32-bit compilers, the machine epsilon is approximately 10^-16, or approximately 2^-52. That is, 1.0 + something less than 10^-16 is indistinguishable from 1.0. Note that machine epsilon is not the smallest number that can be represented, which is 10^-324, or 2^-1074, CN 11 7. MULTIPLICATION IN BINARY The Arithmetic Logic Unit (ALU) in the CPU multiplies binary numbers using a “shift left” and “shift right” logic. The following function uses a multiplying algorithm that illustrates the operation of this shift logic. int result (int x, int y) { int z = 0; while ( y != 0 ) { if (y % 2 == 1) z = z + x; x = 2 * x; y=y/2; } return z; } // If y is odd add x to interim result (z) // Shifts binary number left // Shifts binary number right // When y is 0, return interim result A call to this function with x = 7 and y =13 would return 91 in this way: Iteration 0 1 2 3 4 z 0 7 7 35 91 Decimal x 7 14 28 56 112 y 13 6 3 1 0 z 0000 0000 0000 0111 0000 0111 0010 0011 0101 1011 Binary x 0000 0111 0000 1110 0001 1100 0011 1000 0111 0000 y 0000 1101 0000 0110 0000 0011 0000 0001 0000 0000 8. REPRESENTATIONS OF CHARACTERS Characters are normally represented on PCs using the 8-bit ASCII (American Standard Code for Information Interchange). The ASCII code defines 128 characters from 0 to 127 using the seven of the eight bits in a byte. The extended ASCII set includes the code from 128 to 255 and varies from machine to machine and font to font. A standard for 16-bit representation of characters called Unicode has been created and is used by the Java compiler. This larger data size provides for non-English characters. A table of Unicode characters is on the next page. Note that the Unicode for 'A' is '\u0041' (in hexadecimal) and the unicode for 'a' is '\u0061' (in hexadecimal). What is the code for 'A' in decimal? _____ What is the code for 'a' in decimal? ______ Some mainframe computers use still another format for character representation called EBCDIC (Extended Binary Coded Decimal Interchange Code). CN 12 Unicode, expressed in hexadecimal The Primitive Data Types in Java Real Integers Keyword Size Intel Format Typical Use byte 8-bit two's complement short 16-bit two's complement int 32-bit two's complement for integers elapsed time in milliseconds, financial calculations, large Fibonacci numbers where space is a concern long 64-bit two's complement float 32-bit IEEE 754 double 64-bit IEEE 754 scientific uses char 16-bit Unicode character single letters or characters boolean 1 bit 0, 1 true or false CN 13 9. UNIT REVIEW EXERCISES 1. The octal number system is based on what number? ___________ 2. The largest digit in base 16 is? ___________ 3. What is the base-ten value of the underlined bit in the binary number 0 1 1 0 1 1 1 0? ___________ 4. Express the hexadecimal number FF in base ten. ___________ 5. Express the hexadecimal number 12A in binary. ___________ 6. Find the two’s complement of 1101 0111. ___________ 7. Add these hexadecimal numbers. Leave your answer in hexadecimal. 8. 4F + 68 ___________ 1100 1111 is the signed 8-bit representation of what base ten number? ___________ 9. Express the base ten number 37 in binary. ___________ 10. Express the binary 0101 1011.011 in base sixteen. ___________ 11. Express the base ten number 87 in base sixteen. ___________ 12. Does a base ten number greater than one require more digits than the same number written in binary? ___________ 13. Perform the following addition of two 8-bit signed binary numbers. Check your work by converting each binary number into base ten. 14. Convert the following base ten numbers to binary and add. Check your result by converting the answer back to base ten 1111 0011 0000 1101 - 37 23