# Class Notes ```Class Notes
CS 250
Part II: Binary Arithmetic and Base Conversions
1
1. Base Conversion
1.1 Converting from Binary to Decimal
Approach:
Step 1: list the binary place values (weights) for all digits.
Step 2: add up all those place values where the digit is 1.
Example:
A = 1 0 0 1 1 0 1 1 1
28 27 26 25 24 23 22 21 20
= 28 + 25 + 24 + 22 + 21 + 20
= 619
1.2 Converting from Decimal to Binary
Approach:
Step 1: repeat following steps until the new value becomes zero.
i. Find the largest value of power of 2, which is smaller than the value, and subtract it
from the value.
ii. Replace the value by the new value in step i.
Step 2: construct the binary number: the place digit is one if the corresponding power of 2
was used, otherwise the digit is zero.
Example:
A = 173
=10101101
173 – 128 (27) = 45
45 – 32 (25) = 13
13 – 8 (23) = 5
5 – 4 (22) = 1
1 – 1 (20) = 0
2. Unsigned Binary Arithmetic
2
X
Y
Z
S
C
0
0
0
0
0
0
0
1
1
0
0
1
0
1
0
0
1
1
0
1
1
0
0
1
0
1
0
1
0
1
1
1
0
0
1
1
1
1
1
1
where X and Y are a pair of digits to be added, Z is the carry from previous digit, S is current
summation digit and C is the carry generated by the current digits.
Example:
10010110
+ 00111111
-------------------------------11010101
2.2 Subtraction
It is similar to the decimal subtraction, except that the borrow value is two.
Example:
10001010
-
01010110
--------------------------------00110100
2.3 Multiplication and Division
Binary multiplication and division are similar to the decimal binary multiplication and
division where the addition is the binary addition and the subtraction is the binary
subtraction.
3
Example (multiplication):
1 0 0 0 1 01 0
x
1011
-------------------------10001010
10001010
10001010
-----------------------------------10111101110
Example (division):
1101
1010 10001010
- 1010
-----------1110
- 1010
-----------10010
-
1010
------------1000
3. Signed Binary Representation and Arithmetic
3.1 2’s Complement Representation
Let B = bn-1bn-2 …. b1b0 be an n-bit binary number
Def. 2’s complement of B = bn1 bn2

e.g. if bi =1 then bi =0, if
Example:

b1 b0  1, where bi is the complement value of bi

bi =0 then bi =1.
n = 8, B = 0 1 0 1 01 0 1
2’s complement of B = 1 0 1 0 1 0 1 0 + 1 = 1 0 1 0 1 0 1 1
4
Def. The 2’s complement representation of a signed integer B (n-bit) is
 {B2 ' s

complement of |B|
if B 0
if B&lt;0
Example: (n=8)
2’s complement representation of 5 = 0 0 0 0 0 1 0 1
2’s complement representation of –5 = 1 1 1 1 1 0 1 1
Note the Most Significant Bit (MSB) of the 2’s complement representation is the sign bit the
number. The range of n-bit integers in 2’s complement representation is [ -2n-1, 2n-1 – 1].
3.2 Arithmetics of 2’s Complement Representation Numbers
Addition S = A + B
Step 1: S = A + B and ignore the end carry
Example: (n=8) A = 5, B=-3
0 0 0 0 0 1 0 1 (5)
+
1 1 1 1 1 1 0 1 (-3)
----------------------------------------(1) 0 0 0 0 0 0 1 0
↑
ignore
Subtraction
S =A– B
Step 1: S = A + 2’s complement of B and ignore the end carry
Example:
+
S = 5 – (-3) (n=8)
00000101
(5)
00000011
(2’s complement of 11111101)
---------------------------00001000
5
The multiplication and division of signed integers in 2’s complement representation are
similar to the unsigned multiplication and division, except that the result can be positive or
negative. If it is negative then the result should be represented in 2’s complement
representation.
Example:
3 x 5 = 0 1 1 x 1 0 1 = 15 = 0 0 0 0 1 1 1 1
-3 x 5 = -(15) = 1 1 1 1 0 0 0 1
6
```