• No results found

Lec 08 Computer Arithmatic

N/A
N/A
Protected

Academic year: 2020

Share "Lec 08 Computer Arithmatic"

Copied!
48
0
0

Loading.... (view fulltext now)

Full text

(1)

1

CS1010 Introduction to Computing

Lecture 08

(2)

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

(3)

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

(4)

BINARY ADDITION

Two

n

-bit values

Add individual

bits

Propagate

carries

Example:

10101 21

+ 11001 + 25

101110

46

(5)

EXAMPLE

Add

01011101

2

and

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

(6)

EXAMPLE

Add

111101

and

10111

.

1

1

1

0 1

1

1

1

1

1 0

+

0

0

0

0 1

1

1

≥ (2)

10
(7)

Multiplication (1 of 3)

Decimal

(just for fun)

(8)

Multiplication (2 of 3)

Binary,

two 1-bit

values

A

B

A

B

0

0

0

0

1

0

1

0

0

(9)

Multiplication (3 of 3)

Binary,

two

n

-bit

values

As with decimal values

E.g.,

1110

x 1011

1110

1110

0000

1110

(10)

MULTIPLICATION

Decimal

35

x 105

175

000

35

(11)

EXAMPLE

Multiply the Binary numbers

1111

and

111

.

1111

x 111

1111

1111

1111

(12)

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

(13)

REPRESENTATION OF NEGATIVE

NUMBERS

There are many methods of representing

signed

number in

binary

:

Sign-Magnitude Method

(14)

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

(15)

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.

(16)

Example

Represent

-54

10

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

5410 = 01101102

5410 = 001101102 (in 8 bits) So -5410 in 1’s complement form:

(17)

2’s COMPLEMENT METHOD

Algorithm

First taking

1’s complement

.

(18)

Example

Take

2’s complement

of the binary number

01100110

directly.

Original number

01100110

1’s Complement

10011001

+1

(19)

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.

(20)

Example

Represent

-54

10

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

5410 = 01101102

5410 = 001101102 (in 8 bits) Take 1’s complement of

00110110

= 11001001

+ 1

(21)

BINARY ADDITION

Two 1-bit values

A

B

A + B

0

0

0

0

1

1

1

0

1

1

1

10

(22)

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.

(23)

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

(24)

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

(25)

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

(26)

Exercises

Perform following calculations using

1

st

complement method.

a) 0111000 + 1011100

b) 25 – 50

(27)

SUBTRACTING USING 2’s COMPLEMENT

METHOD

Example:

Calculate

38 – 29

using 8-bit

2’s complement

method.

Solution:

38 + (-29)

3810 = 001001102

(28)

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

(29)

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

(30)

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

(31)

Exercises

Perform following calculations using

2

nd

complement method.

(32)

FRACTIONS

Decimal to decimal

.

3.14 => 4 x 10-2 = 0.04 1 x 10-1 = 0.1

3 x 100 = 3

(33)

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

(34)

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.

(35)

OCTAL TO DECIMAL

127.54

8

127.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

(36)

EXAMPLE

Convert

630.4

8

into decimal equivalent.

630.4

8

= 408.5

10

630.4 => 6 x 82 = 384 3 x 81 = 24

0 x 80 = 0 4 x 8-1 = 0.5

(37)

DECIMAL TO OCTAL

185.3

10

8 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

(38)

HEXADECIMAL TO DECIMAL

2B.C4

16

2B.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

(39)

HEXADECIMAL TO DECIMAL

Convert

758.D1

16

into decimal equivalent.

758.D1

16

= 1880.8164

10

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

(40)

DECIMAL TO HEXADECIMAL

Convert

0.3

10

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

(41)

EXAMPLE

Convert

185.3

10

into 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

(42)

DECIMAL TO BINARY

Convert

0.56

10

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

(43)

EXAMPLE

Convert

56.25

10

into 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

(44)

HEXADECIMAL TO BINARY

Convert

A1.03

16

into 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

(45)

BINARY TO HEXADECIMAL

Convert

101100.1

2

into hexadecimal.

Solution:

First divide your number into groups of 4 bits

starting from the right.

0010 1100 . 1000

2 C 8

(46)

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
(47)

EXERICSE – CONVERT...

Don’t use a calculator!

Decimal

Binary

Octal

Hexa-decimal

29.8

101.1101

3.07

C.82

(48)

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

References

Related documents

Negative numbers are written as the 2’s complement of the corresponding positive number.. Large binary number can easily be converted to hexadecimal by grouping bits 4 at a time and

¤  Determine the sign and magnitude of an integer represented in two’s complement binary. ¤  Determine the two’s complement binary representation of a positive or

• Following are the two rules for addition and subtraction of n-bit signed numbers using the 2's complement representation system (Figure 1.6)..

triangle noteheads = knuckles used on guitar face for

• 1s Complement: A form of signed binary in which negative numbers are created by complementing all bits.. • 2s Complement: A form of signed binary in which the negative

The training losses and testing accuracies, averaged over 10 runs when both algorithms were initialized at the same starting point (the origin), are plotted in Figure 4. During the

Using two's complement to represent negative numbers allows only one representation of zero, and to have effective addition and subtraction while still having the most significant

Each not void variable is computed by the following relation: Variable = Function1(Input1) Operator Function2(Input2). A not void variable is a variable that does not call any