© 2003-2008 byu 02 numbers page 1 ecen 224 binary number systems and codes

21
© 2003-2008 BYU 02 NUMBERS Page 1 ECEn 224 Binary Number Systems and Codes

Upload: christian-fisher

Post on 23-Dec-2015

221 views

Category:

Documents


1 download

TRANSCRIPT

© 2003-2008BYU

02 NUMBERS Page 1

ECEn 224

Binary Number Systemsand

Codes

© 2003-2008BYU

02 NUMBERS Page 2

ECEn 224

Positional Numbers

• What does 5132.13 really mean?• Depends on the number base!• Assuming base 10:

5132.1310 = 5x103 + 1x102 + 3x101 + 2x100 + 1x10-1 + 3x10-2

• Assuming base 6:5132.136 = 5x63 + 1x62 + 3x61 + 2x60 + 1x6-1 + 3x6-2

• We often use a subscript to indicate the base.

© 2003-2008BYU

02 NUMBERS Page 3

ECEn 224

Positional Number Examples

527.4610 = (5 x 102) + (2 x 101) + (7 x 100) + (4 x 10-1) + (6 x 10-2)

527.468 = (5 x 82) + (2 x 81) + (7 x 80) + (4 x 8-1) + (6 x 8-2)

527.465 = illegal why?

1011.112 = (1 x 23) + (0 x 22) + (1 x 21) + (1 x 20) + (1 x 2-1) + (1 x 2-2)

This works for binary as well…

© 2003-2008BYU

02 NUMBERS Page 4

ECEn 224

Conversion from Binary

Convert 101011.112 to base 10:

101011.112 = 1x25 + 0x24 + 1x23 + 0x22 + 1x21 + 1x20 + 1x2-1 + 1x2-

2

= 32 + 0 + 8 + 0 + 2 + 1 + ½ + ¼

= 43.7510

© 2003-2008BYU

02 NUMBERS Page 5

ECEn 224

Convert 11410 to binary:

114

50

18

2

- 64 1x26

- 32 1x25

- 16 1x24

2- 0 0x23

2- 0 0x22

0- 2 1x21

- 0 0x20

0

11410 = 11100102

Rea

d t

his

way

This method also works for fractional numbers.

© 2003-2008BYU

02 NUMBERS Page 6

ECEn 224

114

An Alternate Method

2

2

2

2

2

2

2

57 R 0

28 R 1

14 R 0

7 R 0

3 R 1

1 R 1

0 R 1

11410 = 11100102

Rea

d t

his

way

© 2003-2008BYU

02 NUMBERS Page 7

ECEn 224

Converting fractions from base 10 to binary:Convert 0.710 to binary

0.7x 2

(1).4

x 2

(0).8x 2

(1).6x 2

(1).2x 2

(0).4x 2

(0).8

process starts repeating here

0.710 = 0.1 0110 0110 …2R

ead

th

is w

ay

© 2003-2008BYU

02 NUMBERS Page 8

ECEn 224

Convert 114.710 to binary:

We could use the first technique.

114.7- 64 1x26

50.7- 32 1x25

18.7- 16 1x24

2.7 - 0 0x23

2.7 - 0 0x22

2.7 - 2 1x21

0.7 - 0 0x20

0.7 - 0.5 1x2-1

0.2 - 0.0 0x2-2

0.2 - 0.125 1x2-3

0.075 …

Rea

d t

his

way

11210 = 1110010.10...2

© 2003-2008BYU

02 NUMBERS Page 9

ECEn 224

Convert 114.710 to binary:

Or we could combine the second and third techniques.

11210 = 1110010.10110...2

1142

2

2

2

2

2

2

57 R 0

28 R 1

14 R 0

7 R 0

3 R 1

1 R 1

0 R 1

0.7x 2

(1).4

x 2

(0).8x 2

(1).6x 2

(1).2x 2

(0).4x 2

(0).8

Rea

d t

his

way

© 2003-2008BYU

02 NUMBERS Page 10

ECEn 224

Hexadecimal

• Commonly used for binary data– 1 hex digit 4 binary digits (bits)

• Need more digits than just 0-9– Use 0-9, A-F

• A-F are for 10-15

FA216 = 15x162 + 10x161 + 2x160

FA216 = 1111 1010 0010

Each group of 4 bits 1 hex digit

© 2003-2008BYU

02 NUMBERS Page 11

ECEn 224

Other Notations For Binary and Hex

• Binary– 101102

– 10110b– 0b10110

• Hexadecimal– 57316

– 0x573– 573h– 16#573

© 2003-2008BYU

02 NUMBERS Page 12

ECEn 224

Other Codes

BCDASCIIGray

© 2003-2008BYU

02 NUMBERS Page 13

ECEn 224

Binary Coded Decimal(BCD)

DecimalDigit BCD

0 0 0 0 01 0 0 0 12 0 0 1 03 0 0 1 14 0 1 0 05 0 1 0 16 0 1 1 07 0 1 1 18 1 0 0 09 1 0 0 1

Convert 249610 to BCD Code

2 4 9 6

0 0 1 0 0 1 0 0 1 0 0 1 0 1 1 0

Note this is very different from convertingto binary which yields:

1 0 0 1 1 1 0 0 0 0 0 02

© 2003-2008BYU

02 NUMBERS Page 14

ECEn 224

Binary Coded Decimal(BCD)

• Why use BCD?• In some applications it may be easier to

work with• Financial institutions must be able to

represent base 10 fractions (e.g., 1/10)– 0.110 = 0.00110011001100…2

– Using BCD ensures that numeric results are identical to base 10 results

© 2003-2008BYU

02 NUMBERS Page 15

ECEn 224

Binary Codes ASCII Code

• ASCII American Standard Code for Information Interchange

• ASCII is a 7-bit code used to represent letters, symbols, and terminal codes

• There are also Extended ASCII codes, represented by 8-bit numbers

• Terminal codes include such things as:Tab (TAB)Line feed (LF)Carriage return (CR)Backspace (BS)Escape (ESC)And many more!

© 2003-2008BYU

02 NUMBERS Page 16

ECEn 224

Binary Codes ASCII Code

© 2003-2008BYU

02 NUMBERS Page 17

ECEn 224

Binary Codes Extended ASCII Code

© 2003-2008BYU

02 NUMBERS Page 18

ECEn 224

Binary Codes ASCII Code (partial)

Character

c 1 1 0 0 0 1 1d 1 1 0 0 1 0 0e 1 1 0 0 1 0 1f 1 1 0 0 1 1 0g 1 1 0 0 1 1 1h 1 1 0 1 0 0 0I 1 1 0 1 0 0 1j 1 1 0 1 0 1 0k 1 1 0 1 0 1 1l 1 1 0 1 1 0 0m 1 1 0 1 1 0 1n 1 1 0 1 1 1 0o 1 1 0 1 1 1 1p 1 1 1 0 0 0 0q 1 1 1 0 0 0 1

ASCII Code Convert “help” to ASCII

h e l p

1101000 1100101 1101100 1111000

0x68 0x65 0x6C 0x70

© 2003-2008BYU

02 NUMBERS Page 19

ECEn 224

Binary Codes Gray Code

GrayNumber Code

0 0 0 0 01 0 0 0 12 0 0 1 13 0 0 1 04 0 1 1 05 0 1 1 16 0 1 0 17 0 1 0 08 1 1 0 0 9 1 1 0 110 1 1 1 111 1 1 1 012 1 0 1 013 1 0 1 114 1 0 0 115 1 0 0 0

• Only one bit changes with each number increment

• Not a weighted code

• Useful for interfacing to some physical systems

© 2003-2008BYU

02 NUMBERS Page 20

ECEn 224

Gray Codes are Not Unique

GrayNumber Code

0 0001 0102 1103 1114 0115 0016 1017 100

GrayNumber Code

0 0001 0012 0113 0104 1105 1116 1017 100

© 2003-2008BYU

02 NUMBERS Page 21

ECEn 224

Codes - Summary

• Bits are bits…– Modern digital devices represent everything as

collections of bits– A computer is one such digital device

• You can encode anything with sufficient 1’s and 0’s– Text (ASCII)– Computer programs (C code, assembly code,

machine code)– Sound (.wav, .mp3, …)– Pictures (.jpg, .gif, .tiff)