EE-121 Lect02b

Download as pdf or txt
Download as pdf or txt
You are on page 1of 67

EE-121: Fundamental of Logic Design

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 −

Step Octal Number Decimal Number


4 3 2
Step 1 125708 ((1 × 8 ) + (2 × 8 ) + (5 × 8 ) + (7 ×
1 0
8 ) + (0 × 8 ))10

Step 2 125708 (4096 + 1024 + 320 + 56 + 0)10

Step 3 125708 549610


Decimal to Octal
• Convert 6810 to octal

• 8 68
• 8 8 - 4
• 8 1–0
• 0–1

• (68)10 = (104)8
Binary to Octal
• Make pair of three bits

• (10 101)2 = (010 101)2 = (25)8

• (10111.1) = (010 111 . 100) = (2 7 . 4)8


Hexadecimal Number System
• Uses 10 digits and 6 letters, 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F.
• Letters represents numbers starting from 10. A = 10, B = 11, C = 12, D
= 13, E = 14, F = 15.
• Base 16 number system.
• Each position in a hexadecimal number represents a power of the
base (16).
• Last position in a hexadecimal number represents an x power of the
base (16).
• Example 16x where x represents the last position - 1.
Hexadecimal to Decimal
Example:
Hexadecimal Number: 19FDE16
Calculating Decimal Equivalent −
.
Step Hexadecimal Number Decimal Number
4 3 2
Step 1 19FDE16 ((1 × 16 ) + (9 × 16 ) + (F × 16 ) + (D ×
1 0
16 ) + (E × 16 ))10
4 3 2
Step 2 19FDE16 ((1 × 16 ) + (9 × 16 ) + (15 × 16 ) + (13 ×
1 0
16 ) + (14 × 16 ))10

Step 3 19FDE16 (65536 + 36864 + 3840 + 208 + 14)10

Step 4 19FDE16 10646210


Hexadecimal to Decimal
• Convert D5H to decimal

• (D5)H = (13×161 + 5×160)

= (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

• Convert (25)H to Binary


• 25H = (0010 0101)2

• Convert (3A.7) to Binary

• (3A.7)H = (0011 1010. 0111)2

• Convert (CD.E8) to Binary


• (CD.E8) = (1100 1101. 1110 1000)2
Binary-to-Hexadecimal Conversion
• Simply break the binary number into 4-bit groups, starting at the right-most bit and replace each
4-bit group with the equivalent hexadecimal symbol as in the following example.
• Convert the binary number to hexadecimal:
• 1100101001010111
• Solution:
• 1100 1010 0101 0111

• C A 5 7 = CA57
BINARY TO HEXADECIMAL
• (1010.1101)2 = (A.D)H

• (110.101) = (0110.1010) = (6.A)H

• (1110.11)2 = (1110.1100) = (E.C)H


Binary Arithmetic
• Basic of binary arithmetic
• Binary addition
• Binary subtraction
• Binary multiplication
• Binary division
Why Binary Arithmetic?
• Binary arithmetic is essential part of all the digital computers and
many other digital system.
• Hardware can only deal with binary digits, 0 and 1.
• Must represent all numbers, integers or floating point, positive or
negative, by binary digits, called bits.
• Can devise electronic circuits to perform arithmetic operations:
• add,
• subtract,
• multiply and divide, on binary numbers.
15
Addition
• From right to left, we add each pair of digits
• We write the sum, and add the carry to the next column on the left

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.

There are four rules of binary addition.

In fourth case, a binary addition is creating a sum of (1 + 1 = 10) i.e. 0 is


written in the given column and a carry of 1 over to the next column.
Binary Addition
Example − Addition
Binary Addition
When an input carry = 1 due to a previous result, the rules are

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.

It will be only necessary to borrow is when we try to subtract a 1 from a 0.

In this case a 1 is borrowed from the next higher-order column, which leaves a 0 in that column
and creates a 10

First lets see how subtraction work in decimal numbers.

How do we subtract 11 from 100 and get 89?


Binary Subtraction

The same calculation works in binary,

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

Subtract the binary number 00111 from 10101 and


show the equivalent decimal subtraction.
1 1 1
/ / /
10101 21

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

1’s complement: flip

2’s complement: flip + 1


Complement
1’s Complement

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

In digital circuits, the 1’s complement is formed by using inverters:


1 1 0 0 1 0 1 0

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 3: Addition of two negative numbers


Arithmetic's using 1’s Complement

Case 1: Addition of the positive number with a negative number when the
positive number has a greater magnitude.

• Calculate the 1's complement of the given negative number.



• Sum up with the given positive number.

• If we get the end-around carry 1, add this to the LSB.


Arithmetic's using 1’s Complement
Case 1: Addition of the positive number with a negative number when the
positive number has a greater magnitude.

Example: 1101 and -1001

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.

The 1's complement of the number 1001 is 0110.

2.Now, add both the numbers, i.e., 1101 and 0110;


1101 + 0110 = 1 0011

3.By adding both numbers, we get the end-around carry 1.


4.We add this end around carry to the LSB of 0011.
0011 + 1 = 0100
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.

Calculate the 1's complement of the negative value.

Sum it with a positive number.

In this case, we did not get the end-around carry.

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

1.First find the 1's complement of the negative number 1110.


So, for finding 1's complement, we change all 0 to 1, and all 1 to 0.
1's complement of the number 1110 is 0001.
2.Now, add both the numbers, i.e., 1101 and 0001;
1101 + 0001 = 1110
3.Now, find the 1's complement of the result 1110 that is the final result.
So, the 1's complement of the result 1110 is 0001, and we add a
negative sign before the number so that we can identify that it is a
negative number.
Arithmetic's using 1’s Complement
Case 3: Addition of two negative numbers

First find the 1's complement of both the negative numbers

Add both these complement numbers.

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

Example: -1101 and -1110 in five-bit register

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

• Take 1’s complement of the subtrahend


• Add with minuend
• If the result of above addition has carry bit 1, then add it to the least
significant bit (LSB) of given result
• If there is no carry bit 1, then take 1’s complement of the result which will be
negative
2’s Complement
2’s Complement

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.

Find the 2's complement of the given negative number.

Sum up with the given positive number.

If we get the end-around carry 1 then the number will be a


positive number and the carry bit will be discarded and
remaining bits are the final result.
Arithmetic's using 2’s Complement
Example: 1101 (13) and -1001 (9)

1.First, find the 2's complement of the negative number 1001.

2.The 2's complement of number 1001 is 0110+1 = 0111

3.Add both the numbers, i.e., 1101 and 0111;


1101 + 0111 = 1 0100

4.By adding both numbers, we get the end-around carry 1.

5.We discard the end-around carry.

6.So, the addition of both numbers is 0100 (4).


Arithmetic's using 2’s Complement
Case 2: Adding a positive value with a negative value in case the negative
number has a higher magnitude.

Find the 2's complement value of the negative number.

Add both numbers

Here, no end-around carry is found.

So, we take the 2's complement of the result to get the


final result.
Arithmetic's using 2’s Complement
Example: 1101 (13) and -1110 (-14)

1.Find the 2's complement of the negative number 1110.


0001+1=0010

2.Add both the numbers, i.e., 1101 and 0010;


1101+0010= 1111

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.

Add both these complement 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

3.By adding both numbers, we get the end-around carry 1.

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

•Perform the following Conversion:

• (87)10 = ( ? )2

• (42.76)10 = ( ? )2

• (425)10 = ( ? )8

• (10101)2 =( ? )10

• (110101001)2 = ( ? )10

You might also like