Digital Arithmetic Operations
Digital Arithmetic Operations
Arithmetic:
Operations
Topic Outcome
• Describe how negative number is represented in a computer system
• Perform operations that include negative numbers using the 2’s complement
representation
Signed and Unsigned Binary Numbers
• integer variables are represented in a signed and unsigned manner
• positive and negative values are differentiated by using the sign flag in signed numbers
• unsigned numbers do not use any flag for the sign, i.e., only positive numbers can be stored by the
unsigned numbers.
• digital system
• not possible to use negative sign before them because the data is in binary form in digital computers
• require a special notation representing the sign in binary numbers
Binary Numbers Representation
• computer can understand only (0, 1) language
• binary numbers are represented in both ways, i.e., signed and unsigned
• positive numbers are represented in both ways: signed and unsigned
• negative numbers can only be described in a signed way
• difference between unsigned and signed numbers is that unsigned numbers do not use any sign bit
for positive and negative numbers identification, but the signed number used
Binary Numbers Representation
Binary Number
Representation
• has both the sign bit and the magnitude of the number
• representing the negative decimal number, the corresponding symbol in front of the binary number
will be added
• 1's Complement
• inverting each bit of a number to obtain the 1's complement of a number
• negative numbers can be represented in the form of 1's complement
• binary number also has an extra bit for sign representation as a sign-magnitude form.
• 2's Complement
• inverting each bit of a number and adding plus 1 to its least significant bit
• negative numbers can also be represented in the form of 2's complement
• binary number also has an extra bit for sign representation as a sign-magnitude form
Signed-Magnitude form
• for n bit binary number, 1 bit is reserved for sign symbol
Positive singed-magnitude
• value of sign bit is 0, then the given number will be positive
• value of sign bit is 1, then the given number will be negative
• remaining (n-1) bits represent magnitude of the number.
• magnitude of number zero (0) is always 0, so there can be two
representation of number zero (0), positive (+0) and negative (-0), which Negative singed-magnitude
depends on value of sign bit - ambiguous representation
• sign bit is a most significant bit (MSB) of representation
• range of sign-magnitude form is from -(2(n-1)-1) to (2(n-1)-1)
Signed-Magnitude form
• Ex: 6-bit Signed-Magnitude form
• range of 6-bit Sign-Magnitude form binary number is from -(25-1) to (25-1)
• from minimum value -31 (i.e., 1 11111) to maximum value +31 (i.e., 0 11111)
• zero (0) has two representation, -0 (i.e., 1 00000) and +0 (i.e., 0 00000)
Signed-Magnitude form
• MSB is used, 1 = negative and 0 = positive, the rest are the same
• clear and easy to implement
• Note: operations do not conform to the standard arithmetic rules, as in:
0000 0110 =+ 6
+ 1000 0110
1000 1100
=- 6
= - 12 X
1’s complement
• obtained by inverting each bit of given number
• represent positive numbers in binary form and negative numbers in 1’s complement form
• there is extra bit for sign representation
• value of sign bit is 0, then number is positive - directly represent it in simple binary form
• value of sign bit 1, then number is negative and have to take 1’s complement of given binary
number
• can get negative number by 1’s complement of a positive number and positive number by using 1’s
complement of a negative number
• zero (0) can have two representation, that’s why 1’s complement form is also ambiguous form
• range of 1’s complement form is from -(2(n-1)-1) to (2(n-1)-1)
1’s complement
• Ex: 6-bit 1’s complement form
• range of 6-bit 1’s complement form binary number is from -(25-1) to (25-1)
• minimum value -31 (i.e., 1 00000) to maximum value +31 (i.e., 0 11111)
• zero (0) has two representation, -0 (i.e., 1 11111) and +0 (i.e., 0 00000)
1’s complement
• Subtraction of 2 8-bit binary numbers • 01010111 (the overflow “1” cancels out)
• 11510 in binary is: 011100112 • convert it back from a 1’s complement answer to the real
answer, need add “1” to the 1’s complement result
• 2710 in binary is: 000110112
• 115 – 27 = 115 + (-27) = 88 01010111
• 1’s completement of 27: +1
• Invert 00011011 => 11100100
01011000
01110011 • 010110002 or (64 + 16 + 8) = 8810 in decimal
+ 11100100
Overflow → 1 01010111
Ignore the 9-bit
2’s complement
• obtained by inverting each bit of given number and plus 1 to least significant bit (LSB)
• represent positive numbers in binary form and negative numbers in 2’s complement form
• extra bit for sign representation
• value of sign bit is 0, then number is positive and can directly represent it in simple binary form
• value of sign bit 1, then number is negative and have to take 2’s complement of given binary
number
• can get negative number by 2’s complement of a positive number and positive number by directly
using simple binary representation
• value of most significant bit (MSB) is 1, then take 2’s complement from, else not
• zero (0) has only one (unique) representation which is always positive.
• range of 1’s complement form is from -2(n-1) to (2(n-1)-1)
2’s complement
• Ex: 6-bit 2’s complement form
• range of 6-bit 2’s complement form binary number is from -25 to (25-1)
• minimum value -32 (i.e., 1 00000) to maximum value +31 (i.e., 0 11111)
• zero (0) has one representation, 0 (i.e., 0 00000)
2’s complement
• Ex: Find the 2’s complement expression of -3510
2’s complement
• Ex: Find the 2’s complement expression of -3510
• 3510 = 0010 00112
• flip each of the bits above
• add 1
1’s
= 1 1 0 1 1 1 0 0
complement
add 1 = 0 0 0 0 0 0 0 1
2’s
1 1 0 1 1 1 0 1
complement
2’s complement
• Subtraction of 2 binary numbers • 01011000 (the overflow “1” cancels out)
• 11510 in binary is: 011100112 • convert it back from a 2’s complement answer
• 010110002 or (64 + 16 + 8) = 8810 in decimal
• 2710 in binary is: 000110112
• 115 – 27 = 115 + (-27) = 88
• 2’s completement of 27
• Invert 00011011 => 11100100
• 11100100 + 1 = 11100101
01110011
+ 11100101
Overflow → 1 01011000
Ignore the 9-bit
2’s complement
• Ex: using 8-bit 2’s complement numbers.
• Add −8 to +3
• Add −5 to −2
2’s complement
• Ex: using 8-bit 2’s complement numbers.
• Add −8 to +3 8 => 0000 1000
(+3) 0000 0011 Invert => 1111 0111
Add 1 =>+ 1
+(−8) 1111 1000
1111 1000
------------------------
(−5) 1111 1011 => -ve in 2’s complement, invert => 0000 0100, add 1 => 0000 0101, 4 + 1 = 5, so, is -5
• Add −5 to −2
(−2) 1111 1110
+(−5) 1111 1011
------------------------
(−7) 1 1111 1001 : discard carry-out => -ve in 2’s complement, invert => 0000 0110,
add 1 => 0000 0111, 4+2+1 = 7, so, is -7
2’s complement
• Ex: using 4-bit 2’s complement numbers.
• Add −7 to -3
• Add −5 to −2
2’s complement
• Ex: using 4-bit 2’s complement numbers.
• Add −7 to -3
(−3) 1101
+(−7) 1001
------------------------
(−10) 1 0110 : discard carry-out, 0110 => 4 + 2 = 6??
• Add −5 to −2
(−2) 1110
+(−5) 1011
------------------------
(−7) 1 1001: discard carry-out, -ve in 2’s complement, 1001 => invert 0110 => 0110 + 1 = 0111, 7
2’s complement - Overflow Rule for addition
• 2 Two's Complement numbers are added, and they both have the same sign (both positive or both negative),
then overflow occurs if and only if the result has the opposite sign
• Overflow never occurs when adding operands with different signs.
• Means
• Adding two positive numbers must give a positive result
• Adding two negative numbers must give a negative result
• Overflow occurs if
• (+A) + (+B) = −C
• (−A) + (−B) = +C
• https://www.tutorialspoint.com/unsigned-and-signed-binary-numbers
• https://www.electronics-tutorials.ws/binary/signed-binary-numbers.html
• https://www.doc.ic.ac.uk/~eedwards/compsys/arithmetic/index.html