1 IT 231, CMPE 331 Digital Logic Design Week 2 Number systems and arithmetic.

Post on 03-Jan-2016

213 views 0 download

Tags:

Transcript of 1 IT 231, CMPE 331 Digital Logic Design Week 2 Number systems and arithmetic.

1

IT 231, CMPE 331Digital Logic Design

Week 2

Number systems and arithmetic

2

Number systems and arithmetic

Counting in binary Binary ↔ decimal Hexadecimal, octal and BCD Binary arithmetic Signed numbers

3

Decimal numbers Position of each digit in a weighted number system is

assigned a weight based on the base or radix of the system Radix of decimal numbers is ten, because only ten symbols

(0,1,2,…,9) are used to represent any number

… 102 101 100 . 10−1 10−2 …1 7 3 . 6 2

(1×102) + (7×101) + (3×100) + (6×10-1)+(2×10-2)100 + 70 + 3 + 0.6 + 0.02

base = 10

weights

4

Base-N numbers

Decimal numbers are just a special case of more general base-N numbers– digits take values from 0,1,…,N−1– weights have values: … N3 N2 N1 N0 . N-1 N-2 …

In this course, we are interested in:N=2 binary (Base-2) 0,1N=8 octal (Base-8) 0,1,…,7N=10 decimal 0,1,…,9N=16 hexadecimal (“hex”) ???

5

Counting

Decimal0,1,2,3,4,5,6,7,8,9, 10,11,12,13,…,98,99, 100,101,…

BinaryOnly have two digits, called 0 and 1. Now the counting

sequence becomes: 0 1 10 11 100 101 110 111 1000 1001 ...

6

Counting in binary 0 1 10 11 100 101 110 111

In-class exercise:Fill in the next eight rows

7

Counting in binary 0 0 0 0 0 1 0 0 0 1 2 0 0 1 0 3 0 0 1 1 4 0 1 0 0 5 0 1 0 1 6 0 1 1 0 7 0 1 1 1 8 1 0 0 0 9 1 0 0 110 1 0 1 011 1 0 1 112 1 1 0 013 1 1 0 114 1 1 1 015 1 1 1 1

Decimal Number

Binary Number

N bits

We meet the binary counting sequence many times in this course

You’ll need to remember how to count in binary

Decimal number equal to binary 111…1?

2N - 1

Decimal number equal to binary 111…1?

8

Number systems and arithmetic

Counting in binary Binary ↔ decimal Hexadecimal, octal and BCD Binary arithmetic Signed numbers

9

1. Write the decimal weight of each column

2. Place 1’s in the columns that sum to the decimal number

Decimal → binary

Example: Convert decimal 49 to binary

26 25 24 23

22 21 20

64 32 16 8 4 2 1 0 1 1 0 0 0 1

Decimal 49 = binary 110001 Write: 4910 = 1100012

10

… 32 16 8 4 2 1 . ½ ¼ …1 0 0 1 0 1 . 0 1

In binary, weights are powers of 2

Binary → decimal

… 25 24 23

22 21 20 . 2−1 2−2 …

32 +4 +1 +¼ = 37.2510

Example: Convert binary 100101.01 to decimal

11

Convert 10111101.0112 to decimal

Convert 1910 and 9910 to binary

Convert 75.510 and 119.25 10 to binary

12

Number systems and arithmetic

Counting in binary Binary ↔ decimal Hexadecimal, octal and BCD Binary arithmetic Signed numbers

13

Octal Base-8 weighted number system 8 digits: 0,…,7

Digits 8 and 9 not allowed in octal! Weights are powers of 8

83 82 81 80

512 64 8 1Example: Express 37028 in decimal

3×512 + 7×64 + 0×8 + 2×1 = 198610

512 64 8 13 7 0 2

0 1 2 3 4 5 6 7 8 9 10 11 12 13 1415

0 1 2 3 4 5 6 7 10 1112 13 14 15 16 17

0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111

Decimal Octal Binary

14

Hexadecimal 0 1 2 3 4 5 6 7 8 9 10 11 12 13 1415

0 1 2 3 4 5 6 7 8 9 A B C D E F

0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111

Decimal Hexadecimal Binary

Base-16 weighted number system 16 digits: 0,…,9 plus characters A,B,C,D,E,F Weights are powers of 16 → compact way

of writing very large numbers

1×4096 + 10×256 + 2×16 +15×1 = 670310

163 162 161 160

4096 256 16 1Example: Express 1A2F16 in decimal

4096 256 16 11 A 2 F

Hexadecimal is much more commonly used

than octal

15

Convert 43910 to octal

Convert 65010 to hexadecimal

16

Binary → hexadecimal Convert a binary number to hexadecimal by: breaking binary number into 4-bit groups, starting at right-

most bit Replacing each group with equivalent hexadecimal digit

Example: Express 1111110001011010012 in hexadecimal

11 1111 0001 0110 10010011 1111 0001 0110 1001

3 F 1 6 91111110001011010012 = 3F16916

Break into 4-bit groups… adding leading zeros if necessary

Replace each group with hexadecimal digit

17

Binary → octal

Same way as converting binary to hexadecimal except break binary number into 3-bit groups

Example: Express 1111110001011010012 in octal-base

18

0 1 2 3 4 5 6 7 8 9 10 11 12 13 1415

0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111

Decimal Binary BCD

0001 0001 0001 0001 0001 0001

0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 0000 0001 0010 0011 0100 0101

Binary-coded decimal (BCD) In BCD, each decimal digit is

expressed with it’s 4-bit binary code

1010,1011,1100,1101,1110, 1111 are invalid BCD codes (why?)

BCD not as efficient as straight binary, but used widely for applications involving limited processing, eg. keypad inputs & clock displays

Example: Express 246910 in BCD

2 4 6 90010 0100 0110

1001

19

Number systems and arithmetic

Counting in binary Binary ↔ decimal Hexadecimal, octal and BCD Binary arithmetic Signed numbers

20

Adding two bitsFour possible cases:

0 0 1 1 0 + 1 + 0 + 1 + 0 0 0 1 0 1 1 0

0 + 0 = 0 sum=0, carry=0 0 + 0 = 00 + 1 = 1 sum=1, carry=0 0 + 1 = 11 + 0 = 1 sum=1, carry=0 1 + 0 = 11 + 1 = 10 sum=0, carry=1 1 + 1 = 2

augend addend +carry sum

Decimal

21

Binary addition

Example: Add the binary numbers 011 and 001 and show the equivalent decimal addition

22

Binary addition with carry in

1 + 0 + 0 = 01 sum=1, carry=0 1+0+0 = 11 + 0 + 1 = 10 sum=0, carry=1 1+0+1 = 2 1 + 1 + 1 = 11 sum=1, carry=1 1+1+1 = 3

Decimal

When a carry is generated in one column:

need to add third bit (1) in the next column means sum of one bit from each number, plus

the carry bit

23

Example: Add the binary numbers 00111 and 10101. Show the equivalent decimal addition

00111 710101 21

0

1

0

1

1

1

1

0

1 28

Binary addition

24

Number systems and arithmetic

Counting in binary Binary ↔ decimal Hexadecimal, octal and BCD Binary arithmetic Signed numbers

25

Signed numbersSo far we have considered only addition of positive numbers. What about negative numbers?

Signed numbers have both sign and magnitude information

Three ways of representing signed binary numbers:

1. Sign-magnitude form2. 1’s complement form3. 2’s complement form

All three methods use the sign bit (left-most bit) to tell whether the number is positive or negative:

– Sign bit = 0 : positive number– Sign bit = 1 : negative number

2’s complement method is by far the most widely used method in applications

26

Signed numbers in sign-magnitude form

To represent a signed binary number in sign-magnitude form:– left-most bit is the sign bit– remaining bits represent the magnitude

Example: Express the decimal number −39 as an 8-bit number in sign-magnitude form

Write 8-bit number for +39: 00100111 (39=32+4+2+1)

Change sign bit to a 1 to represent −39: 10100111

27

1’s complement of a binary number: change all 1s to 0s, and all 0s to 1s

1’s complement

Example: Find the 1’s complement of 10110010

10110010

↓↓↓↓↓↓↓↓

01001101

28

2’s complement

To get the 2’s complement of a binary number: add 1 to the 1’s complement

Example: Find the 2’s complement of 10110010

10110010 01001101 1’s complement

1 + add 1

01001110 2’s complement

29

2’s complement — shortcut method1. Start at the right-most bit and write the bits as

they are, till the first 1

2. Take the 1’s complement of the remaining bits

Example: Find the 2’s complement of 10110010 using the shortcut method

10110010 binary number

01001110 2’s complement

1’s complement of original bits These bits stay the same

30

2’s complement

Find the 2’s complement of 10010000 using both methods described above

31

Signed numbers in 1’s complement form

To represent a signed binary number in 1’s complement form:– Positive numbers:

represented as “normal” binary numbers, i.e. same as positive sign-magnitude

– Negative numbers: represented as 1’s complement of corresponding positive number

Example: Represent −2510 in 8-bit 1’s complement form

+2510 = 000110012 (since 25 = 16+8+1)

1’s complement of 00011001 is 11100110

32

Signed numbers in 2’s complement form

To represent a signed binary number in 2’s complement form:– Positive numbers:

represented as “normal” binary numbers, i.e. same as positive sign-magnitude

– Negative numbers: represented as 2’s complement of corresponding positive number

Example: Represent −2510 in 8-bit 2’s complement form

+2510 = 000110012

2’s complement of 00011001 is 11100111

33

Express +6810 as a binary number in 8-bit 2’s complement form

Express −4110 as a binary number in 8-bit 2’s complement form

34

To add two signed numbers in 2’s complement form:

Perform the addition Discard any final carry out of MSB (=sign bit) Result is in signed 2’s complement form Result will be correct provided range is not exceeded Three examples:

00011110 = +30 00001111 = +1500101101 = +45

00001110 = +14 11101111 = -1711111101 = -3

11111111 = -1 11111000 = -811110111 = -91

Discard carry out of sign bit

Arithmetic with signed binary numbers: addition

35

Overflow occurs when result of addition is out of range

number of bits required to represent sum exceeds number of bits in the two numbers added

occurs if (+A) + (+B) = −C or (−A) + (−B) = +C

01000000 = +64 01000001 = +6510000001 = −127

10000001 = −127 10000001 = −127

100000010 = +2

Examples:

Answers are both incorrect as sign bit of result disagrees with sign of augend and addend

Discard carry

Overflow condition

36

Arithmetic with signed binary numbers: subtraction

To subtract two signed numbers in 2’s complement form:

Take 2’s complement of subtrahend, then add the minuend A − B = A + (−B) Digital computers can use the same circuitry to add and

subtract → saving in hardware Remember: 2’s complement of subtrahend B is the

negation of B Discard any final carry out of MSB. The result is in signed form.

37

00001111 = +151

Discard carry

Take 2’s complement of subtrahend and add:00011110 = +3011110001 = -15

Examples: same numbers on slide 14, but subtract rather than add:

0001111000001111-

0000111011101111

11111111 11111000- -

00011111 = +31

00001110 = +1400010001 = +17

00000111 = +71

Discard carry

11111111 = -100001000 = +8

(+30) –(+15)

(+14) –(-17)

(-1) –(-8)

Arithmetic with signed binary numbers: subtraction