1
CS1010 Introduction to Computing
Lecture 08
Review – multiplying powers
•
For common bases,
add powers
.
2
6
2
10= 2
16= 65,536
or…
2
6
2
10= 64
2
10= 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
2and
00110010
2•
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
1
≥ (2)
10Multiplication (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
REPRESENTATION OF NEGATIVE
NUMBERS
•
There are many methods of representing
signed
number in
binary
:
–
Sign-Magnitude Method
1’s COMPLEMENT METHOD
•
1’s complement
of a
binary
number can be
directly 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.
Original number
01100110
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
-54
10in
1’s complement form
using 8
bits.
2 54
27 0 2
13 1 2
6 1 2
3 02 1 12 0 1
5410 = 01101102
5410 = 001101102 (in 8 bits) So -5410 in 1’s complement form:
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
-54
10in
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
5410 = 01101102
5410 = 001101102 (in 8 bits) Take 1’s complement of
00110110
= 11001001
+ 1
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.
3810 = 001001102
2910 = 000111012
Represent negative numbers in 1’s complement
-2910 = 111000102
Add the number & its 1’s complement
001001102
+ 111000102
Carry: 1 000010002
Add End Carry:+ 1
EXAMPLE
•
Calculate
– 54 – 30
using 8-bit
1’s complement
method.
•
Solution:
(– 54) + (– 30)
5410 = 001101102
3010 = 000111102
Represent negative numbers in 1’s complement -5410 = 110010012
Add numbers in its 1’s complement form
110010012
+ 111000012
Carry: 1 101010102
Add End Carry:+ 1 101010112
Convert the 1’s complement result into decimal. As MSB is 1 so it’s a negative number
Exercises
•
Perform following calculations using
1
stcomplement 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)
3810 = 001001102
Represent negative numbers in 2’s complement
Take 1’s complement of 29 =
00011101 and then add 1
-2910 = 111000112 (2’s complement)
Add 2’s complement representation and ignore the end carry.
001001102
+ 111000112
Carry: 1 000010012
Ignore Carry, the answer is
EXAMPLE
•
Calculate
– 54 – 30
using 8-bit
2’s complement
method.
•
Solution:
(– 54) + (– 30)
5410 = 001101102
3010 = 000111102
Represent negative numbers in 2’s complement
-5410 = 110010102
Add 2’s complement representation
110010102
+ 111000102
Carry: 1 101011002
Convert the 2’s complement result into decimal. As MSB is 1 so it’s a negative number
101011002 = 01010011 = -84
Ignore Carry, the answer is
Exercises
•
Perform following calculations using
2
ndcomplement method.
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.54
8127.548 => 1 x 82 = 64.000 2 x 81 = 16.000
7 x 80 = 7.000 5 x 8-1 = 0.625 4 x 8-2 = 0.0625
EXAMPLE
•
Convert
630.4
8into decimal equivalent.
630.4
8= 408.5
10630.4 => 6 x 82 = 384 3 x 81 = 24
0 x 80 = 0 4 x 8-1 = 0.5
DECIMAL TO OCTAL
•
185.3
108 185 23 1 8
2 78 0 2
18510 = 02718
0.310 = 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
HEXADECIMAL TO DECIMAL
•
2B.C4
162B.C416 => 2 x 161 = 32.000 B x 160 = 11.000
C x 16-1 = .075
4 x 16-2 = .015 43.09010
Remember that
A = Ten
B = Eleven
C = Twelve
D = Thirteen
E = Fourteen
HEXADECIMAL TO DECIMAL
•
Convert
758.D1
16into decimal equivalent.
758.D1
16= 1880.8164
10758.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.3
10into 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
EXAMPLE
•
Convert
185.3
10into hexadecimal.
0.310 = 0.4C16 18510 = B916
185.310 = 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.56
10into 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
EXAMPLE
•
Convert
56.25
10into binary.
2 56
28 0 2
14 0 2
7 0 2
3 1 2
1 12 0 1
Result Fractional Part Integral Part
2 x 0.25
2 x 0.50 0.51.0 50 01
5610 = 01110002 0.2510 = 0.012
HEXADECIMAL TO BINARY
•
Convert
A1.03
16into Binary.
A = 1010
1 = 0001
0 = 0000
3 = 0011
A1.0316 = 10100001.000000112
Remember that
A = Ten
B = Eleven
C = Twelve
D = Thirteen
E = Fourteen
BINARY TO HEXADECIMAL
•
Convert
101100.1
2into hexadecimal.
•
Solution:
•
First divide your number into groups of 4 bits
starting from the right.
0010 1100 . 1000
2 C 8
OCTAL − HEXADECIMAL
•
Convert to Binary as an intermediate step
Example:
(
0
1 0 1 1 0 . 0 1 0
)
2(
1 6 . 4
)
16Assume Zeros
Works both ways (Octal to Hex & Hex to Octal)
(
2 6 . 2
)
8EXERICSE – 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