Transcript PPP 2
1
CENG 222 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Computers are built using the logic circuits that operate on information
represented by two-valued electric signals. We label the two values as “0” and “1”, and
we define amount of information represented by such a signal as a bit of information,
where bit stands for binary digit. The most natural way to represent a number in a
computer system is by a string of bits called a character code.
Number representation. Consider an n-bit vector
B = bn−1 . . . b1b0
where bi = 0 or 1 for 0 ≤ i ≤ n − 1. This vector can represent an unsigned integer value
V(B) in the range 0 to 2n − 1, where
V(B) = bn−1 × 2n−1 +・ ・ ・+b1 × 21 + b0 × 20
We need to represent both positive and negative numbers. Three systems are
used for representing such numbers:
• Sign-and-magnitude
• 1’s-complement
• 2’s-complement
2
CENG 222 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
In all three systems, the leftmost bit is 0 for positive numbers and 1 for negative
numbers. Positive values have identical representations in all systems, but negative
values have different
representations. Lets analyze four bit numbers.
In the sign-and-magnitude system, negative values are represented by
changing the most significant bit (b3) from 0 to 1 in the B vector of the corresponding
positive value.
In 1’s-complement representation, negative values are obtained by
complementing each bit of the corresponding positive number. For n-bit numbers, this
operation is equivalent to subtracting the number from 2n − 1.
In the 2’s-complement system, forming the 2’s-complement of an n-bit
number is done by subtracting the number from 2n. Hence, the 2’s-complement of a
number is obtained by adding 1 to the 1’s-complement of that number.
3
CENG 222 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
4
CENG 222 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Number Representation
The sign-and-magnitude system seems the most natural,
because we deal with sign-and-magnitude decimal values in
manual computations. The 1’s-complement system is easily
related to this system, but the 2’s-complement system may appear
somewhat unnatural. However, we will show that the 2’scomplement system leads to the most efficient way to carry out
addition and subtraction operations.
It is the one most often used in modern computers.
5
CENG 222 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Decimal to Binary Transform
Example:
117
2
Decimal
-10
58
number 117
17 - 4
- 16 18
1 - 18
0
2
29
-2
9
-8
1
2
14
- 14
0
2
7
-6
1
2
3
2
-2
1
1
LSB (Least Significant Bit) MSB (Most Significant Bit)
1110101
6
CENG 222 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Binary to Decimal Transform
For this transform is good to
memorize set of powers of
two numbers:
Refers to memory:
7
20 = 1
21 = 2
22 = 4
23 = 8
24 = 16
25 = 32
26 = 64
27 = 128
28 = 256
29 = 512
210 = 1024
220 = 1 MB
230 = 1 GB
KB (Kilo Byte)
(Mega Byte)
(Giga Byte)
CENG 222 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Binary to Decimal Transform
Example:
Binary number
01110101
= 0×27 + 1×26 + 1×25 + 1×24 + 0×23 + 1×22 + 0×21 + 1×20
= 0×128 + 1×64 + 1×32 + 1×16 + 0×8 + 1×4 + 0×2 + 1×1
= 64 + 32 + 16 + 4 + 1 = 117
Decimal number
117
8
CENG 222 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Octal and Hexadecimal Systems
Octal system. Binary number is divided by groups of three bit
each. Each group is presented by decimal digit from 0 to 7.
Example:
101 001 010 111 110 001
5 1 2 7 6 1
Hexadecimal system. Binary number is divided by groups of
four bit each. For coding are used digits from 0 to 9. Additional
digits are A=10, B=11, C=12, D=13, E=14, F=15.
Example:
1010 0101 0001 1110 1100 0011
A
5
1
E
C 3
9
CENG 222 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Addition of the Positive Numbers:
In order to add multiple-bit numbers, we use a method
analogous to that used for manual computation with decimal
numbers. We add bit pairs starting from the low-order (right) end
of the bit vectors, propagating carries toward the high-order (left)
end. The carry-out from a bit pair becomes the carry-in to the
next bit pair to the left. The carry-in must be added to a bit pair in
generating the sum and carry-out at that position.
10
CENG 222 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Addition and Subtraction of Signed Numbers:
Example:
(+7) + (-3) = (+4)
(+7)
(-3)
0111
1011
(SM)
1100
(1’s)
1101
(2’s)
Direct transform of 1 1 0 1 to unsigned
decimal gives 1×23+1×22+0×21+1×20 = 13
To understand 2’s-complement
arithmetic, consider addition
modulo N (abbreviated as mod N).
Consider the case N = 16, shown
in the figure.
11
+
1
01 1 1
1 101
0100
+4
Carry-out bit
CENG 222 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Addition and Subtraction of Signed Numbers:
Example:
(-6) + (+5) = (-1)
(+5)
(-6)
0101
1110
1001
1010
(SM)
(1’s)
(2’s)
Locate 1 0 1 0 on the circle in Figure.
Then move 0 1 0 1 (5) steps in the
clockwise direction to arrive at 1 1 1 1,
which yields the correct answer of -1.
1010
+ 0101
0 1111
-1
Carry-out bit
12
CENG 222 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Addition and Subtraction of Signed Numbers:
To add two numbers, add
their n-bit representations, ignoring
the carry-out bit from the MSB
position. The sum will be the
algebraically correct value in 2’scomplement representation if the
actual result is in the range−2n−1
through +2n−1 − 1.
To subtract two numbers X and Y , that is, to
perform X − Y , form the 2’s-complement of Y , then add it to
X using the add rule. Again, the result will be the
algebraically correct value in 2’s-complement representation
if the actual result is in the range −2n−1 through +2n−1 − 1.
13
CENG 222 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Overflow and Sign Extension
Overflow:
When answers do not fall within the representable
range, we say that arithmetic overflow has occurred.
We add additional bits to extend range of numbers.
Sign Extension:
We often need to represent a value given in a
certain number of bits by using a larger number of bits.
For a positive number, this is achieved by adding zeroes to
the left. For a negative number in 2’s-complement
representation, the leftmost bit, which indicates the sign
of the number, is a 1. A longer number with the same
value is obtained by replicating the sign bit to the left as
many times as needed.
14
CENG 222 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Sign Extension
Example:
Positive number
0 1 0 1 1 0 0 1
0 0 0 0 0 0 0 0 0 1 0 1 1 0 0 1
Negative number
Sign
1 0 0 0 1 1 1 0
1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 0
15
CENG 222 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Overflow in Integer Arithmetic
Using 2’s-complement representation, n bits can
represent values in the range −2n−1 to +2n−1 − 1. For
example, the range of numbers that can be represented by
4 bits is −8 through +7, as shown before. When the actual
result of an arithmetic operation is outside the
representable range, an arithmetic overflow has occurred.
When adding unsigned numbers, a carry-out of 1
from the most significant bit position indicates that an
overflow has occurred.
However, this is not always true when adding signed
numbers. For example, let’s us add 2’s-complement
representation for 4-bit signed numbers.
16
CENG 222 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Overflow in Integer Arithmetic
For example, using 2’s-complement representation for 4-bit
signed numbers, if we add +7 and +4, the sum vector is 1011,
which is the representation for −5, an incorrect result. In this
case, the carry-out bit from the MSB position is 0. If we add −4
and −6, we get 0110 = +6, also an incorrect result. In this case,
the carry-out bit is 1. Hence, the value of the carry-out bit from
the sign-bit position is not an indicator of overflow.
(Operations in 2’s complement system)
+7
+4
Negative !
17
01 1 1
+ 0100
10 1 1
(-5)
-4
-6
Carry-out bit
Positive !
1
1100
+ 1010
0110
(+6)
CENG 222 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Overflow in Integer Arithmetic 1
These observations lead to the following way to detect
overflow when adding two numbers in 2’s-complement
representation.
1. Examine the signs of the two summands and the sign of the
result. When both summands have the same sign, an overflow
has occurred when the sign of the sum is not the same as the
signs of the summands.
2. When subtracting two numbers, the testing method needed
for detecting overflow has to be modified somewhat; but it is
still quite straightforward.
18
CENG 222 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Overflow in Integer Arithmetic 2
Another method of overflow detecting is based on
analysis of carry-in signal to sign bit and carry-out signal from
sign bit.
(Operations in 2’s complement system)
+7
+4
01 1 1
-4
1100
+ 0100
-6
+ 1010
10 1 1
Carry-out bit
1
0110
Negative !
(-5)
Positive !
(+6)
If carry-in present but carry-out absent, or no carry-in but
there is carry-out, there is the overflow! It may be described
as
19
CENG 222 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Character Representation
The most common encoding scheme for characters is
ASCII (American Standard Code for Information Interchange).
Alphanumeric characters, operators, punctuation symbols, and
control characters are represented by 7-bit codes. It is
convenient to use an 8-bit byte to represent and store a
character. The code occupies the low-order seven bits. The
high-order bit is usually set to 0. Note that the codes for the
alphabetic and numeric characters are in increasing sequential
order when interpreted as unsigned binary numbers. This
facilitates sorting operations on alphabetic and numeric data.
MSB (Most Significant Bit), which increase range of
numbers, is used in representation of symbols of different
national languages.
20
CENG 222 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Character Representation
21
CENG 222 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV