Download appendix A

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

History of logarithms wikipedia , lookup

Large numbers wikipedia , lookup

Elementary arithmetic wikipedia , lookup

Approximations of π wikipedia , lookup

Location arithmetic wikipedia , lookup

Elementary mathematics wikipedia , lookup

Addition wikipedia , lookup

Arithmetic wikipedia , lookup

Positional notation wikipedia , lookup

Transcript
APPENDIX A
BINARY NUMBERS
Finite-precision numbers







On most computers, the amount of memory available for storing a number is fixed at the time that the
computer is designed.
The finite nature of computer forces us to deal only with numbers that can be represented in a fixed
number of digits. We call such numbers finite-precision numbers.
In order to study properties of finite-precision numbers, let us examine the set of positive integers
representable by 3 decimal digits, with no decimal point and no sign.
o This set has exactly __________ numbers:
o With this restriction, it is impossible to express certain kinds of numbers, such as:
One important property of arithmetic on the set of all integers is closure with respect to the operations
of addition, subtraction, and multiplication.
o In other words, for every pair of integers i and j, i+j, i-j, ixj are also integers.
o The set of integers is not closed with respect to division, because
Finite precision numbers are not closed with respect to any of these 4 basic operations
o The violations can be divided into 2 mutually exclusive classes:
1. Operations whose result is larger than the largest number in the set (_____________ error) or
smaller than the smallest number in the set (_____________ error).
2. Operations whose result is neither too large nor too small but is simply not a member of the
set.
o Because computers have finite memories and therefore must of necessity perform arithmetic on
finite-precision numbers, the result of certain calculations will be, from the point of classical
mathematics, just plain wrong.
The algebra of finite-precision numbers is different from normal algebra.
o Still using 3-digit decimal numbers as an example, consider the associative law:
a+(b-c)=(a+b)-c
Let us evaluate both sides for a=700, b=400, c=300. The associative law does not hold. The order
of operations is important.
o As another example, consider the distributive law:
ax(b-c)=axb-axc
Let us evaluate both sides for a=5, b=210, c=195. The distributive law does not hold.
Judging from these examples, one might conclude that although computers are general-purpose
devices, their finite nature renders them especially unsuitable for doing arithmetic.
o This conclusion is, of course, not true
Decimal number system





It has 10 symbols:
The decimal number system is a positional system in that each digit position of a number has an
associated place value.
o For an integer, from right to left, the first digit position has a place value of _____, the second
digit position has a place value of _____, the third digit position has a place value of _____, and
so on.
The decimal number system uses 10 as the base for exponentiation, called the radix.
o We say the decimal number system is a base 10 system.
What does 9134 mean? It means
o This representation of the number 9134 is called expanded notation.
More examples:



52613=
4x105 + 9x103 + 7 =
Consider 9+1. One more than 9 is "ten". But there is no digit in the system to represent "ten". So we
write 9+1 as 10, because the 1 in the second column is worth _________
Consider
99
+ 1
------The first 20 decimal integers:
Other radix number systems


When dealing with computers, it is frequently convenient to use radices other than 10.
o The most important radices are 2, 8, and 16.
o The number systems based on these radices are called binary, octal, and hexadecimal,
respectively.
A radix k number system requires ___ different symbols to represent the digits ___________.
Base 2 system (binary)






Binary numbers are built up from the 2 binary digits:
The binary number system is also a positional system.
o For an integer, from right to left, the first digit position has a place value of _____, the second has
a place value of _____, the third has a place value of _____, and so on.
What does 1011 mean? It means
Consider 1+1. One more than 1 is "two". But there is no digit in the binary system to represent "two".
So we write 1+1 as 10, because the 1 in the second column is worth __________.
Consider
11
+ 1
------The first 20 binary integers:
Base 8 system (octal)






Octal numbers are built up from the 8 octal digits:
The octal number system is a also positional system.
o For an integer, from right to left, the first digit position has a place value of _____, the second has
a place value of _____, the third has a place value of _____, and so on.
What does 1011 mean? It means
Consider 7+1. One more than 7 is "eight". But there is no digit in the system to represent "eight". So
we write 7+1 as 10, because the 1 in the second column is worth
Consider
77
+ 1
------The first 20 octal integers:
Base 16 system (hexadecimal)

Hexadecimal numbers are built up from the 16 hexadecimal digits:





The base 16 number system is also a positional system.
o For an integer, from right to left, the first digit position has a place value of _____, the second has
a place value of _____, the third has a place value of _____, and so on.
What does A0B1 mean? It means
Consider F +1. One more than F is "sixteen". But there is no digit in the system to represent "sixteen".
So we write F+1 as 10, because the 1 in the second column is worth
Consider
FF
+ 1
------The first 20 hex integers:
Notation


The number 93 cannot be a base 8 number, because
When ambiguity arises, we attach a subscript to the number to denote the base. Thus, 93 12 is base 12,
9316 is base 16.
Convert a base b integer to base 10

Write the base b number in expanded form in decimal and simplify. The result is in decimal.
1012 =
1238 =
B9316 =
Convert a base 10 integer to base b


Follow the algorithm:
1. Divide the base 10 number by b.
2. Save the remainder
3. Divide the quotient by b
4. Save the remainder
5. Divide the quotient by b
6. Save the remainder
7. Continue the divide and save steps until the quotient is 0; the answer is the remainders saved
written out in reverse order, i.e., the last one saved is the first one written out.
Convert 42 to base 2.

Convert 143 to base 8.

Convert 143 to base 16.
Convert a base 2 integer to base 8


One algorithm:
1. Convert the base 2 number to base 10.
2. Convert the base 10 number to base 8.
Another algorithm (simpler):
1.
2.
Divide the base 2 integer into groups of 3 bits, starting from the right. It may be necessary to add
1 or 2 leading 0s to fill out the last group of 3 bits.
Each group can be directly converted to a single octal digit, 0 to 7, according to the conversion
given in the first 8 lines of Figure A-3.
---------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------- ------------------------------Figure A-3. The first 16 decimal integers and their binary, octal, and hexadecimal equivalents.

Convert 101110102 to base 8.
Convert a base 8 integer to base 2



One algorithm:
1. Convert the base 8 number to base 10.
2. Convert the base 10 number to base 2.
Another algorithm:
1. Each octal digit is replaced by the equivalent 3-bit binary number, according to the conversion
given in the first 8 lines of Figure A-3.
Convert 1728 to base 2.
Convert a base 2 integer to base 16



One algorithm:
1. Convert the base 2 number to base 10.
2. Convert the base 10 number to base 16.
Another algorithm:
1. Divide the base 2 number into groups of 4 bits, starting from the right. It may be necessary to add
1 or 2 or 3 leading 0s to fill out the last group of 4 bits.
2. Each group can be directly converted to a single hex digit, 0 to F, according to the conversion
given in the first 16 lines of Figure A-3.
Convert 1101110102 to base 16.
Convert a base 16 integer to base 2



One algorithm:
1. Convert the base 16 number to base 10.
2. Convert the base 10 number to base 2.
Another algorithm:
1. Each hex digit is replaced by the equivalent 4-bit binary number, according to the conversion
given in the first 16 lines of Figure A-3.
Convert 17F216 to base 2.
Negative binary integers

4 different systems for representing negative numbers have been used in digital computers at 1 time or
another in history: signed magnitude, 1's complement, 2's complement, excess 2 m-1 for m-bit numbers.
The signed magnitude scheme



In this system the leftmost bit is the sign bit (0 is + and 1 is -) and the remaining bits hold the absolute
magnitude of the number.
Using 8-bit word:
o 42 is represented by
o -42 is represented by
o 0001 0111 is
o 1001 0111 is
o The largest positive number is
o The largest negative number is
Disadvantages:
o There are 2 representations for 0, namely,
o Addition and subtraction require special algorithms because normal procedures do not work
directly on signed magnitude numbers. For example, consider 23+(-1):
0001 0111 //+23
+
1000 0001 //-1
-----------------------------
The 1's complement scheme



In this system the leftmost bit is the sign bit (0 is + and 1 is -)
o For positive numbers:
 The sign bit is 0, and the remaining bits hold the absolute magnitude of the number.
o For negative numbers:
 First find the representation for its positive counterpart.
 Then replace each 1 by a 0 and each 0 by a 1 including the sign bit (an operation known as 1's
complement, performed on a bit string).
Using 8-bit word:
o 42 is represented by
o -42 is represented by
o 0001 0111 is
o 1001 0111 is
o The largest positive number is
o The largest negative number is
Disadvantages:
o There are 2 representations for 0, namely,
o Addition and subtraction require special algorithms because normal procedures do not work
directly on 1's complement numbers. For example, consider 23+(-1):
0001 0111 //+23
+
1111 1110 //-1
-----------------------------
The 2's complement scheme

In this system the leftmost bit is the sign bit (0 is + and 1 is -)
o For positive numbers:
 The sign bit is 0, and the remaining bits hold the absolute magnitude of the number.
o


For negative numbers:
 First find the representation for its positive counterpart.
 Then find the 1's complement of its positive counterpart.
 Then add 1.
Using 8-bit word:
o 42 is represented by
o -42 is represented by
o 0001 0111 is
o 1001 0111 is
o The largest positive number is
o The largest negative number is
Advantages:
o There is only 1 representation for 0, namely,
o Addition and subtraction does not require special algorithms because normal procedures do work
directly on 2's complement numbers.
//23+(-1)
0001 0111 //+23
+
1111 1111 //-1
----------------------------o
o
A carry generated by the addition of the leftmost bits is merely thrown away.
If the addend and the augend are of opposite signs, overflow error cannot occur. If they are of the
same sign and the result is of the opposite sign, overflow error has occurred and the answer is
wrong.
The excess 2m-1 scheme for m-bit numbers




This system represents a number by storing it as the sum of itself and 2 m-1.
o In this system the leftmost bit is also the sign bit (1 is + and 0 is -)
Using 8-bit word, i.e., m=8, the system is called excess 2 8-1 which is excess 128.
o A number is stored as its true value plus 128.
o 42 is represented by
o -42 is represented by
o 0001 0111 is
o 1001 0111 is
o The largest positive number is
o The largest negative number is
Advantages:
o There is only 1 representation for 0, namely,
Disadvantages:
o Addition and subtraction require special algorithms because normal procedures do not work
directly on excess 28-1 numbers. For example, consider 23+(-1):
1001 0111 //+23
+
0111 1111 //-1
-----------------------------
BCD scheme




In a packed BCD (binary coded decimal) scheme, each decimal digit is coded with 4 bits, packed into a
byte, or word, or doubleword, or how many bytes are required.
The decimal number 926708 encoded in BCD is
The BCD number 1001 0010 0110 0111 0000 1001 represents the decimal number
For signed BCD, the lower nibble of the rightmost byte is usually used as the sign flag.
o Standard sign values are 1100 (hex C) for positive (+) and 1101 (D) for negative (−).



For example, the number −1,234,567 is 7 digits wide and is encoded as:
While packed BCD does not make optimal use of storage, the extra storage requirements are usually
offset by the accuracy it provides.
Packed BCD is supported in the COBOL programming language as the "PACKED-DECIMAL" (part
of the 1985 COBOL standard) data type.
Problem Set
1.
What is 1001101001 (binary) in decimal? In octal? In hexadecimal?
2.
Which of the following are valid hexadecimal numbers? BED, CAB, DEAD, DECADE, ACCEDED,
BAG, DAD.
3.
Express the decimal number 100 in all radices from 2 to 9.
4.
Perform the following calculations on 8-bit 2’s complement numbers (hint: a-b=a+(-b))
00101101
11111111
00000000
11110111
+ 01101111
+ 11111111
- 11111111
- 11110111
--------------------------------------------------------------------
5.
Perform the following addition problems for 3-bit binary numbers in 2’s complement. For each sum,
state whether an overflow occurred
000
000
111
100
100
+ 001
+ 111
+ 110
+ 111
+ 100
---------------------------------------------
6.
Using 8-bit word, give the 8-bit representation of the decimal number 20 in:
a. Sign-magnitude
b. 1’s complement
c. 2’s complement
d. excess 128
7.
Using 8-bit word, give the 8-bit representation of the number -20 in:
a. Sign-magnitude
b. 1’s complement
c. 2’s complement
d. excess 128
8.
Using 8-bit word, give the decimal number represented by 0111 0000 in:
a. Sign-magnitude
b. 1’s complement
c. 2’s complement
d. excess 128
9.
Using 8-bit word, give the decimal number represented by 1111 0000 in:
a. Sign-magnitude
b. 1’s complement
c. 2’s complement
d. excess 128
10. The unsigned decimal number 1023 encoded in BCD is:
11. The BCD number 0101 0011 0111 0010 represents the unsigned decimal number:
12. The signed decimal number 1023 encoded in BCD is:
13. The BCD number 0101 0011 0111 0010 1101 represents the signed decimal number: