Download presentation
Presentation is loading. Please wait.
Published byRosanna Tyler Modified over 9 years ago
1
Number Representation (Part 2) Computer Architecture (Fall 2006)
2
Unsigned Representations Unsigned number representations Binary (base-2) Octal (base-8) Hexadecimal (base-16) –Given n bits Range: 0 to 2 n -1 decimal numbers
3
Need for Signed Representations Unsigned representations cannot represent negative numbers –Such as: -2, -51 etc. However negative numbers are frequently used –Need a representation for positive & negative numbers – that is, signed numbers
4
Standard Signed Representations Signed numbers are represented using 3 different standards –Sign-bit Magnitude (SBM) representation –1’s Complement Representation –2’s Complement Representation This is the representation that is used by computers today!
5
Strategy for Signed Representation General strategy is as follows: –All representations assume fixed size 8, 16, 32, or 64 bits operated on as a single unit-word –Given n bits (unsigned range: 0 to 2 n -1) Break the range into two halves –One half represents positive numbers 0 to 2 n-1 -1 corresponds to positive numbers –Decimal value: 0 to 2 n-1 -1 –Another half represents negative numbers 2 n-1 to 2 n -1 corresponds to negative numbers –Decimal range: -2 n-1 to -1
6
Sign-Bit Magnitude (SBM) Representation Uses left-most bit called sign bit to indicate sign –Sign bit = 0 implies positive number –Sign bit = 1 implies negative number Example: 2525 0 2424 0 2323 1 2 0 2121 0 2020 1 2727 1 2626 0 Decimal value = -9 10 Sign-bit
7
SBM Examples (1) Represent 10 10 using 8-bit SBM –Converting to binary: 10 10 = 1010 2 –8-bit SBM representation: 00001010 2 Sign bit is 0 to indicate positive value. Represent -15 10 using 8-bit SBM –Converting to binary: 15 10 = 1111 2 –8-bit SBM representation: 10001111 2 Sign bit is 1 to indicate negative value
8
SBM Examples (2) Convert 8-bit SBM 00000101 2 to Decimal –The sign bit (left most bit) is 0 indicating positive value –Converting 0000101 2 to decimal we get 5 10 –00000101 2 in SBM = 5 10 Convert 8-bit SBM 10001101 2 to Decimal –The sign bit (left most bit) is 1 indicating negative value! –Converting 0001101 2 to decimal we get 13 10 –Result: 10001101 2 = -13 10
9
1’s Complement Representation There are a few drawbacks with SBM –There are 2 different representations for zero +0 (00000000) and -0 (10000000) Logic circuits for addition and subtraction of binary numbers are complicated as they have to handle sign bit separately. 1’s Complement Representation –Fixed size representation –Most significant bit is reserved as sign-bit –Positive numbers are represented using standard binary notation –Negative numbers are represented by inverting all bits in the standard binary notation
10
Example of 1’s Complement Represent 12 10 using 8-bit 1’s Complement –Converting to binary: 12 10 = 1100 2 –8-bit 1’s complement representation: 00001100 2 Sign bit is 0 to indicate positive value. Represent -13 10 using 8-bit SBM –Converting to binary: 13 10 = 1101 2 –8-bit representation: 00001101 2 –Since original decimal number was negative each bit in 8-bit representation is inverted! –1s Complement representation = 11110010 Note: Sign bit becomes 1 to indicate negative value
11
Example of 1’s Complement Convert 1’s complement 00001111 2 to decimal –Sign bit is 0 to indicating positive value. –Converting 00001111 2 to decimal we get 15 10 –Final result: +15 10 Convert 1’s complement 11110101 2 to decimal –Sign bit is 1 indicating negative value. –First invert all the bits to get: 00001010 2 –Convert above binary to decimal to get 10 10 –Final result: -10 10
12
1’s Complement Still has 2 different representations for 0 +0 (00000000) and -0 (11111111) However, A – A operations can be easily represented –A – A = A + (-A) = 11111111 (which is effectively 0 in 1’s complement) General subtraction does not work in all cases!
13
2’s Complement Overcomes the limitations of SBM and 1’s complement representation! –Fixed size representation –Enables subtraction of numbers via addition! –Also reserves a special sign bit (left most bit) 0 (sign bit) indicates positive numbers 1 (sign bit) indicates negative numbers –Conversion to and from 2’s complement requires similar steps Several of them are identical to 1’s complement
14
Convert Decimal to 2’s Complement Signed Decimal Number PositiveNegative Decimal to n-bit Binary Invert all the n bits Add 1 2
15
2’s Complement Example Represent +20 10 using 8-bit 2’s Complement Since number is positive simply represent +20 10 as a 8-bit binary number! +20 10 = 00010100 2 Represent -18 10 using 8-bit 2’s complement Since number is negative we need to do 1’s complement and add 1 2 –Step 1: Convert 18 10 to 8-bit binary = 00010010 2 –Step 2: Invert bits = 11101101 2 –Step 3: Add 1 2 = 11101110 2 –Final result: -18 10 = 11101110 2 in 8-bit 2’s complement
16
2’s Complement to Decimal Binary number in n-bit 2’s complement Value of Sign-bit (Left most bit) Binary to decimal (Negative number) Invert all bits Add 1 Binary to decimal (positive number) Sign Bit=0Sign Bit=1
17
Example Convert 8-bit 2’s complement to decimal –Case 1: 00001010 2 Sign bit is 0 indicating positive number Simply convert binary to decimal to get 10 10 –Case 2: 11111010 2 Sign bit is 1 indicating negative number Step 1: Invert all bits to get 00000101 2 Step 2: Add 1 2 to get 00000110 2 Convert binary to decimal to get -6 10 –Note the negative sign on decimal number!
18
Subtraction using 2’s Complement Perform 5 10 – 3 10 using 4-bit 2’s complement 5 10 – 3 10 = 5 10 + (-3 10 ) 5 10 = 0101 2 -3 10 = (0011 2 →1100 2 + 1 2 ) = 1101 2 5 10 + (-3 10 ) = 0101 2 + 1101 2 = 0010 2 0010 2 = 2 10 INV
19
Subtraction using 2’s Complement Perform 2 10 – 4 10 using 4-bit 2’s complement 2 10 – 4 10 = 2 10 + (-4 10 ) 2 10 = 0010 2 -4 10 = (0100 2 →1011 2 + 1 2 ) = 1100 2 2 10 + (-4 10 ) = 0010 2 + 1100 2 = 1110 2 1110 2 is 2’s complement result and because it is negative (sign bit is 1) it needs to be converted 1110 2 → 0001 2 + 1 2 = 0010 2 = -2 10 INV
20
Subtraction Circuit Circuit to subtract two 3-bit 2’s complement numbers –Recollect that subtraction is performed via addition in 2’s complement representation. Addition is performed using Full Adder modules Cascaded together in the form of Ripple Carry Adder –One of the numbers have to be converted to 2’s complement representation Invert all the bits Add 1 2
21
Logic Circuit to add 3 bits: Full Adder Refer to truth table shown earlier –Sum = A + B + C –Carry = (AB) + (BC) + (CA) A B Sum C Carry AB BC CA A + B Full Adder
22
Ripple Carry Adder Several full adder’s can be cascaded to add multiple-bits! –Circuit reflects the way binary numbers are added –The following circuit adds 2 3-bit binary numbers namely A 2 A 1 A 0 and B 2 B 1 B 0 to yield result C 3 C 2 C 1 C 0 FA0 A0A0 B0B0 C1C1 C0C0 FA1 A1A1 B1B1 FA2 A2A2 B2B2 C3C3 C2C2 0 What happens this bit is set to 1 instead of 0?
23
Subtraction Circuit FA0 A0A0 B0B0 C1C1 C0C0 FA1 A1A1 B1B1 FA2 A2A2 B2B2 C2C2 1 Add 1 to inverted B n bits to generate 2’s complement! Each bit is inverted (Step 1 of Converting to 2’s Complement)
24
Add/Subtract Circuit Circuit to Add or Subtract two 3-bit 2’s complement number FA0 A0A0 B0B0 C1C1 C0C0 FA1 A1A1 B1B1 FA2 A2A2 B2B2 C3C3 C2C2 0 Control Input 0 = Add 1 = Subtract The XOR gate inverts the bits only when control input is 1
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.