Download DRO Week 8

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 mathematical notation wikipedia , lookup

Big O notation wikipedia , lookup

Large numbers wikipedia , lookup

Location arithmetic wikipedia , lookup

Positional notation wikipedia , lookup

Elementary mathematics wikipedia , lookup

Arithmetic wikipedia , lookup

Addition wikipedia , lookup

Transcript
www.hndit.com
IT1004: Data
Representation
and Organization
Negative number representation
www.hndit.com
Sign-Magnitude Representation
• This is one method used to represent negative
numbers in Binary
• Here an extra digit is placed in front the existing
binary number to represent the sign
• If this extra digit is a '1', it means that the rest of
the digits represent a negative number
• If the extra digit is a 0', it means that the number
is a positive
Example :
+37 = 00100101 (in 8 bits)
-37 = 10100101 (in 8 bits)
What is the range of numbers that can be represented in this method?
2
www.hndit.com
Sign-Magnitude Representation…
Example 2:
+127 = 01111111 (in 8 bits)
- 127 = 11111111 (in 8 bits)
Example 3 :
0= 00000000 (in 8 bits)
if we consider negative value of this bit stream
we get 10000000
This is also representing zero
This is a drawback of Sign-Magnitude representation
(That is two values to represent zero as +0 and -0)
3
One’s complement
www.hndit.com
• One's complement number representation is used for signed
numbers in binary format
• To obtain the 1's complement of a number:
– Get the binary format of the given number
– Complement all the bits in the binary number
• there are different representations for +0 and -0 in one's
complement.
Examples of 8-bit one's complement numbers:
BIT Pattern
Decimal Value
BIT Pattern
Decimal Value
0000 0000
+0
0000 0011
+3
1111 1111
-0
1111 1100
-3
0000 0001
+1
0001 1111
+31
1111 1110
-1
1110 0000
-31
One’s complement
www.hndit.com
The range of 8-bit one's complement integers is -127 to +127.
Exercises: Find the 1’s complement of
1. 4510
2. 3710
3. ABC16
4. 1238
5. 458
www.hndit.com
Addition in One’s complement
• Addition of signed numbers in one's complement is
performed using binary addition with end-around
carry.
• If there is a carry out of the most significant bit of the
sum, this bit must be added to the least significant bit
of the sum.
•
To add decimal 17 to decimal -8 in 8-bit one's complement:
0001 0001
1111 0111
1 0000 1000
1
0000 1001
(17)
(-8 )
=
(9)
810 = 0000 10002
www.hndit.com
Two’s
complement
number
representation
Two’s complement
www.hndit.com
• Two's complement number representation is used for
signed numbers on most modern computers.
• This notation allows a computer to add and subtract
numbers using the same operations
• We can illustrate two's complement notation as
follows:
– A fixed number of bits are used to represent numbers
– The most significant bit is called the sign bit
– This same notation is used to represent both positive and
negative numbers
Two’s complement…
Positive numbers are represented normally
• Example 1: Using a 4 bit representation
5 in 2's complement = 0101
• Example 2: Using an 8 bit representation
5 in 2's complement = 00000101
• Example 3: Using an 8 bit representation
24 base 16
= 0010 0100
www.hndit.com
Two’s complement…
www.hndit.com
Negative numbers Are represented using a 2's complement form
• To obtain the 2's complement of a number:
– Complement the bits
– Add one to the result
Example1 : Find the 2’s complement of the following 8
bit number
00101001
11010110 ………….. First, invert the bits
+ 00000001 ………….. Then, add 1
= 11010111
The 2’s complement of 00101001 is 11010111
Two’s complement…
Example 2 : (4 bits)
Represent -6 in 2's complement
+6 …………………… 0110
complement ……. 1001
add 1 ……………… 0001
-6 =
1010
www.hndit.com
Example 3: (4 bits)
Represent -3 in 2's complement
+3 …………………… 0011
complement ……. 1100
add 1 ………………..0001
-3 = 1101
Example 4: (5 bits)
Represent -13 in 2's complement
+13 ………………… 01101
complement …. 10010
add 1 …………… 00001
-13 = 10011
Two’s complement…
Represent the following
numbers in two’s
complement form using
5 bits
1.
2.
3.
4.
5.
-5
-7
-4
-11
-3
www.hndit.com
Represent the
following numbers in
two’s complement
form using 8 bits
1.
2.
3.
4.
5.
-12
-18
-21
-19
-9
www.hndit.com
Addition and subtraction in 2's
complement notation
• Addition is performed by doing the simple binary addition of the two
numbers.
• Subtraction is accomplished by first performing the 2's complement
operation on the number being subtracted and then adding the two
numbers.
Examples: 5 bits
8 ……. 01000
+4 …… 00100
12 …… 01100
-8 ..…….. 11000
+ -4 ………. 11100
-12 ………. 10100
8 ……….. 01000
+ -4 ………. 11100
4 ……….. 00100
13
www.hndit.com
Addition and subtraction in 2's
complement notation
• Since we are working with numbers contained in
a fixed number of bits, we must be able to detect
overflow following an operation.
• No overflow occurs when the value of the bit
carried into the most significant bit is the same as
the value carried out of the most significant bit.
• Overflow occurs when the value of the bit carried
into the most significant bit is not the same as the
bit carried out of the most significant bit.
14
www.hndit.com
Addition and subtraction in 2's
complement notation
• Example: 4 bits
6 ……… 0110
+ 1 ……… 0001
7 ……… 0111
7 ……… 0111
+ 1 ……… 0001
8 ……… 1000
X
This means
a negative
number
Here we can get the correct answer by increasing the number of bits
15
www.hndit.com
Addition and subtraction in 2's
complement notation
• Example: 5 bits
6 ……… 00110
+ 1 ……… 00001
7 ……… 00111
7 ……… 00111
+ 1 ……… 00001
8 ……… 01000
16
Addition and subtraction in 2's
www.hndit.com
complement notation…
Perform the following
calculations in two’s
complement method
using 5 bits
1.
2.
3.
4.
5.
10-5
-7+3
-4 +1
-11+ 8
-3+4
Perform the following
calculations in two’s
complement method using
8 bits
1.
2.
3.
4.
5.
-12+7
-18+9
22-21
-19+11
-9+5
Polynomial Evaluation
Whole Numbers (Radix = 10):
123410 = 1  103 + 2  102 + 3  101 + 4  100
With Fractional Part (Radix = 10):
36.7210 = 3  101 + 6  100 + 7  10-1 + 2  10-2
General Case (Radix = R):
(S1S0.S-1S-2)R =
S1  R1 + S0  R0 + S-1  R -1 + S-2  R-2
www.hndit.com