CS-226:
Digital Logic Design
Binary Arithmetic
Virendra Singh
Professor
Computer Architecture and Dependable Systems Lab
Department of Electrical Engineering
Indian Institute of Technology Bombay
http://www.ee.iitb.ac.in/~viren/
E-mail:
[email protected] Lecture 2: 12 January 2021 CADSL
Digital System
Input Logic Design Output
12 Jan 2021 CS-226@IITB 2 CADSL
Why Binary Arithmetic?
3+5 =8
0011 + 0101 = 1000
12 Jan 2021 CS-226@IITB 3 CADSL
Binary
Arithmetic
12 Jan 2021 CS-226@IITB 4 CADSL
Single Bit Binary Addition
Given two binary digits (X,Y), we get the following
sum (S) and carry (C):
X 0 0 1 1
+Y +0 +1 +0 +1
CS 0 1 1 10
12 Jan 2021 CS-226@IITB 5 CADSL
Truth Table: Two Bit Adder
Binary Sum
X Y
(C)(S)
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 0
CARRY SUM
12 Jan 2021 CS-226@IITB 6 CADSL
Truth Tables of Logical Operations
• Truth tables are used to show/define the
relationships between the truth values of
– the individual propositions and
– the compound propositions based on them
p q pÙq pÚq pÅq pÞq pÛq
0 0 0 0 0 1 1
0 1 0 1 1 1 0
1 0 0 1 1 0 0
1 1 1 1 0 1 1
12 Jan 2021 CS-226@IITB 7 CADSL
Single Bit Binary Addition with Carry
Given two binary digits (X,Y), a carry in (Z) we get the
following sum (S) and carry (C):
Carry in (Z) of 0: Z 0 0 0 0
X 0 0 1 1
+Y +0 +1 +0 +1
CS 00 01 01 10
Carry in (Z) of 1: Z 1 1 1 1
X 0 0 1 1
+Y +0 +1 +0 +1
CS 01 10 10 11
12 Jan 2021 CS-226@IITB 8 CADSL
Full Adder: Include Carry Input
S=X+Y+Z
Z Y X
Decimal value Binary value
0 0 0 0 00
0 0 1 1 01
0 1 0 1 01
0 1 1 2 10
1 0 0 1 01
1 0 1 2 10
1 1 0 2 10
1 1 1 3 11
CARRY SUM
12 Jan 2021 CS-226@IITB 9 CADSL
Truth Table: Full Adder
Binary value
Z Y X
(C)(S)
0 0 0 0 0
0 0 1 0 1
0 1 0 0 1
0 1 1 1 0
1 0 0 0 1
1 0 1 1 0
1 1 0 1 0
1 1 1 1 1
CARRY SUM
12 Jan 2021 CS-226@IITB 10 CADSL
Multiple Bit Binary Addition
• Extending this to a multiple bit examples:
Carries 00000
Augend 01100
Addend 10001
Sum 11101
12 Jan 2021 CS-226@IITB 11 CADSL
Single Bit Binary Subtraction
• Given two binary digits (X,Y), we get the following
difference (S) and borrow (B)
X 0 0 1 1
-Y -0 -1 -0 -1
BS 00 11 01 00
12 Jan 2021 CS-226@IITB 12 CADSL
Truth Table: Two Bit Subtractor
Binary Difference
X Y
(B)(D)
0 0 0 0
0 1 1 1
1 0 0 1
1 1 0 0
BORROW DIFFERENCE
12 Jan 2021 CS-226@IITB 13 CADSL
Single Bit Binary Subtraction with
Borrow
• Given two binary digits (X,Y), a borrow in (Z) we get
the following difference (S) and borrow (B):
• Borrow in (Z) of 0: Z 0 0 0 0
X 0 0 1 1
-Y -0 -1 -0 -1
BS 00 11 01 00
• Borrow in (Z) of 1: Z 1 1 1 1
X 0 0 1 1
-Y -0 -1 -0 -1
BS 11 10 00 11
12 Jan 2021 CS-226@IITB 14 CADSL
Multiple Bit Binary Subtraction
• Extending this to a multiple bit example:
• Notes:
– The 0 is a Borrow-In to the least significant bit.
– If the Subtrahend > the Minuend, interchange and append a – to the
result.
Borrows 00000
Minuend 10110
Subtrahend 10010
Difference 00100
12 Jan 2021 CS-226@IITB 15 CADSL
Multiple Bit Binary Subtraction
• Extending this to a multiple bit examples:
• Notes: The 0 is a Borrow-In to the least significant bit.
If the Subtrahend > the Minuend, interchange and
append a – to the result.
Borrows 00110
Minuend 10110
Subtrahend 10011
Difference 00011
12 Jan 2021 CS-226@IITB 16 CADSL
Binary Multiplication
The binary multiplication table is simple:
0*0=0 | 1*0=0 | 0*1=0 | 1*1=1
Extending multiplication to multiple digits:
Multiplicand 1011
Multiplier x 101
Partial Products 1011
0000 -
1011 - -
Product 110111
12 Jan 2021 CS-226@IITB 17 CADSL
Signed
Numbers
12 Jan 2021 CS-226@IITB 18 CADSL
Signed Magnitude?
• Use fixed length binary representation
• Use left-most bit (called most significant bit or
MSB) for sign:
0 for positive
1 for negative
• Example: +18ten = 00010010two
–18ten = 10010010two
12 Jan 2021 CS-226@IITB 19 CADSL
Difficulties with Signed Magnitude
• Sign and magnitude bits should be differently
treated in arithmetic operations.
• Addition and subtraction require different logic
circuits.
• Overflow is difficult to detect.
• “Zero” has two representations:
+ 0ten = 00000000two
– 0ten = 10000000two
• Signed-integers are not used in modern computers.
12 Jan 2021 CS-226@IITB 20 CADSL
Problems with Finite Math
• Finite size of representation:
– Digital circuit cannot be arbitrarily large.
– Overflow detection – easy to determine when the
number becomes too large.
0000 0100 1000 1100 10000 10100
Infinite -4 0 4 8 12 16 20
-∞ ∞
universe
of integers
4-bit numbers
• Represent negative numbers:
– Unique representation of 0.
12 Jan 2021 CS-226@IITB 21 CADSL
4-bit Universe
0000 0000
1111 0001 1111 0001
16/0 15 0
Modulo-16 15
-0
(4-bit)
1100 12 4 0100 1100 12 -3 4 0100
universe
-7 7
8 7 8
0111
1000 1000
Only 16 integers: 0 through 15, or – 7 through 7
12 Jan 2021 CS-226@IITB 22 CADSL
One Way to Divide Universe
1’s Complement Numbers
Decimal Binary number
0000 magnitude
1111 0001 Positive Negative
15 0
-0 0 0000 1111
1100 12 -3 4 0100 1 0001 1110
-7 7 2 0010 1101
8
0111 3 0011 1100
1000
4 0100 1011
Negation rule: invert bits. 5 0101 1010
6 0110 1001
Problem: 0 ≠ – 0 7 0111 1000
12 Jan 2021 CS-226@IITB 23 CADSL
Another Way to Divide Universe
2’s Complement Numbers
Decimal Binary number
0000
magnitude Positive Negative
1111 0001
15 0 0 0000
-1
1 0001 1111
1100 12 -4 4 0100
2 0010 1110
-8 7
8 3 0011 1101
0111
Subtract 1 1000 4 0100 1100
on this side
5 0101 1011
Negation rule: invert bits 6 0110 1010
and add 1 7 0111 1001
8 1000
12 Jan 2021 CS-226@IITB 24 CADSL
Integers With Sign – Two Ways
• Use fixed-length representation, but no explicit
sign bit:
– 1’s complement: To form a negative number,
complement each bit in the given number.
– 2’s complement: To form a negative number, start
with the given number, subtract one, and then
complement each bit, or
first complement each bit, and then add 1.
• 2’s complement is the preferred representation.
12 Jan 2021 CS-226@IITB 25 CADSL
2’s-Complement Integers
• Why not 1’s-complement? Don’t like two zeros.
• Negation rule:
• Subtract 1 and then invert bits, or
• Invert bits and add 1
• Some properties:
• Only one representation for 0
• Exactly as many positive numbers as negative numbers
• Slight asymmetry – there is one negative number with no
positive counterpart
12 Jan 2021 CS-226@IITB 26 CADSL
General Method for Binary
Integers with Sign
• Select number (n) of bits in representation.
• Partition 2n integers into two sets:
• 00…0 through 01…1 are 2n/2 positive integers.
• 10…0 through 11…1 are 2n/2 negative integers.
• Negation rule transforms negative to positive, and vice-versa:
• Signed magnitude: invert MSB (most significant bit)
• 1’s complement: Subtract from 2n – 1 or 1…1 (same as “inverting all
bits”)
• 2’s complement: Subtract from 2n or 10…0 (same as 1’s complement +
1)
12 Jan 2021 CS-226@IITB 27 CADSL
Three Systems (n = 4)
10000
0000 0000 0000
1111 1111 1111
0 0010 0 0
–7 2 –0
–1
6 –6 6
–2 7 7 7
–0 –5 –7 1010 –8
1010 0111 1010 0111 0111
1000 1000 1000
1010 = – 2 1010 = – 5 1010 = – 6
Signed magnitude 1’s complement integers 2’s complement integers
12 Jan 2021 CS-226@IITB 28 CADSL
Three Representations
Sign-magnitude 1’s complement 2’s complement
000 = +0 000 = +0 000 = +0
001 = +1 001 = +1 001 = +1
010 = +2 010 = +2 010 = +2
011 = +3 011 = +3 011 = +3
100 = - 0 100 = - 3 100 = - 4
101 = - 1 101 = - 2 101 = - 3
110 = - 2 110 = - 1 110 = - 2
111 = - 3 111 = - 0 111 = - 1
(Preferred)
12 Jan 2021 CS-226@IITB 29 CADSL
2’s Complement Numbers (n = 3)
0
000
subtraction addition
-1 111 001 +1
Pos
Neg
itiv
ativ
e
010 +2
e
-2 110
num
num
ber
ber
s
s
-3 101 011 +3
100 rf low Negation
Ove
-4
12 Jan 2021 CS-226@IITB 30 CADSL
Summary
• For a given number (n) of digits we have a finite
set of integers. For example, there are 103 =
1,000 decimal integers and 23 = 8 binary
integers in 3-digit representations.
• We divide the finite set of integers [0, rn – 1],
where radix r = 10 or 2, into two equal parts
representing positive and negative numbers.
• Positive and negative numbers of equal
magnitudes are complements of each other: x +
complement (x) = 0.
12 Jan 2021 CS-226@IITB 31 CADSL
Summary: Defining Complement
• Decimal integers:
• 10’s complement: – x = Complement (x) = 10n – x
• 9’s complement: – x = Complement (x) = 10n – 1 – x
– For 9’s complement, subtract each digit from 9
– For 10’s complement, add 1 to 9’s complement
• Binary integers:
• 2’s complement: – x = Complement (x) = 2n – x
• 1’s complement: – x = Complement (x) = 2n – 1 – x
– For 1’s complement, subtract each digit from 1
– For 2’s complement, add 1 to 1’s complement
20
12 Aug 2020
Jan 2021 EE-224@IITB
CS-226@IITB 31
32 CADSL
Understanding Complement
• Complement means “something that
completes”:
e.g., X + complement (X) = “Whole”.
• Complement also means “opposite”, e.g.,
complementary colors are placed opposite
in the primary color chart.
• Complementary numbers are like electric
charges. Positive and negative charges of
equal magnitudes annihilate each other.
12 Jan 2021 CS-226@IITB 33 CADSL
2’s-Complement Numbers
000 001 010 011 100 101
Infinite
-∞ . . . -1 0 1 2 3 4 5... ∞
universe
of integers
1000 1000 Finite
Universe
999 000 111 000
Finite of 3-bit
001 001
Universe of binary
3-digit numbers
Decimal
numbers
499 011
501 101
500 100
12 Jan 2021 CS-226@IITB 34 CADSL
2’s-Complement to Decimal
Conversion
n-2
bn-1 bn-2 . . . b1 b0 = – 2n-1bn-1 + Σ 2i bi
i=0
8-bit conversion box
-128 64 32 16 8 4 2 1
Example -128 64 32 16 8 4 2 1
1 1 1 1 1 1 0 1
– 128 + 64 + 32 + 16 + 8 + 4 + 1 = – 128 + 125 = – 3
12 Jan 2021 CS-226@IITB 35 CADSL
Thank You
12 Jan 2021 CS-226@IITB 36 CADSL