EE-121 Lect02b
EE-121 Lect02b
EE-121 Lect02b
Binary Athematic
Dr. Muhammad Hanif
Number System
Octal Number System
• Uses eight digits, 0,1,2,3,4,5,6,7.
• Base 8 number system
• Each position represents a power of the base (8).
• Last position in an octal number represents an x power of the base (8).
• Example: 8x where x represents the last position - 1.
Octal to Decimal
Example:
Octal Number − 125708
Calculating Decimal Equivalent −
• 8 68
• 8 8 - 4
• 8 1–0
• 0–1
• (68)10 = (104)8
Binary to Octal
• Make pair of three bits
= (13 ×16 + 5× 1)
= (208+5)
= 21310
Decimal to Hexadecimal
• Convert 213 to Hex
• 16 213
• 13 -5
• 0 -13 (D)
• (213)10 = D5H
Hexadecimal to Binary
• Create pair of four bits
1 9 8 0 1 1
+ 2 6 4 + 0 0 1
Sum 4 6 2 Sum 1 0 0
Carry 0 1 1 Carry 0 1 1
General concept
• Decimal addition • Binary addition
(carry) 1_ ( carry) 111_
19 10011
+ 7 + 111
26 11010
• 16+8+2 = 26
Binary Addition
It is a key for binary subtraction, multiplication, division.
1 + 0 + 0 = 01 Sum = 1, carry = 0
1 + 0 + 1 = 10 Sum = 0, carry = 1
1 + 1 + 0 = 10 Sum = 0, carry = 1
1 + 1 + 1 = 11 Sum = 1, carry = 1
Binary Addition (by example)
11 3 100 4
+11 +3 + 10 +2
110 6 110 6
111 7 110 6
+ 11 +3 +100 +4
1010 10 1010 10
Binary Addition
Binary Addition
Add the binary numbers 00111 and 10101 and show the
equivalent decimal addition.
0111
00111 7
10101 21
1 1 1 0 0 = 28
Binary Subtraction
Subtraction and Borrow:
These two words will be used very frequently for the binary subtraction.
There are four rules of binary subtraction.
Binary Subtraction
When doing subtracting, it is sometimes necessary to borrow from the next higher-order column.
In this case a 1 is borrowed from the next higher-order column, which leaves a 0 in that column
and creates a 10
except that the 10 in row (i) of columns B and C means binary 10, i.e. 2,
Binary Subtraction (by example)
11 3 11 3
-01 -1 -10 -2
10 2 01 1
101 5
-011 -3
010 2
Binary Subtraction
Example:
Binary Subtraction
00111 7
0 1 1 1 0 = 14
Example
• And more ripple -
29
Binary Subtraction
• The above example is the most convenient way for us to do
binary subtraction by hand.
• But how to represent “minus” sign in computer?
• There is another approach however and this is the way that
computers subtract binary digits.
• This approach is called Two's Complement.
Signed Binary Numbers
Signed Binary Numbers
• When we use pencil-and-paper
• – We use symbols “+” and “-”
• Computers: We need to represent these symbols using bits
• Convention:
• 0 positive 1 negative
• The leftmost bit position is used as a sign bit
• Bits to the right of sign bit is the number
• – In unsigned representation, the leftmost bit is a part of the number
(i.e. the most significant bit (MSB))
Signed Binary Numbers
Signed Binary Numbers
• Sign-Magnitude form
• In this form, a binary number has a bit for a sign symbol.
• If this bit is set to 1, the number will be negative else the number will
be positive if it is set to 0.
• Apart from this sign-bit, the n-1 bits represent the magnitude of the
number.
Signed Binary Numbers
• Signed Magnitude Method
• MSB ‘0’ represent “Positive Number”
• MSB ‘1’ represent “Negative Number
Signed Binary Numbers: Problem
Complement
• Complementing is an operation on base-r numbers
• Goal:
To simplify subtraction operation
– Rather turn the subtraction operation into an addition operation
• Two types:
1) Radix complement (r’s complement)
2) Diminished complement ((r-1)’s complement)
• When r = 2
1) 2’s complement
2) 1’s complement
Signed Binary Numbers
• 1's Complement
• By inverting each bit of a number, we can obtain the 1's complement
of a number.
• The negative numbers can be represented in the form of 1's
complement.
• In this form, the binary number also has an extra bit for sign
representation as a sign-magnitude form.
Signed Binary Numbers
• 2's Complement
• By inverting each bit of a number and adding plus 1 to its least
significant bit, we can obtain the 2's complement of a number.
• The negative numbers can also be represented in the form of 2's
complement.
• In this form, the binary number also has an extra bit for sign
representation as a sign-magnitude form.
Complement
• Easier way to compute 1’ s and 2’ s complements
• Use binary expansions
• 1’s complement: negate
• 2’s complement: negate + increment
The 1’s complement of a binary number is just the inverse of the digits.
To form the 1’s complement, change all 0’s to 1’s and all 1’s to 0’s.
For example, the 1’s complement of 11001010 is
00110101
0 0 1 1 0 1 0 1
Arithmetic's using 1’s Complement
There are three different cases when we add two binary numbers :
Case 1: Addition of the positive number with a negative number when the
positive number has a greater magnitude.
Case 2: Adding a positive value with a negative value in case the negative
number has a higher magnitude.
Case 1: Addition of the positive number with a negative number when the
positive number has a greater magnitude.
1.First, find the 1's complement of the negative number 1001. So, for finding 1's
complement, change all 0 to 1 and all 1 to 0.
So, take the 1's complement of the result to get the final result.
Arithmetic's using 1’s Complement
Case 2: Adding a positive value with a negative value in case the negative
number has a higher magnitude.
Example: 1101 and -1110
In this case, we always get the end-around carry, which get added
to the LSB,
For getting the final result, take the 1's complement of the result.
Arithmetic's using 1’s Complement
Case 3: Addition of two negative numbers
1.Firstly find the 1's complement of the negative numbers 01101 and 01110.
So, for finding 1's complement, we change all 0 to 1, and all 1 to 0.
1's complement of the number 01110 is 10001, and 01101 is 10010.
2.Now, we add both the complement numbers, i.e., 10001 and 10010;
10001+10010= 1 00011
3.By adding both numbers, we get the end-around carry 1.
4.We add this end-around carry to the LSB of 00011.
00011+1=00100
5.Now, find the 1's complement of the result 00100 that is the final answer.
6.So, the 1's complement of the result 00100 is 11011, and add a negative sign before
the number so that we can identify that it is a negative number.
Arithmetic's using 1’s Complement
Summary
The 2’s complement of a binary number is found by adding 1 to the LSB of the 1’s
complement.
Recall that the 1’s complement of 11001010 is
00110101 (1’s complement)
To form the 2’s complement, add 1: +1
00110110 (2’s complement)
1 1 0 0 1 0 1 0
Input bits
Carry
Adder
in (add 1)
Output bits (sum)
0 0 1 1 0 1 1 0
2’s Complement
Arithmetic's using 2’s Complement
Let's say we want to compute 1000 ( 8 ) - 11 ( 3 ).
•Step 1: Write the equation out, padding the bottom number with 0's
1000
0011 -
•Step 2: Invert the digits of the lower number
1000
1100
•Step 3: Add 1 to the lower number
1000
1101
•Step 4: Add those two numbers together to get 10101
•Step 5: Remove the leading 1 (and any 0's after it). You are left
with 0101 ( 5 ).
Arithmetic's using 2’s Complement
Case 1: Addition of the positive number with a negative number when the
positive number has a greater magnitude.
3.Find the 2's complement of the result 1110 that is the final result.
4.So, the 2's complement of the result 1110 is 0001, and add a negative
sign before the number so that we can identify that it is a negative
number.
Arithmetic's using 2’s Complement
In this case, first, find the 2's complement of both the negative numbers.
In this case, we will always get the end-around carry, which will be added to the
LSB, and for getting the final result, we will take the2's complement of the result.
Arithmetic's using 2’s Complement
Example: -1101 (-13) and -1110 (-14) in five-bit register
1.Firstly find the 2's complement of the negative numbers 01101 and 01110. 2's
complement of the number 01110 is 10010, and 01101 is 10011.
2.We add both the complement numbers, i.e., 10001 and 10010;
10010+10011= 1 00101
4.This carry is discarded and the final result is the 2.s complement of the result 00101. So,
the 2's complement of the result 00101 is 11011, and we add a negative sign before the
number so that we can identify that it is a negative number.
Quiz 01
• (87)10 = ( ? )2
• (42.76)10 = ( ? )2
• (425)10 = ( ? )8
• (10101)2 =( ? )10
• (110101001)2 = ( ? )10