Upload
tranngoc
View
231
Download
5
Embed Size (px)
Citation preview
your name
2’s Complement Arithmetic
DBLabDept. of Computer Science
Sangji Univ.
referenced from http://goo.gl/o7QcP , http://goo.gl/CGnI4 and http://goo.gl/zzEWD
your name
Binary Number Representation
• Clearly this is an unsatisfactory system. Alternatively, instead of using a range of voltages, we make use of two voltages:– 0 volts, representing a ‘low’ or ‘0’– 5 volts, representing a ‘high’ or ‘1’.
• We can then form numbers by stringing ‘0’s and ‘1’s together:0 00001 00012 00103 0011
your name
Binary Number Representation
• From this was born the binary number system.– It is a base-2 system: i.e. its digits are in the set [0, 1].
• Counting in binary is exactly the same as counting in decimal, except that we increment the next digital after ‘1’ instead of ‘9’.– Decimal: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21...– Binary: 0 1 10 11 100 101 110 111 100....
your name
The Octal Number System
• As an example, let’s regroup bits into groups of 3, and assign them symbols according to this table:000 - 0 001 - 1 010 - 2 011 - 3100 - 4 101 - 5 110 - 6 111 - 7So for the previous examples, after re-grouping (from right to left) we get:101 011 101 001 010 111 111 110 101 011Assigning the symbols, we get:5351277653
your name
The Octal Number System
• By grouping bits together and assigning groups of 3 bits symbols from ‘0’ to ‘7’, we have created a new number system called ‘octal’.
• Octal is a base-8 number systems, meaning that the digits belong to the set [0, 7].
• Counting in octal is similar to counting in decimal, except that the largest digit is 7:– 0 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17 20 21 22 23 24 25 26 27
30....
your name
The Hexadecimal Number System
• Instead of grouping into groups of 3 bits, we can also group into 4 bits. Each pattern of 4 bits will be assigned 1 of 16 symbols according to this table:0000 - 0 0001 - 1 0010 - 2 0011 - 30100 - 4 0101 - 5 0110 - 6 0111 - 71000 - 8 1001 - 9 1010 - A 1011 - B1100 - C 1101 - D 1110 - E 1111 - F
• If we re-grouped the previous example into 4 bits (from right to left), we get:0010 1011 1010 0101 0111 1111 1010 1011
your name
The Hexadecimal Number System
• Using the table and assigning symbols, we get:0010 1011 1010 0101 0111 1111 1010 10112BA57FAB
• This is the hexadecimal number system, or base-16 number system.
• Counting in hexadecimal:0 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15 16 17 18 19 1A 1B
1C 1D 1E 1F 20 21 22 ...
your name
Signed Numbers
• The number systems shown in the previous slides allow us to perform lots of useful stuff on computers, but they have a fundamental problem:– There is no way to represent negative numbers!!
• We can solve this by augmenting an extra digit to the front of the number to represent the sign. The processor can then look at this extra digit and decide on the sign-ness of the number. We call this “sign and magnitude”:– E.g. if we used ‘0’ to represent +ve, and 9 to represent –ve, then 123
can be written as 0123, while –123 can be written as 9123– If we used ‘0’ to represent +ve, and ‘1’ to represent –ve, then the
binary number 01001 will be written as 001001, while –(01001) will be written as 11001.
your name
Signed Numbers
• The signed-magnitude system is simple, but has some fundamental problems:– Cannot directly add numbers together:
• E.g. (1011)sm + (0001)sm = (1100)sm– In S+Mag, this means (-3) + 1 = (-4)!!
– It is not complementary:• E.g. –3 + 3 = (1011 + 0011)sm = 1110 = -6??
• Need to introduce other negative-number systems– Radix Complement– Diminished Radix Complement
your name
Radix Complements
• The radix complement C of a number N with M digits in a base B is defined to be:– C = (BM – N)Bcns
– We can prove that C is the complement of N by finding C + N:
• C + N = (BM – N) + N = BM
• This BM is what we call an end-round-carry, and is discarded, giving us a final answer of 0.
your name
Radix Complements
• E.g. Find the radix-complement of (235)10:– -(235)10 = 103 – 235 = (765)10cns
• Find the radix-complement of (101101)2:– 26 – 101101 = 1000000 – 101101 = (010011)2cns
– The radix-complement of a binary number is particularly useful in computers, and is called the Two’s Complement Number System.
• Find the radix-complement of (327)8:– 83 – 327 = 1000 – 327 = (455)8
your name
Diminished Radix Complement
• There is another complementary number system called the Diminished Radix Complement or (B-1) complement. The diminished radix complement C of an M digit number N in base B is defined to be:– C = (BM –1 – N)B-1cns
• Find the 9s Complement of (3212)10:– 104 - 1 = 9999– 9999 - 3212 = (6787)9cns
• Find the 1s complement of (101101)2– 26 - 1 = 111111– 111111 - 101101 = (010010)1cns
your name
1’s Complement Arithmetic
• The Formula
your name
1’s Complement Arithmetic
• In Binary
your name
2’s Complement Arithmetic
• The Formula
your name
2’s Complement Arithmetic
• In Binary
your name
2’s Complement Arithmetic
• Conversion from 2’s Complement
your name
2’s Complement Arithmetic
• Conversion to 2’s Complement
your name
2’s Complement
• The 2’s complement number system is the radix-complement number system for binary numbers:– 101011 in 2’s complement is written as (101011)2cns or
(101011)2s for short.
• Range of n-bit 2’s complement:– Smallest possible number represented: -2n-1
– Largest possible number represented: 2n-1 - 1– So for 8-bit 2’s complement, the range is [-128, 127].
• It is an asymmetric number system: there are more negative numbers than positive
• 2’s Complement has 1 representation for 0: (00000000)2s
your name
Floating Point Number Representation
your name
Floating Point Number Representation
your name
Floating Point Number Representation
• Normalization
your name
Floating Point Number Representation
• Example
your name
Floating Point Number Representation• Biased Notation for Exponent