340 likes | 568 Views
Binary Arithmetic. Addition in binary. The basic addition table is easy to write down In general, 2 binary nos are added in the usual column-by-column way, carrying a ‘1’ to the next column on the left if necessary Example: 1101 2 + 101 2 = 10010 2
E N D
Addition in binary • The basic addition table is easy to write down • In general, 2 binary nos are added in the usual column-by-column way, carrying a ‘1’ to the next column on the left if necessary • Example: 11012 + 1012 = 100102 • Exercise: Calculate 1011012 + 101112
Addition in binary • The basic addition table is easy to write down • In general, 2 binary nos are added in the usual column-by-column way, carrying a ‘1’ to the next column on the left if necessary • Example: 11012 + 1012 = 100102 • Exercise: Calculate 1011012 + 101112 Answer:10001002
Subtraction in Binary • The method for subtracting decimal nos, column-by-column from right to left, is also used for subtracting binary nos • Example: 110112 – 11012 = 11102 • Exercise: 100102 – 10112
Subtraction in Binary • The method for subtracting decimal nos, column-by-column from right to left, is also used for subtracting binary nos • Example: 110112 – 11012 = 11102 • Exercise: 100102 – 10112 Answer: 1112
Addition in Two’s Complement • Rule for adding integers in two’s complement • Add two bits and propagate the carry • If there is a final carry after the leftmost column addition, discard it • Range in Two’s complement • (2N-1 ) to + (2N-1 -1) • Before performing any arithmetic operation check that the input numbers and the result fall within the range
Example • (+17) + (+22) (+39) • 8 bit representation
Example • (+17) + (+22) (+39) • Solution: Carry 1 00010001 00010110 -------------- 00100111
Example • (+24) + (-17) (+7) • 8 bit representation Carry 11111 00011000 11101111 ----------------- 00000111 • Discard the last carry
Subtraction in Two’s Complement • There is no difference between addition and subtraction in Two’s complement • To subtract • Take the two’s complement of the second number • Add it to the first number • Number 1 - Number 2 = Number 1 + (-Number 2)
Example • (+101) - (+62) (+101) + (-62) (+39) • 8 bit representation Carry 11 01100101 11000010 ---------------- 00100111
Practice Question • Add -35 and 20 by converting to two’s complement • (-35) + (20) (-15) • 8 bit representation
Solution • Add -35 and 20 • (-35) + (20) (-15) Carry 111 11011101 00010100 --------------- 11110001
Practice Question • Subtract 20 from 30 by converting to two’s complement • Use 8 bit representation
Practice Question • Subtract 20 from 30 by converting to two’s complement • Also convert the result into decimal • Use 8 bit representation • Solution: 00011110 11101100 --------------- 00001010
Practice Question • Subtract 20 from -30 by converting to two’s complement • Also convert the result into decimal • Use 8 bit representation
Practice Question • Subtract 20 from -30 by converting to two’s complement • Use 8 bit representation • Solution hint: (-30) + (-20) = -50