Arithmetic
Arithmetic
Binary Representation
Unsigned :
• The binary number
1
20-02-2024
Binary Representation
2’s Complement:
32 bits can only represent 232 numbers – if we wish to also Each number represents the quantity
represent negative numbers, we can represent 231 positive -231 x31 + x30 230 + x29 229 + … + x1 21 + x0 20
numbers (incl zero) and 231 negative numbers
Why is this representation favorable?
0000 0000 0000 0000 0000 0000 0000 0000two = 0ten Consider the sum of 1 and -2 …. we get -1
0000 0000 0000 0000 0000 0000 0000 0001two = 1ten Consider the sum of 2 and -1 …. we get +1
… This format can directly undergo addition
0111 1111 1111 1111 1111 1111 1111 1111two = 231-1 without any conversions!
Binary Representation
2’s Complement: 0000 0000 0000 0000 0000 0000 0000 0000two = 0ten
0000 0000 0000 0000 0000 0000 0000 0001two = 1ten
…
0111 1111 1111 1111 1111 1111 1111 1111two = 231-1
Note that the sum of a number x and its inverted representation x’ always equals a string of 1s (-1)
x + x’ = -1
x’ + 1 = -x … hence, can compute the negative of a number by
-x = x’ + 1 inverting all bits and adding 1
2
20-02-2024
ALU Design
1bit ALU: Q. How to do subtraction?
ALU 31
ALU 0 – ALU 30
3
20-02-2024
ALU 0 – ALU 30
Carry-save array
4
20-02-2024
1 1 0 1 Multiplicand (13)
1 0 1 1 Multiplier (11)
---------
1101
1101
0000
1101
-----------------
1 0 0 0 1 1 1 1 Product (143)
Unsigned Multiplication
Unsigned multiplication:
M
1101
C Q
0 0000 1011 Initialization
A
5
20-02-2024
Signed Multiplication
Booth’s Algorithm
Example: 2 x -3 = -6, or 0010 x 1101 = 1111 1010
M
0010 Q-1
Q
0000 1101 0 Initialization Current bit Bit to right Operation
A
1 0 Subtract
1111 0101 1 Arithmetic right shift 1111 1010 1 Arithmetic right shift
6
20-02-2024
2’s complement of 𝑎
Unsigned Division
Restoring division:
Do the following 𝒏 times
1. Shift 𝐴 and 𝑄 left one bit
2. 𝐴 ← 𝐴 − 𝑀
3. If the sign of 𝐴 is 1 i.e., 𝑎 = 1
𝑞 ←0
𝐴 ← 𝐴 + 𝑀 //Restore After division 𝑛-bit quotient is in 𝑄 and the remainder is in 𝐴
else
𝑞 ←1
7
20-02-2024
8
20-02-2024
Signed Division
Simplest solution: Use unsigned division and negate the quotient if signs of divisor and dividend disagree.
Note: The equation 𝐷𝑖𝑣𝑖𝑑𝑒𝑛𝑑 = 𝑄𝑢𝑜𝑡𝑖𝑒𝑛𝑡 × 𝐷𝑖𝑣𝑖𝑠𝑜𝑟 + 𝑅𝑒𝑚𝑎𝑖𝑛𝑑𝑒𝑟 must always hold
9
20-02-2024
Floating-point standard:
• Defined by IEEE Std 754-1985
• Developed in response to divergence of representations
• Portability issues for scientific code
• Now almost universally adopted
• Two representations
• Single precision (32-bit)
• Double precision (64-bit)
10
20-02-2024
Example
• Represent 0.75 • What number is represented by the single-
• 0.75 = 1.12 × 2–1 precision float
• S=0 11000000101000…00
• Fraction = 1000…002 • S=1
• Exponent = –1 + Bias • Fraction = 01000…002
• Single: –1 + 127 = 126 = 011111102 • Exponent = 100000012 = 129
• Single: 0011111101000…00
• x = (–1)1 × (1 + 012) × 2(129 – 127)
Q. Represent 0.5 = (–1) × 1.25 × 22
= –5.0
Q. How to represent 0 ?
A. Exponent 00…0, Fraction: 00…0
11
20-02-2024
Summary
Value inf 2 special cases up top that use the 0 255 00…0
Value NAN reserved exponent field of 255 0 255 xx….x
Highest Norm ~2 x 2127 0 254 11….1
12
20-02-2024
13