1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: [email protected].

40
1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: [email protected]

Transcript of 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: [email protected].

Page 1: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

1

Tutorial: ITI1100

Dewan Tanvir AhmedSITE, UofO

Email: [email protected]

Page 2: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

2

Binary Numbers• Base (or radix)

– 2– example: 0110

• Number base conversion– example: 41 = 101001

• Complements– 1's complements ( 2n- 1 ) - N– 2's complements 2n - N– Subtraction = addition with the 2's complement

• Signed binary numbers» signed-magnitude, 10001001» signed 1's complement, 11110110» signed 2's complement. 11110111

Page 3: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

3

Binary Number SystemBase = 22 Digits: 0, 1

Examples:1001b = 1 * 23 + 0 * 22 + 0 * 21 + 1 * 20 = 8 + 1 = 9 

1010 1101b = 1 * 27 + 1 * 25 + 1 * 23 + 1 * 22 + 1 = 128 + 32 + 8 + 4 + 1 = 173

Note: it is common to put binary digits in groups of 4 to make it easier to read them.

Page 4: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

4

Ranges for Data Formats

No. of bits Binary BCD

1 0 – 1

2 0 – 3

3 0 – 7

4 0 – 15 0 – 9

7 0 – 127

8 0 – 255 0 – 99

16 0 - 65,535 0 – 9999

24 0 – 16,777,215 0 – 999999

Page 5: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

5

In General (binary)

No. of bitsBinary

Min Max

n 0 2n – 1

Page 6: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

6

Signed Integers• “unsigned integers” = positive values only

• Must also have a mechanism to represent “signed integers” (positive and negative values!)

-1010 = ?2

• Two common schemes: – sign-magnitude and – twos complement

Page 7: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

7

Sign-Magnitude• Extra bit on left to represent sign

– 0 = positive value– 1 = negative value

• 6-bit sign-magnitude representation of +5 and –5:

+5: 0 0 0 1 0 1

+ve 5

-5: 1 0 0 1 0 1

-ve 5

Page 8: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

8

Ranges (revisited)

No. of bits

Binary

Unsigned Sign-magnitude

Min Max Min Max

1 0 1

2 0 3 -1 1

3 0 7 -3 3

4 0 15 -7 7

5 0 31 -15 15

6 0 63 -31 31

Page 9: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

9

In General …

No. of bits

Binary

Unsigned Sign-magnitude

Min Max Min Max

n 0 2n - 1 -(2

n-1 - 1) 2n-1 - 1

Page 10: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

10

Difficulties with Sign-Magnitude

• Two representations of zero– Using 6-bit sign-magnitude…

• 0: 000000• 0: 100000

• Arithmetic is awkward!

Page 11: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

11

Complementary Representations

• 1’s complement• 2’s complement• 9’s complement• 10’s complement

Page 12: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

12

Complementary Notations

• What is the 3-digit 10’s complement of 207?– Answer:

• What is the 4-digit 10’s complement of 15?– Answer:

• 111 is a 10’s complement representation of what decimal value?– Answer:

Page 13: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

13

• What is the 3-digit 10’s complement of 207?– Answer: 793

• What is the 4-digit 10’s complement of 15?– Answer: 9985

• 111 is a 10’s complement representation of what decimal value?– Answer: 889

Exercises – Complementary Notations

Page 14: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

14

2’s Complement• Most common scheme of representing

negative numbers • natural arithmetic - no special rules!• Rule to represent a negative number in 2’s C

1. Decide upon the number of bits (n)2. Find the binary representation of the +ve value in n-bits3. Flip all the bits 4. Add 1

Page 15: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

15

2’s Complement Example

• Represent -5 in binary using 2’s complement notation

1. Decide on the number of bits

2. Find the binary representation of the +ve value in 6 bits

3. Flip all the bits

4. Add 1

6 (for example)

111010

111010+ 1 111011

-5

000101+5

Page 16: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

16

Sign Bit

• In 2’s complement notation, the MSB is the sign bit (as with sign-magnitude notation)– 0 = positive value– 1 = negative value

-5: 1 1 1 0 1 1

-ve

+5: 0 0 0 1 0 1

+ve 5 2’s complement

Page 17: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

17

“Complementary” Notation

• Conversions between positive and negative numbers are easy

• For binary (base 2)…

Page 18: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

18

Example+5

2’s C

-5

2’s C

+5

0 0 0 1 0 1

1 1 1 0 1 0+ 1

1 1 1 0 1 1

0 0 0 1 0 0+ 1

0 0 0 1 0 1

+ve -ve

2’s C

2’s C

Page 19: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

19

Range for 2’s Complement

• For example, 6-bit 2’s complement notation

-32 -31 ... -1 0 1 ... 31

000000111111 000001 011111100000 100001

Negative, sign bit = 1 Zero or positive, sign bit = 0

Page 20: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

20

Ranges

No. of bitsBinary

Unsigned Sign-magnitude 2’s complement

Min Max Min Max Min Max

1 0 1

2 0 3 -1 1 -2 1

3 0 7 -3 3 -4 3

4 0 15 -7 7 -8 7

5 0 31 -15 15 -16 15

6 0 63 -31 31 -32 31

Page 21: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

21

In General (revisited)

No. of bits

Binary

Unsigned Sign-magnitude 2’s complement

Min Max Min Max Min Max

n 0 2n - 1 -(2

n-1 - 1) 2n-1

-1 -2n-1

2n-1

- 1

Page 22: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

22

What is -6 plus +6?

• Zero, but let’s see

-6: 10000110+6: +00000110 10001100

Sign-magnitude

-6: 11111010+6: +00000110 00000000

2’s complement

Page 23: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

23

2’s Complement Subtraction

• Easy, no special rules• Subtract?? • Actually … addition!

A – B = A + (-B)

add 2’s complement of B

Page 24: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

24

What is 10 subtract 3?

• 7, but…• Let’s do it (we’ll use 6-bit values)

10 – 3 = 10 + (-3) = 7

001010+111101 000111

+3: 000011

-3: 111101

Page 25: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

25

What is 10 subtract -3?

• 13, but…• Let’s do it (we’ll use 6-bit values)

10 – (-3) = 10 + (-(-3)) = 13

-3: 111101

+3: 000011

001010+000011 001101

Page 26: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

26

M - N• M + 2’s complement of N

– M + (2n - N) = M - N + 2n

• If M N– Produce an carry, which is discarded

• If M < N– results in 2n - (N - M), which is the 2’s complement of (N-M)

Page 27: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

27

Overflow

• Carry out of the leading digit• If we add two positive numbers and we get a carry

into the sign bit we have a problem• If we add two negative numbers and we get a carry

into the sign bit we have a problem• If we add a positive and a negative number we won't

have a problem• Assume 4 bit numbers (+7 : -8)

Page 28: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

28

Unsigned

0123456789101112131415

Binary

0000000100100011010001010110011110001001101010111100110111101111

SignedMag

01234567

-0-1-2-3-4-5-6-7

1'sComp

01234567

-7-6-5-4-3-2-1-0

2'sComp

01234567

-8-7-6-5-4-3-2-1

N = 4 Number Represented

Page 29: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

29

Overflow

3 0011 4 0100 3 0011 4 0100 6 0110 8 1000

• If we add two positive numbers and we get a carry into the sign bit we have a problem

Page 30: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

30

Overflow

-5 1011 -4 1100

-3 1101 -5 1011

-8 11000 -9 10111

• If we add two negative numbers and we get a carry into the sign bit we have a problem

Page 31: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

31

Overflow

• If we add a positive and a negative number we won't have a problem

5 0101 -4 1100

-3 1101 5 0101

2 10010 1 10001

Page 32: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

32

Overflow

• If we add two positive numbers and we get a carry into the sign bit we have a problem

3 0011 4 0100

3 0011 4 0100

6 0110 8 1000carry in 0carry out 0

carry in 1carry out 0

Page 33: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

33

Overflow

• If we add two negative numbers and we get a carry into the sign bit we have a problem

-5 1011 -4 1100

-3 1101 -5 1011

-8 11000 -9 10111carry in 1carry out 1

carry in 0carry out 1

Page 34: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

34

Overflow

• If we add a positive and a negative number we won't have a problem

5 0101 -4 1100

-3 1101 5 0101

2 10010 1 10001

carry in 1carry out 1

carry in 1carry out 1

Page 35: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

35

Binary Codes• n-bit binary code

– 2n distinct combinations

• BCD – Binary Coded Decimal (4-bits)• 0 0000• 1 0001• … …• 9 1001

• BCD addition• Get the binary sum• If the sum > 9, add 6 to the sum• Obtain the correct BCD digit sum and a carry

Page 36: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

36

Binary Codes• ASCII code

– American Standard Code for Information Interchange– alphanumeric characters, printable characters (symbol),

control characters

• Error-detection code– one parity bit - an even numbered error is undetected– “A” 41:100|0001 - - >

– 0100|0001 (even), – 1100|0001 (odd)

Page 37: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

37

Binary Logic• Boolean algebra

• Binary variables: X, Y– two discrete values (true or false)

• Logical operations– AND, OR, NOT

• Truth tables

Page 38: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

38

Logic Gates• Logic circuits

– circuits = logical manipulation paths• Computations and controls

– combinations of logic circuits• Logic Gates

Page 39: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

39

Timing diagram

Page 40: 1 Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO Email: dahmed@site.uottawa.ca.

40

Thank You!