0% found this document useful (0 votes)
25 views

Lecture 4-5 Binary Arthmetics

Uploaded by

Defenders
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Lecture 4-5 Binary Arthmetics

Uploaded by

Defenders
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 57

Data Representation/ Binary

Arithmetic

Instructor: Muhammad Zeshan Qurashi

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

• Four Basic rules for binary addition

1st digit 2nd digit Sum Carry


0 0 0 0
0 1 1 0
1 0 1 0
1 1 0 1

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

• Subtraction of binary digits

1st digit 2nd digit Difference Borrow


0 0 0 0
0 1 1 1
1 0 1 0
1 1 0 0

6
Binary Subtraction

Subtraction of binary numbers


Borrow
1 Decimal
1st Number
1 0 1 1 11
2nd Number
0 1 1 0 6
Difference
0 1 0 1 5

7
Binary Subtraction

Subtraction of binary numbers

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:

• Work with two numbers at a time to make it simple


• Work in one column at a time
• Watch the borrows
8
Binary Multiplication

• Multiplication of binary digits

1st digit 2nd digit Product


0 0 0
0 1 0
1 0 0
1 1 1

9
Binary Multiplication

• Multiplication of binary numbers


1101 (13)
x 101 (5)
1st product term 1101
2nd product term 0000x
3rd product term 1101xx

Product 1000001 (65)


10
Binary Division

• Binary division process is similar to decimal division


10
101 | 1101
101
011
000
1
1
11
Hexadecimal Addition

• 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

Important skill: Programmers frequently add and subtract the


addresses of variables and instructions.

12
Hexadecimal Subtraction

• When a borrow is required from the digit to the left, add 16 to


the current digit's value:

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)

• Arithmetic Operations on Signed Data


– Addition
– Subtraction
– Multiplication

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

• Maximum number in N-bits = 2N - 1


• Minimum Number ? 18
Signed Numbers Representation
• For signed numbers, sign information has to be
saved in 0’s and 1’s
• Signed Numbers can be represented as
– Sign Magnitude Form
– Complement Form
» 1’s Complement
» 2’s Complement
• All signed number representations use the most
significant bit (MSB) to represent the sign of the
number
– ‘0’  Positive Number
– ‘1’  Negative Number
19
Signed Magnitude Representation
• Magnitude does not change with sign. MSB is ‘0’ for
+ve numbers, ‘1’ for –ve numbers
S Magnitude (Binary)
(+3)10  ( 0 0 1 1 )2
(−3)10  ( 1 0 1 1 )2

Sign

Magnitude

• Issues with Sign Magnitude Representation


– Two representations for ‘+0’ and ‘-0’ 20
Signed Magnitude Representation:
Issues
Decimal Sign Mod.
• For a 4-bit binary
+7 0111
representation,
number range is +6 0110
-7 ≤ Number ≤ +7 +5 0101
+4 0100
• For n-Bit Representation +3 0011
+2 0010
+1 0001
−2n−1+1 ≤ Number ≤ +2n−1 − 1 +0 0000
–0 1000
–1 1001
• Sign Magnitude has –2 1010
two representations for ‘0’ –3 1011
–4 1100
– +0  0000 –5 1101
–6 1110
–-0 1000 –7 1111
9/21/2020 21
Signed Magnitude Representation :
Arithmetic Operations
• Sign Magnitude representation can’t include the sign
bit in Addition operation
0 0 1 1  (+3)10 1 0 0 1  (- 1)10
+ 1 0 1 1  (−3)10 + 0 1 0 1  (+5)10

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

• 1’s Complement is also used in logic operations


Complement of Complement of a Number
is original number
24
1’s Complement : Issues
Decimal 1’s Comp.
• For a 4-bit binary
+7 0111
representation,
number range is +6 0110
-7 ≤ Number ≤ +7 +5 0101
+4 0100
• For n-Bit Representation +3 0011
−2n−1+1 ≤ Number ≤ +2n−1 − 1 +2 0010
+1 0001
+0 0000
• 1’s Complement has –0 1111
two representations for ‘0’ –1 1110
–2 1101
– +0  0000 –3 1100
–4 1011
–-0 1111 –5 1010
–6 1001
–7 1000
25
2’s Complements
• 2’s Complement of a number is computed by :
– Take 1’s complement and add 1 to LSB
OR
– Toggle all bits to the left of the first ‘1’ from the
right
Example:
Number:
10110000 10110000
1’s Comp.:
01001111
+ 1

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)

1 Code (2’s Comp.)

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

• When subtracting A – B, convert B to its two's


complement
• Add A to (–B)

00001100 00001100
– 00000011 11111101
00001001

Practice: Subtract 0101 from 1001.

35
Subtraction using Complements

36
Subtraction using Complements

37
Subtraction using Complements

38
Learn How To Do the Following:

• Form the two's complement of a hexadecimal integer


• Convert signed binary to decimal
• Convert signed decimal to binary
• Convert signed decimal to hexadecimal
• Convert signed hexadecimal to decimal

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

• Common Binary Codes


– BCD
– Excess 3
– Gray Code
– ASCII Code

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

• If a BCD digit is greater than 9 or 1001, it will


be invalid BCD Code
447
BCD Codes : Applications
• Real world data is in Decimal
• Computers/Digital Systems process data in
Binary
• BCD Codes are used in data display E.g. Seven
Segment Displays, LCD Displays

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

Two BCD Digits


4 46
6
BCD Addition

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

You might also like