Lecture 3 IS064
Lecture 3 IS064
Binary
Arithmetic
1/18/2023 2
Why Binary Arithmetic?
3+5 =8
1/18/2023 3
Why Binary Arithmetic?
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.
1/18/2023 4
Positive Integers
Decimal system: made of 10 digits, {0,1,2, . . . , 9}
41 = 4×101 + 1×100
255 = 2×102 + 5×101 + 5×100
Binary system: made of two digits, {0,1}
00101001 = 0×27 + 0×26 + 1×25 + 0×24
+1×23 + 0×22 + 0×21 + 1×20
= 32 + 8 +1 = 41
11111111 = 255, largest number with 8
binary digits, 28-1
1/18/2023 5
Base or Radix
For decimal system, 10 is called the base
or radix.
Decimal 41 is also written as 4110 or 41ten
Base (radix) for binary system is 2.
Thus, 41ten = 1010012 or 101001two
Also, 111ten = 1101111two
and 111two = 7ten
What about negative numbers?
1/18/2023 6
Signed Magnitude – What Not to Do
1/18/2023 7
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.
1/18/2023 8
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.
1/18/2023 9
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
1/18/2023 10
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
1/18/2023 11
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
Subtract 1 -8 7
on this side 8 3 0011 1101
0111
1000 4 0100 1100
Negation rule: invert bits 5 0101 1011
and add 1 6 0110 1010
7 0111 1001
8 1000
1/18/2023 12
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.
1/18/2023 13
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
1/18/2023 14
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)
1/18/2023 15
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
1/18/2023 16
Three Representations
Sign-magnitude 1’s complement 2’s complement
(Preferred)
1/18/2023 17
2’s Complement Numbers (n = 3)
0
000
subtraction addition
-1 111 001 +1
-2 110 010 +2
-3 101 011 +3
100 Negation
-4
1/18/2023 18
2’s Complement n-bit Numbers
Range: – 2n –1 through 2n –1 – 1
Unique zero: 00000000 . . . . . 0
Negation rule: see slide 11 or 13.
Expansion of bit length: stretch the left-most bit
all the way, e.g., 11111101 is still 101 or – 3.
Also, 00000011 is same as 011 or 3.
Most significant bit (MSB) indicates sign.
Overflow rule: If two numbers with the same sign
bit (both positive or both negative) are added,
the overflow occurs if and only if the result has
the opposite sign.
Subtraction rule: for A – B, add – B and A.
1/18/2023 19
Addition
Adding bits:
0+0= 0
0+1= 1
1+0= 1
carry
1 + 1 = (1) 0
Adding integers:
1 1 0
0 0 0...... 0 1 1 1 two = 7ten
+ 0 0 0...... 0 1 1 0 two = 6ten
= 0 0 0 . . . . . . 1 (1)1 (1)0 (0)1 two = 13ten
1/18/2023 20
Subtraction
Direct subtraction
1/18/2023 21
Overflow: An Error
Examples: Addition of 3-bit integers (range - 4 to +3)
000
-2-3 = -5 110 = -2
111 0 001
+ 101 = -3 1
-1
= 1011 = 3 (error) – +
2 010
110 -2
3+2 = 5 011 = 3
-3 3
010 = 2 101 -4 011
= 101 = -3 (error) 100 Overflow
crossing
Increase
0011 of 4-bit
1100
0100 binary
1011 integers
0101
1010 0110
1001 1000 0111
Forbidden fence
1/18/2023 23
Adding Two Bits
s=a+b
a b
Decimal Binary
0 0 0 00
0 1 1 01
1 0 1 01
1 1 2 10
CARRY SUM
1/18/2023 24
Half-Adder Adds two Bits
“half” because it has no carry input
Adding two bits:
a b a+b
0 0 00 AND carry
0 1 01
HA
1 0 01 a
1 1 10 XOR sum
b
carry sum
1/18/2023 25
Full Adder: Include Carry Input
s=a+b+c
a b c
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
1/18/2023 26
Full-Adder Adds Three Bits
OR carry
AND
AND
HA
HA
a XOR sum
XOR
b
c
FA
1/18/2023 27
32-bit Ripple-Carry Adder
c32
c32 c31 . . . c2 c1 0 a31 (discard)
a31 . . . a2 a1 a0 b31 FA31
+ b31 . . . b2 b1 b0 s31
c31
s31 . . . s2 s1 s0
a2
b2 FA2 s2
a1
b1 FA1 c2 s1
a0
b0 FA0 c1 s0
c0 = 0
1/18/2023 28
Binary Multiplication (Unsigned)
1 0 0 0 two = 8ten multiplicand
1 0 0 1 two = 9ten multiplier
____________
1000
0000 partial products
0000
1000
____________ Basic algorithm: For n = 1, 32,
1 0 0 1 0 0 0two = 72ten only If nth bit of multiplier is 1,
then add multiplicand × 2 n –1
to product
1/18/2023 29
Digital Circuits for Multiplication
Need:
Three registers for multiplicand, multiplier and product.
Adder or arithmetic logic unit (ALU).
What is a register
A memory device – unit cell stores one bit.
A 32-bit register has 32 storage cells. It can store a
32-bit integer.
1 bit right shift divides integer by 2
bit 32 bit 0
1/18/2023 30
Multiplication Flowchart
Start Initialize product register to 0 Partial product number, n = 1
n = 32 n < 32
Done n=? n=n+1
1/18/2023 31
Example: 0010two× 0011two
0010two × 0011two = 0110two, i.e., 2ten × 3ten = 6ten
1/18/2023 32
Multiplying with Signs
1/18/2023 33
Example 1: 1010two× 0011two
1010two × 0011two = 101110two, i.e., – 6ten × 3ten = – 18ten
1/18/2023 34
Symbol Representation
Early versions (60s and 70s)
Six-bit binary code (Control Data Corp., CDC)
EBCDIC – extended binary coded decimal
interchange code (IBM)
Presently used –
ASCII – American standard code for information
interchange – 7 bit code specified by American
National Standards Institute (ANSI), see Table
1.11 on page 63; an eighth MSB is often used as
parity bit to construct a byte-code.
Unicode – 16 bit code and an extended 32 bit
version
1/18/2023 35
ASCII
Each byte pattern represents a character (symbol)
– Convenient to write in hexadecimal, e.g., with even
parity,
00000000 0ten 00hex null
01000001 65ten 41hex A
11100001 225ten E1hex a
– Table 1.11 on page 63 gives the 7-bit ASCII code.
– C program – string – terminating with a null byte (odd
parity):
01000101 01000011 01000101 10000000
69ten or 45hex 67ten or 43hex 69ten or 45hex 128ten or 80hex
E C E (null)
1/18/2023 36
Wide Range of Numbers
A large number:
976,000,000,000,000 = 9.76 × 1014
A small number:
0.0000000000000976 = 9.76 × 10 –14
1/18/2023 37