binary numbers 1. outcome familiar with the binary system binary to decimal and decimal to binary...

52
Binary Numbers 1

Upload: amelia-jensen

Post on 28-Mar-2015

269 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

Binary Numbers

1

Page 2: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

Outcome

• Familiar with the binary system• Binary to Decimal and decimal to binary• Arithmetic and logic operation in binary

system• Logic gates• Half Adder and Full Adder• Hexadecimal system

Page 3: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

Reading

• Goldsmiths Study guide: – mathematics for computing

• http://www.math.grin.edu/~rebelsky/Courses/152/97F/Readings/student-binary

• http://en.wikipedia.org/wiki/Binary_numeral_system

• http://www.cut-the-knot.org/do_you_know/BinaryHistory.shtml

• http://www.binarymath.info/

Page 4: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

The Decimal Number System (con’t)

4

• The decimal number system is also known as base 10. The values of the positions are calculated by taking 10 to some power.

• Why is the base 10 for decimal numbers?o Because we use 10 digits, the digits 0 through 9.

Page 5: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

The Binary Number System – base 2

5

• The decimal number system is a positional number system with a base 10.

• Example: 5623

• 5623 = 5000 + 600 + 20 + 3 = 5 x 103 + 6 x102 + 2 x 101 + 3 x 100

5000 600 20 35 x 103 6 x102 2 x 101 3 x 100

Page 6: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

The Binary Number System

6

• The binary number system is also known as base 2. The values of the positions are calculated by taking 2 to some power.

• Why is the base 2 for binary numbers?o Because we use 2 digits, the digits 0 and 1.

Page 7: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

The Decimal Number System - base 10

7

• The decimal number system is a positional number system with a base 10.

• Example: 1011

• 10112 = 1000 + 000 + 10 + 1 = 1 x 23 + 0 x22 + 1 x 21 + 1 x 20 = 1110

1000 000 10 11 x 23 0x22 1 x 21 1x 20

Page 8: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

Why Bits (Binary Digits)?• Computers are built using digital circuits

– Inputs and outputs can have only two values– True (high voltage) or false (low voltage)– Represented as 1 and 0

• Can represent many kinds of information– Boolean (true or false)– Numbers (23, 79, …)– Characters (‘a’, ‘z’, …) ASCII, UNICODE– Pixels– Sound

• Can manipulate in many ways– Read and write– Logical operations– Arithmetic– …

8

Page 9: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

Base 10 and Base 2• Base 10

– Each digit represents a power of 10– 5417310 = 5 x 104 + 4 x 103 + 1 x 102 + 7 x 101 + 3 x 100

• Base 2

– Each bit represents a power of 2– 101012= 1 x 24 + 0 x 23 + 1 x 22 + 0 x 21 + 1 x 20 = 2110

9

Page 10: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

Converting from Binary to Decimal

10

1 0 0 1 1 0 1 1 X 20 = 1 26 25 24 23 22 21 20 0 X 21 = 0 1 X 22 = 4 20 = 1 1 X 23 = 8 21 = 2 0 X 24 = 0 22 = 4 0 X 25 = 0 23 = 8 1 X 26 = 64 24 = 16 7710

25 = 32 26 = 64

Page 11: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

Converting from Binary to Decimal (con’t)

Practice conversions:

Binary Decimal

110010 100111 1010101

11

Page 12: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

Converting From Decimal to Binary (con’t)

12

• Make a list of the binary place values up to the number being converted.• Perform successive divisions by 2, placing the remainder of 0 or 1 in each of the

positions from right to left.• Continue until the quotient is zero.• Example: 4210

25 24 23 22 21 20

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

42/2 = 21 and R = 021/2 = 10 and R = 110/2 = 5 and R = 05/2 = 2 and R = 12/2 = 1 and R = 01/2 = 0 and R = 1

4210 = 1010102

Page 13: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

Example 1710

We repeatedly divide the decimal number by 2 and keep remainders

– 17/2 = 8 and R = 1– 8/2 = 4 and R = 0– 4/2 = 2 and R = 0– 2/2 = 1 and R = 0– 1/2 = 0 and R = 1

The binary number representing 17 is 10001

Page 14: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

Converting From Decimal to Binary (con’t)

Practice conversions:

Decimal Binary

59 82 175

14

Page 15: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

15

Fractional Numbers

Decimal456.7810 = 4 x 102 + 5 x 101 + 6 x 100 + 7 x 10-1+8 x 10-2

Binary1011.112 = 1 x 23 + 0 x 22 + 1 x 21 + 1 x 20 + 1 x 2-1 + 1 x 2-2

= 8 + 0 + 2 + 1 + 1/2 + ¼ = 11 + 0.5 + 0.25 = 11.7510

Page 16: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

16

Binary Fractional to decimal nNumbers (cont)

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

= 8 + 0 + 2 + 1 + 1/2 + ¼= 11 + 0.5 + 0.25 = 11.7510

• Example 2:

• 111.112 = 1 x 22 + 1 x 21 + 1 x 20 + 1 x 2-1 + 1 x 2-2

= 4 + 2 + 1 + 1/2 + ¼ = 7.7510

Example3: 11.0112 = 1 x 21 + 1 x 20 + 0x 2-1 + 1 x 2-2 + 1 x 2-3

= 2 +1 +0 + ¼ +1/8 = 3.37510

Page 17: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

17

Fractional numbers

Examples: 7.7510 = (?)2

1. Conversion of the integer part: same as before – repeated division by 27 / 2 = 3 (Q), 1 (R) 3 / 2 = 1 (Q), 1 (R) 1 / 2 = 0 (Q), 1 (R) 710 = 1112

2. Conversion of the fractional part: perform a repeated multiplication by 2 and extract the integer part of the result0.75 x 2 =1.50 extract 10.5 x 2 = 1.0 extract 1 0.7510 = 0.112

0.0 stop

Combine the results from integer and fractional part, 7.7510 = 111.112

How about choose some of

Examples: try 5.625

write in the same order

4 2 1 1/2 1/4 1/8=0.5 =0.25 =0.125

Page 18: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

18

Fractional Numbers (cont.)

Exercise 3: Convert (0.8125)10 to its binary form

Solution:

0.8125 x 2 = 1.625 extract 1

0.625 x 2 = 1.25 extract 1

0.25 x 2 = 0.5 extract 0

0.5 x 2 = 1.0 extract 1

0.0 stop

(0.8125)10 = (0.1101)2

Page 19: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

19

Representing fraction with error

0.6 x 2 = 1.2 extract 1

0.2 x 2 = 0.4 extract 0

0.4 x 2 = 0.8 extract 0

0.8 x 2 = 1.6 extract 1

0.6 x 2 =

(0.6)10 = (0.1001 1001 1001 …)2

Example: Convert (0.6)10 to its binary form

Page 20: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

20

Fractional Numbers (cont.)

• Errors– One source of error in the computations is due to back and forth

conversions between decimal and binary formatsExample: (0.6)10 + (0.6)10 = 1.210

Since (0.6)10 = (0.1001 1001 1001 …)2

Lets assume a 8-bit representation: (0.6)10 = (0 .1001 1001)2 , therefore0.6 0.10011001

+ 0.6 + 0.100110011.00110010

Lets reconvert to decimal system: (1.00110010)b= 1 x 20 + 0 x 2-1 + 0 x 2-2 + 1 x 2-3 + 1 x 2-4 + 0 x 2-5 + 0 x 2-6 + 1 x 2-7 + 0 x 2-8

= 1 + 1/8 + 1/16 + 1/128 = 1.1953125

Error = 1.2 – 1.1953125 = 0.0046875

Page 21: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

Bits, Bytes, and Words

• A bit is a single binary digit (a 1 or 0).• A byte is 8 bits• A word is 32 bits or 4 bytes• Long word = 8 bytes = 64 bits• Quad word = 16 bytes = 128 bits• Programming languages use these standard number of

bits when organizing data storage and access.

21

Page 22: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

Adding Two Integers: Base 10

• From right to left, we add each pair of digits• We write the sum, and add the carry to the next column

22

1 98

+ 2 64

Sum

Carry

0 11

+ 0 01

Sum

Carry

2

1

6

1

4

0

0

1

0

1

1

0

Page 23: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

Example 10011110 1101111+ + 111 1101-------------------- -------------------= 101 0 0 101 = 1111100

Page 24: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

Binary subtraction

Page 25: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

Binary subtraction (Cont)

Page 26: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

Binary subtraction (Cont)

Page 27: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

Exercise

• 10010101 - 11011 =?• 10000001 - 111 = ?

Page 28: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

ELEC 2200 (Agrawal) 28Spring 2007, Jan. 17

Binary Multiplication

1 0 0 0 two = 8ten multiplicand

1 0 0 1 two = 9ten multiplier____________

1 0 0 0 0 0 0 0 partial products 0 0 0 0 1 0 0 0____________ 1 0 0 1 0 0 0two = 72ten

Page 29: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

ELEC 2200 (Agrawal) 29Spring 2007, Jan. 17

Binary Division

1 3 Quotient1 1 / 1 4 7 Divisor / Dividend 1 1 3 7 Partial remainder 3 3 4 Remainder

0 0 0 0 1 1 0 11 0 1 1 / 1 0 0 1 0 0 1 1 1 0 1 1 0 0 1 1 1 0 1 0 1 1 0 0 1 1 1 1 1 0 1 1 1 0 0

Page 30: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

30

Bitwise Operators: Shift Left/Right

• Shift left (<<): Multiply by powers of 2– Shift some # of bits to the left, filling the blanks with 0

• Shift right (>>): Divide by powers of 2– Shift some # of bits to the right

• For unsigned integer, fill in blanks with 0• What about signed integers? Varies across machines…

– Can vary from one machine to another!

0 0 1 1 0 1 0 053

1 1 0 1 0 0 0 053<<2

0 0 1 1 0 1 0 053

0 0 0 0 1 1 0 153>>2

Page 31: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

Boolean Algebra to Logic Gates

• Logic circuits are built from components called logic gates.

• The logic gates correspond to Boolean operations +, *, ’.

• Binary operations have two inputs, unary has one

OR+

AND*

NOT’

Page 32: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

ANDA

B

A*B

Logic Gate:

Series Circuit:

A B

A B A*B

0 0 0

0 1 0

1 0 0

1 1 1

Truth Table:A*B

Page 33: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

A

B

A+B

Logic Gate:

Parallel Circuit:

A

B

A B A+B

0 0 0

0 1 1

1 0 1

1 1 1

Truth Table:A+B

OR

Page 34: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

NOTALogic Gate:

(also called an inverter)

a A

0 1

1 0

Truth Table:

A’ or A

Page 35: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

n-input Gates• Because + and * are binary operations, they can be

cascaded together to OR or AND multiple inputs.

AB

C

ABC

A+B+C

A+B+C

AB

ABC

ABC

ABC

Page 36: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

NAND and NOR Gates• NAND and NOR gates can greatly simplify circuit diagrams. As we

will see, can you use these gates wherever you could use AND, OR, and NOT.

NAND

NOR

A B AB

0 0 1

0 1 1

1 0 1

1 1 0

A B AB

0 0 1

0 1 0

1 0 0

1 1 0

Page 37: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

XOR and XNOR Gates• XOR is used to choose between two mutually exclusive inputs.

Unlike OR, XOR is true only when one input or the other is true, not both.

XOR

XNOR

A B AB

0 0 0

0 1 1

1 0 1

1 1 0

A B A B

0 0 1

0 1 0

1 0 0

1 1 1

Page 38: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

Binary Sums and Carries

a b Sum a b Carry0 0 0 0 0 00 1 1 0 1 01 0 1 1 0 01 1 0 1 1 1

38

XOR AND

0100 0101

+ 0110 0111

1010 1100

69

103

172

Page 39: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

ELEC 2200 (Agrawal) 39Spring 2007, Jan. 17

Design Hardware Bit by Bit

• Adding two bits:a b half_sum

carry_out0 0 0 00 1 1 01 0 1 01 1 0 1

• Half-adder circuit

ab

half_sum

carry_out

XOR

AND

Page 40: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

Half Adder (1-bit)

A B S(um)

C(arry)

0 0 0 0

0 1 1 0

1 0 1 0

1 1 0 1

HalfAdder

A B

S

C

Page 41: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

Half Adder (1-bit)

AB C

BABABAS

A B S(um)

C(arry)

0 0 0 0

0 1 1 0

1 0 1 0

1 1 0 1

A

BSum

Carry

Page 42: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

Full Adder

Cin A B S(um)

Cout

0 0 0 0 0

0 0 1 1 0

0 1 0 1 0

0 1 1 0 1

1 0 0 1 0

1 0 1 0 1

1 1 0 0 1

1 1 1 1 1

FullAdder

A B

S

Cout

Carry In(Cin)

Page 43: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

Full Adder

B)Cin(AABCout BACinS

A

B

Cin

Cout

S

H.A. H.A.

Page 44: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

Full Adder

B)Cin(AABCout BACinS

Cout

S

HalfAdder

S

C

A

B

HalfAdder

S

C

A

BB

A

Cin

Page 45: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

4-bit Ripple Adder using Full Adder

FullAdder

A B

CinCout

S

S0

A0 B0

FullAdder

A B

CinCout

S

S1

A1 B1

FullAdder

A B

CinCout

S

S2

A2 B2

FullAdder

A B

CinCout

S

S3

A3 B3

Carry

A

BS

C

Half Adder

A

B

CinCout

SH.A. H.A.

Full Adder

Page 46: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

Working with Large Numbers

46

0 1 0 1 0 0 0 0 1 0 1 0 0 1 1 1 = ?

• Humans can’t work well with binary numbers; there are too many digits to deal with.

• Memory addresses and other data can be quite large. Therefore, we sometimes use the hexadecimal number system.

Page 47: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

The Hexadecimal Number System• The hexadecimal number system is also known as base 16. The values of

the positions are calculated by taking 16 to some power.• Why is the base 16 for hexadecimal numbers ?

– Because we use 16 symbols, the digits 0 and 1 and the letters A through F.

47

Page 48: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

The Hexadecimal Number System (con’t)

Binary Decimal Hexadecimal Binary Decimal Hexadecimal

0 0 0 1010 10 A 1 1 1 1011 11 B 10 2 2 1100 12 C 11 3 3 1101 13 D 100 4 4 1110 14 E 101 5 5 1111 15 F 110 6 6 111 7 7 1000 8 8 1001 9 9

48

Page 49: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

The Hexadecimal Number System (con’t)

• Example of a hexadecimal number and the values of the positions:

3 C 8 B 0 5 1 166 165 164 163 162 161 160

49

Page 50: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

Example of Equivalent Numbers

50

Binary: 1 0 1 0 0 0 0 1 0 1 0 0 1 1 12

Decimal: 2064710

Hexadecimal: 50A716

Notice how the number of digits gets smaller as the base increases.

Page 51: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

Summary

• Convert binary to decimal• Decimal to binary• Binary operation• Logic gates • Use of logic gates to perform binary operations

– Half adder– Full adder

• The need of Hexadecimal Hexadecimal

Page 52: Binary Numbers 1. Outcome Familiar with the binary system Binary to Decimal and decimal to binary Arithmetic and logic operation in binary system Logic

Next lecture (Data representation)

• Put this all together– negative and positive integer representation– unsigned notation– Signed notation– Excess notation– Tow’s complement notation

• Floating point representation– Single and double precision

• Character, colour and sound representation