Computer Number Systems
Computer Number Systems
Computer Number Systems
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
last revision May 2010
1. INTRODUCTION
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. The binary number system easily translates into the octal (base 8)
and the hexadecimal (base 16) system. Programmers need to think in all three
number systems.
P la c e V a lu e F o rm u la
b 8 b 7 b 6 b 5 b 4 b 3b 2 b1b 0 . b −1b −2 b −3b −4
Likewise, the digits in the base 2 number 110101 represent powers of the base 2.
Examples:
1) Convert 13F16 to base ten. 2) Convert 0.23 to base 10
13F16 = 1(16)2 + 3(16)1 + F(16)0 the place value of the "2" is 3-1.
a negative exponent means a fraction 2
= 1(256) + 3(16) + 15(1) 3
= 256 + 48 + 15 2
= 319 10 therefore, 0.23 = = 0.6666667 10
3
Exercise 1
1. How many digits are there in base 8 (octal)? ______ digits, from ____ through ____
In problems 3 – 10, convert the base b number to its equivalent in base 10.
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, 78 + 38 has a carry
operation, namely, 78 + 38 = 1010 = (1 group of 8 and 2 left over) = 128
carry sum
As another example,
B16 + 616 = 1110 + 610 = 1710 = (1 group of 16 and 1 left over) = 1116
Addition in base 2 is so simple that you only have to memorize three rules:
02 + 02 = 02 and 12 + 02 = 12 and 12 + 12 = 102
Exercise 2
Add in the indicated base. Then check your answers for 1 – 5 by doing the work in the
decimal base (base 10).
1. 12 2. 11 2 3. 112 4. 1112
+ 12 + 01 2 +112 +0112
Exercise 3
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. On paper, you start by dividing from the
bottom: Now you try. Convert 2510 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
2 │25
Example:
6 R6
Convert the base ten number 110 to hexadecimal. ____
16 | 6 R 14 Æ E
______
16 │110
Therefore, 11010 = 6E16
Exercise 4
Convert the following base ten numbers to the indicated base number.
1. 25 10 = ___________ 2 2. 73 10 = ____________ 16
3. 65 10 = ___________ 2 4. 92 10 = ____________ 16
6. 7 10 = ______________ 3
So far, we have only talked about positive integers. How should we represent both
positive and negative (or “signed”) numbers?
The computer actually calculates the negative number using a different algorithm. For
example, to represent –6 in binary:
First, change the absolute value of the number to binary | - 6 10 | = 0000 0110
Example:
Convert -128 to its two-complement representation:
Step 1 – Change | -128 | to binary 1000 0000
In this unit, we will use 8-bits instead of the 32-, or 64-bits that today’s computers
actually use. The Apple II and other personal computers of the 1970’s were 8-bit
computers.
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’s-
complement method must be used to find the absolute value.
EXERCISE 5
1 – 4. Form the one’s and two’s complements of the following signed binary numbers.
Any time you enter, e.g. -2, we have seen that two's complement system translates
that into 1111 1110. This may seem complicated, but in this system, *all* arithmetic
operations (+ - * / % √ ) become easier. Addition is just the addition of bits.
Subtraction is the addition of two’s complement. (Computers and calculators don’t
actually subtract; they just add the two's complement.) Furthermore, multiplication is
repeated addition. Division is repeated two’s complement addition. No wonder
programmers like the two’s complement system.
Example:
Example:
Add these two 8-bit signed binary numbers and 1110 1100
then check your work by converting all three + 1101 0110
numbers to base ten.
11111
Add – Add the bits, column by column, carrying. 1110 1100
If the sum overflows 8 bits, discard + 1101 0110
the overflow bit. 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 (see Section 5) will
be used to find their absolute values.
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.)
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 4. -17
+ 9 + -13
CN 9
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.
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.
double x = Math.sqrt(-1);
System.out.println( x +" " + (x == x) ); NaN false
y *= -1.0;
System.out.println( y +" " + (y == y + 1) ); -Infinity 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.
A call to this function with x = 7 and y =13 would return 91 in this way:
Decimal Binary
Iteration z x y z x y
0 0 7 13 0000 0000 0000 0111 0000 1101
1 7 14 6 0000 0111 0000 1110 0000 0110
2 7 28 3 0000 0111 0001 1100 0000 0011
3 35 56 1 0010 0011 0011 1000 0000 0001
4 91 112 0 0101 1011 0111 0000 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