1
CS1010 Introduction to Computing
Lecture 08
Review – multiplying powers
• For common bases, add powers.
26 × 210 = 216 = 65,536
or…
26 × 210 = 64 × 210 = 64k
Binary Addition (1 of 2)
• Two 1-bit values
A B A + B
0 0 0
0 1 1
1 0 1
1 1 10
BINARY ADDITION
• Two n-bit values
– Add individual bits
– Propagate carries
– Example:
10101 21 + 11001 + 25
101110 46
EXAMPLE
• Add 01011101
2 and 001100102
• Solution:
0 1 0 1 1 1 0 1
+ 0 0 1 1 0 0 1 0
1 1
1 1
1 0 0 0
0 0
0 0
1 1
EXAMPLE
• Add 111101 and 10111.
1 1 1 0 1 1 1 1 1 1 0 + 0 0 0
0 1 1
Multiplication (1 of 3)
• Decimal (just for fun)
Multiplication (2 of 3)
• Binary, two 1-bit values
A B A × B
0 0 0
0 1 0
1 0 0
Multiplication (3 of 3)
• Binary, two n-bit values
– As with decimal values
– E.g.,
1110 x 1011 1110 1110 0000 1110
MULTIPLICATION
• Decimal
35 x 105 175 000 35
EXAMPLE
• Multiply the Binary numbers 1111 and 111.
1111
x 111
1111 1111 1111
EXAMPLE
• Bit by bit.
0
1 1 1 1
0
1 1 0
0
0 0 0 0
0
1 1 1 1
0
1 1 1 1
0 0 0
REPRESENTATION OF NEGATIVE
NUMBERS
• There are many methods of representing signed
number in binary:
– Sign-Magnitude Method
– 1’s Complement Method
– 2’s Complement Method
1’s COMPLEMENT METHOD
• 1’s complement of a binary number can bedirectly obtained by changing all 0’s to 1’s and all 1’s to 0’s.
• Example:
Take 1’s complement of the binary number 01100110 directly.
Representation of Negative Numbers
using 1’s Complement Method
• Algorithm
– First determine the number of bits to represent the number.
– Convert the modules of the given number in binary.
– Place a 0 in MSB and binary conversion of the number in remaining bits.
Example
• Represent -5410 in 1’s complement form using 8 bits.
2 54
27 0 2
13 1 2
6 1 2
3 02 1 12 0 1
54
10 = 01101102
54
10 = 001101102 (in 8 bits)
So -54
10 in 1’s complement
form:
-54
2’s COMPLEMENT METHOD
• Algorithm– First taking 1’s complement.
Example
• Take 2’s complement of the binary number 01100110 directly.
Original number 01100110 1’s Complement 10011001
+1
Representation of Negative Numbers
using 2’s Complement Method
• Algorithm
– First determine the number of bits to represent the number.
– Convert the modules of the given number in binary.
– Place a 0 in MSB and binary conversion of the number in remaining bits.
Example
• Represent -5410 in 2’s complement form using 8 bits. Modulus of -54
10 = 54
2 54
27 0 2
13 1 2
6 1 2
3 02 1 12
0 1
54
10 = 01101102
54
10 = 001101102 (in 8 bits)
Take 1’s complement of
00110110
= 11001001
+ 1
= 11001010
BINARY ADDITION
• Two 1-bit values
A B A + B
0 0 0
0 1 1
1 0 1
1 1 10
BINARY SUBTRACTION
• Modern day computers use 2’s complement
or 1’s complement method for performing
subtraction.
• Example:
Calculate 38 – 29 using 8-bit 1’s complement method.
• Write both the numbers in binary form using 8 bits.
38
10 = 001001102
29
10 = 000111012
Represent negative numbers in 1’s complement
-29
10 = 111000102
Add the number & its 1’s complement
00100110
2
+ 11100010
2
Carry: 1 00001000
2
Add End Carry:+ 1 00001001
EXAMPLE
• Calculate – 54 – 30 using 8-bit 1’s complement
method.
• Solution:
(– 54) + (– 30)
54
10 = 001101102
30
10 = 000111102
Represent negative numbers in 1’s complement
-54
10 = 110010012
-30
Add numbers in its 1’s complement form
11001001
2
+ 11100001
2
Carry: 1 10101010
2
Add End Carry:+ 1 10101011
2
Convert the 1’s complement result into decimal. As MSB is 1 so it’s a negative number
10101011
Exercises
• Perform following calculations using 1st complement method.
a) 0111000 + 1011100 b) 25 – 50
SUBTRACTING USING 2’s COMPLEMENT
METHOD
• Example:
Calculate 38 – 29 using 8-bit 2’s complement
method.
Solution:
38 + (-29)
38
10 = 001001102
29
Represent negative numbers in 2’s complement
Take 1’s complement of 29 = 00011101
and then add 1
-29
10 = 111000112 (2’s complement)
Add 2’s complement representation and ignore the end carry.
00100110
2
+ 11100011
2
Carry: 1 00001001
2
EXAMPLE
• Calculate – 54 – 30 using 8-bit 2’s complement
method.
• Solution:
(– 54) + (– 30)
54
10 = 001101102
30
10 = 000111102
Represent negative numbers in 2’s complement
-54
10 = 110010102
-30
Add 2’s complement representation
11001010
2
+ 11100010
2
Carry: 1 10101100
2
Convert the 2’s complement result into decimal. As MSB is 1 so it’s a negative number
10101100
2 = 01010011 = -84
Exercises
• Perform following calculations using 2nd complement method.
a) 0111000 – 1011100 b) 25 – 50
FRACTIONS
• Decimal to decimal.
3.14 => 4 x 10-2 = 0.04 1 x 10-1 = 0.1
3 x 100 = 3
FRACTIONS
• Binary to Decimal
10.1011 => 1 x 2-4 = 0.0625 1 x 2-3 = 0.125
0 x 2-2 = 0.0 1 x 2-1 = 0.5 0 x 20 = 0.0 1 x 21 = 2.0
DECIMAL TO BINARY
• 3.14579
3.14579
.14579 x 2 0.29158 x 2 0.58316 x 2 1.16632 x 2 0.33264 x 2 0.66528 x 2 1.33056 etc.
OCTAL TO DECIMAL
• 127.548
127.54
8 => 1 x 8
2
= 64.000 2 x 81 = 16.000
7 x 80 = 7.000 5 x 8-1 = 0.625 4 x 8-2 = 0.0625 87.6875
EXAMPLE
• Convert 630.48 into decimal equivalent.
630.4
8 = 408.510
630.4 => 6 x 82 = 384 3 x 81 = 24
0 x 80 = 0 4 x 8-1 = 0.5
DECIMAL TO OCTAL
• 185.310
8 185 23 1 8
2 7 8
0 2
185
10 = 02718
0.3
10 = 0.231468
Result Fractional Part Integral Part
8 x 0.3 8 x 0.4 8 x 0.2 8 x 0.6 8 x 0.8
2.4 3.2 1.6 4.8 6.4 4 2 6 8 4 2 3 1 4 6 185.3
HEXADECIMAL TO DECIMAL
• 2B.C416
2B.C4
16 => 2 x 16
1
= 32.000 B x 160 = 11.000
C x 16-1 = .075
4 x 16-2 = .015 43.090
10
Remember that
A = Ten
B = Eleven
C = Twelve
D = Thirteen
E = Fourteen
HEXADECIMAL TO DECIMAL
• Convert 758.D116 into decimal equivalent.
758.D1
16 = 1880.816410
758.D1 => 7 x 162 = 1792 5 x 161 = 80
8 x 160 = 8 D x 16-1= 0.8125
1 x 16-2= 0.0039
DECIMAL TO HEXADECIMAL
• Convert 0.310 into hexadecimal
• Because C is the repeating value therefore, as a convention, we shall take it once only.
Result Fractional Part Integral Part
16 x 0.3 16 x 0.8 16 x 0.8
4.8 12.8 12.8
8 8 8
4
12 = C 12 = C
0.3
EXAMPLE
• Convert 185.310 into hexadecimal.
0.3
10 = 0.4C16
185
10 = B916
185.3
10 = B9.4C16
Result Fractional Part Integral Part
16 x 0.3 16 x 0.8 16 x 0.8
4.8 12.8 12.8 8 8 8 4
DECIMAL TO BINARY
• Convert 0.5610 into binary. Give answer up to
6 decimals.
Result Fractional Part Integral Part
2 x 0.56 2 x 0.12 2 x 0.24 2 x 0.48 2 x 0.96 2 x 0.92 2 x 0.84 2 x 0.68
1.12 0.24 0.48 0.96 1.92 1.84 1.68 1.36 12 24 48 96 92 84 68 36 1 0 0 0 1 1 1 1 0.56
EXAMPLE
• Convert 56.2510 into binary.
2 56
28 0 2
14 0 2
7 0 2
3 12 1 12
0 1
Result Fractional Part
Integral Part
2 x 0.25 2 x 0.50
0.5 1.0 5 0 0 1 56
10 = 01110002
0.25
10 = 0.012
56.25
HEXADECIMAL TO BINARY
• Convert A1.0316 into Binary.
A = 1010
1 = 0001
0 = 0000
3 = 0011
A1.03
16 = 10100001.000000112
Remember that
A = Ten
B = Eleven
C = Twelve
D = Thirteen
E = Fourteen
BINARY TO HEXADECIMAL
• Convert 101100.12 into hexadecimal.
• Solution:
• First divide your number into groups of 4 bits starting from the right.
0010 1100 . 1000
2 C 8
101100.1
OCTAL − HEXADECIMAL
• Convert to Binary as an intermediate step
Example:
( 0 1 0 1 1 0 . 0 1 0 )2
( 1 6 . 4 )16
Assume Zeros
Works both ways (Octal to Hex & Hex to Octal)
( 2 6 . 2 )8
EXERICSE – CONVERT...
Don’t use a calculator!
Decimal Binary Octal
Hexa-decimal
29.8
101.1101
3.07
C.82
Exercise – Convert …
Decimal Binary Octal
Hexa-decimal
29.8 11101.110011… 35.63… 1D.CC…
5.8125 101.1101 5.64 5.D
3.109375 11.000111 3.07 3.1C 12.5078125 1100.10000010 14.404 C.82