Lecture 4-5 Binary Arthmetics
Lecture 4-5 Binary Arthmetics
Arithmetic
Department of Computing
Shifa Tameer-e-Millat University (STMU).
06/11/2024
Computer : Number Representation,
Arithmetic Operations (Addition,
Subtraction )
2
Binary Arithmetic
• Binary Addition
• Binary Subtraction
• Binary Multiplication
• Binary Division
3
Binary Addition
4
Binary Addition
Carry
1 1 1 1 Decimal
1
1st Number
1 0 10 1 11
2nd Number
0 1 1 0 6
3rd Number
1 0 0 0 8
4th Number
0 0 1 1 3
Sum
1 1 1 0 0 28
5
Binary Subtraction
6
Binary Subtraction
7
Binary Subtraction
Borrow
1 1 Decimal
1st Number
1 1 0 1 0 26
2nd Number
1 0 1 0 1 21
Difference
0 0 1 0 1 5
General Rules:
9
Binary Multiplication
• Divide the sum of two digits by the number base (16). The quotient
becomes the carry value, and the remainder is the sum digit.
1 1
36 28 28 6A
42 45 58 4B
78 6D 80 B5
21 / 16 = 1, rem 5
12
Hexadecimal Subtraction
16 + 5 = 21 – 7 = 14 = E
1
C6 75
A2 47
24 2E
Practice: The address of var1 is 00400020. The address of the next variable
after var1 is 0040006A. How many bytes are used by var1?
13
Addition and Multiplication Examples
14
15
Hexadecimal Complement
16
Computer : Data Representation
• Data in computers is stored in binary format i.e.
0’s and 1’s
• Numbers in computers can be of Types
– Unsigned Numbers (Positive)
– Signed Numbers (Positive , Negative)
17
Unsigned Numbers
• Positive numbers/unsigned numbers are stored
in binary number format inside computers
• No of bits required to represent an unsigned
number depends on its magnitude
• Example
• 5 can be represented in 3 bits i.e. (101)2
• 10 can be represented in 4 bits i.e. (1010)2
• 25 can be represented in 5 bits i.e. (11001)2
Sign
Magnitude
1 1 1 0 (−6)10 1 1 1 0 (−6)10
• An appropriate format should represent the signed
data and handle arithmetic operations
• Solution Complements !!!
22
Compliment of a Number
• Computers use Complements to store signed
numbers (Positive and Negative)
– 1’s Complement ( r - 1 )
– 2’s Complement ( r)
• Applications of Complements
• Complements are used for subtraction inside
computers
• Complements can also be used in
operations logic
– 1’s complement is used for bit inversion
23
1’s Complement
• Complements used by Computers for
simplification
are of Subtraction operation
• 1’s Complement of number is obtained by
replacing
a
– All ‘0’s by ‘1’s
–All ‘1’s by ‘0’s
Example : (10110000)2
1’s Complement = (01001111)2
01010000 01010000
26
Signed Numbers :
2’s Complement Representation
• Computers store signed numbers in 2’s complement
binary format.
– Positive numbers are represented in binary
– Negative numbers are represented in 2’s Complement form
0 Magnitude (Binary)
Example
(+3)10 (0 011)2
(−3)10 (1 101)2
27
2’s Complement Representation :
Range
• For a 4-bit binary Decimal 2’s Comp.
representation, range is
number +7 0111
+6 0110
-8 ≤ Number ≤ +7 +5 0101
• For n-Bit Representation +4 0100
+3 0011
−2n−1 ≤ Number ≤ +2n−1 − 1 +2 0010
+1 0001
+0 0000
• Single representation for ‘0’
–1 1111
–2 1110
–3 1101
–4 1100
–5 1011
–6 1010
–7 1001
–8 1000
28
Range of Unsigned & Signed Numbers
• 4-Bit Numbers = 24 Decimal 2’s Comp.
+7 0111
• Unsigned No. Representation +6 0110
0 ≤ Number ≤ + 15 +5 0101
+4 0100
0 ≤ Number ≤ + 24 − 1 +3 0011
• n-Bit Representation +2 0010
0 ≤ Number ≤ + 2n − 1 +1 0001
+0 0000
• Signed No. Representation –1 1111
–2 1110
− 8 ≤ Number ≤ + 7
–3 1101
−23 ≤ Number ≤ + 23 − 1 –4 1100
• n-Bit Representation –5 1011
–6 1010
−2n−1 ≤ Number ≤ + 2n−1 − 1
–7 1001
–8 1000
29
1s, 2s, 9s and 10s Complement
30
Arithmetic Operations
( Addition,
Subtraction )
31
Binary Addition
• For binary addition
– Represent the numbers in binary form
– Add numbers bit by bit
– Propagate the carry, if required
(9)10 + (5)10
1001
+ 0101
1110 + 14
32
Binary Subtraction : On Paper
• Represent the numbers in binary form
• Subtract numbers bit by bit
• Borrow during binary subtraction has value of 2
• Example
(5)10 – (1)10 (8)10 –
(3)10
0101 1000
• Computer’s do - 0001 -
subtraction in 2’s 0011
Complement form
0+140 0 0+150 1
33
Binary Subtraction in Computers:
2’s Complement Arithematic
• Represent the numbers in binary form
• Take 2’s complement of 2nd no. and add into the 1st
no.
(5)10 – (1)10 (5)10 – (6)10
• If Carry = 1 (+5)10 + (-1)10 (+5)10 + (-6)10
ignore it, and the
result is 0101 0101
positive
+ 1111 + 1010
• If Carry = 0
Result is 0 1111
negative
1 0100
+4 –1
34
Binary Subtraction
00001100 00001100
– 00000011 11111101
00001001
35
Subtraction using Complements
36
Subtraction using Complements
37
Subtraction using Complements
38
Learn How To Do the Following:
39
Practice Questions : Binary Numbers
• Compute 1’s Complement of following Numbers
» 0000 , 101011 , 01001011
• Compute 2’s Complement of following Numbers
» 0000 , 010101 , 11010010
• Compute 1’s and 2’s complements of the
following binary numbers:
» 11101010, 01111110 , 00000001,
10000000
• Subtract 2nd no. from 1st in each pair
» (010010 , 000111) ,
» (01010010, 00110100)
40
Practice Questions …
• Subtract the following unsigned binary numbers
using the 2’s-complement of the subtrahend.
Where result should be negative, 2’s
complement it and affix a minus sign
» 110100 – 10101
» 1011 – 110000
» 101010 – 101011
• Convert following numbers to binary and
compute result using 2’s complement
arithematic. Conver the answers back to decimal
» (37 – 61)
» (-55 + 70)
41
Binary Codes
42
Binary Codes
• Group of n bits
– Up to 2n unique Combinations
– Each combination represents an element of
information
43
4
3
Binary Coded Decimal (BCD)
• In BCD representation, each decimal digit is
represented by a 4 bit binary code
Decimal BCD
• BCD code for 8 is 1000 0 0000
• BCD code for 35 is 1 0001
2 0010
0011 0101 3 0011
4 0100
35 5 0101
6 0110
7 0111
0011 0101 8 1000
9 1001
9/29/2020 458
BCD Addition
• If sum of two BCD Digits is ≤ 9, BCD addition is
like binary addition
• If sum of two BCD Digits is > 9, a
correction factor of 6 must be added
5 0101 7 0111
+ 3 + 0011 + 5 + 0101
8 1000 12 1100 Invalid
BCD Code
+ 0110
0001 0010
47
BCD Addition Example
48
Other Decimal Codes
49
BCD Subtraction
• If subtraction of two BCD Digits is ≤ 9,
BCD subtraction is like binary subtraction
• If subtraction of two BCD Digits is > 9,
a correction factor of 6 must be subtracted
8 1000 17 0001 0 1 1 1
- 2 - 0010 - 9 - 1001
6 0110 8 1110 Invalid
BCD Code
- 0110
1000
5 50
0
51
52
53
54
Excess-3 Code
• Excess-3 Code is Decimal Excess-3 Binary
obtained by adding 3 00 0011 0000
to each of the 01 0100 0001
decimal digit before 02 0101 0010
03 0110 0011
binary representation 04 0111 0100
• E.g. Excess-3 05 1000 0101
representation of 35 06 1001 0110
07 1010 0111
08 1011 1000
35 09 1100 1001
0110 1000
55
Gray Code / Reflected Code
• Gray Code is a Decimal Gray Binary
binary code in which 00 0000 0000
one bit changes from 01 0001 0001
02 0011 0010
one code to the next 03 0010 0011
code 04 0110 0100
• First and last code 05 0111 0101
06 0101 0110
also differ by one bit 07 0100 0111
(Cyclic Codes) 08 1100 1000
• E.g. 4-bit Gray codes 09 1101 1001
10 1111 1010
are shown in table 11 1110 1011
12 1010 1100
13 1011 1101
14 1001 1110
15 1000 1111
56
Gray Code Generation
• N-bit Gray code using Reflect and Prefix method
57