chapter 4: the building blocks: binary numbers, boolean...

81
Chapter 4: The Building Blocks: Binary Numbers, Boolean Logic, and Gates Invitation to Computer Science,

Upload: others

Post on 24-Feb-2021

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Chapter 4: The Building Blocks: Binary Numbers, Boolean Logic, and Gates

Invitation to Computer Science,

Page 2: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 2

Objectives

In this chapter, you will learn about:

The binary numbering system

Boolean logic and gates

Building computer circuits

Control circuits

Page 3: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 3

Introduction

Chapter 4 focuses on hardware design (also called logic design)

How to represent and store information inside a computer

How to use the principles of symbolic logic to design gates

How to use gates to construct circuits that perform operations such as adding and comparing numbers, and fetching instructions

Page 4: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 4

The Binary Numbering System

A computer’s internal storage techniques are different from the way people represent information in daily lives

Information inside a digital computer is stored as a collection of binary data

Bit = BInary digiT

Page 5: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 5

Binary numbering system Base-2 Built from ones and zeros Each position is a power of 2

1101 = 1 * 23 + 1 * 22 + 0 * 21 + 1 * 20

Decimal numbering system Base-10 Each position is a power of 10

3052 = 3 * 103 + 0 * 102 + 5 * 101 + 2 * 100

Binary Representation of Numbers

Page 6: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 6

Figure 4.2Binary-to-DecimalConversion Table

Page 7: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 7

Conversion from Base 10 to Base 2

To convert a number n from base 10 to base 2 you must:

1. Divide n by 2, then its quote by 2 and continue this way until the quote is 0

2. Collect all the remainders produced by the divisions performed in Step 1

3. Write the remainders in the opposite order they have been produced

Page 8: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 8

Conversion from Base 10 to Base 2 (cont.)

Example: What is 17810 in base 2?

17810= 1011 00102

178 089 144 022 011 15 12 01 10

2

1*27 + 1*25 + 1*24 +1*21 =

128 + 32 + 16 + 2 = 17810

quoteremainder

....

....

Page 9: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 9

Conversion from Base 10 to Base 2

To convert a number n from base 2 to base 10 you must:

1. Multiply each digit by 2p where p is the positionof the digit.

Of course the digits that are 0 will produce a product equal to zero, so you can limit you attention only to the digits equal to 1

2. Add all the products obtained in Step 1.

Page 10: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 10

Conversion from Base 2 to Base 10

Example: What is 10110112 in base 10?

101 10112 = = 1*26 + 1*24 + 1*23 + 1*21 +1*20 =

= 64 + 16 + 8 + 2 + 1 = = 9110

101 10112 = 9110

Page 11: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 11

Conversion from Base 10 to Base k

General Rule: To convert a number n from base 10 to

base k you must:

1. Divide n by k, then its quote by k and continue this way until the quote is 0

2. Collect all the remainders produced by the divisions performed in Step 1

3. Write the remainders in the opposite order they have been produced

Page 12: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 12

Conversion from Base 10 to Base k (cont.)

Example: What is 17810 in base 5?

17810=12035

178 335 07 21 10

5

1*53 + 2*52 + 0*51 + 3*50 =

125 + 50 + 0 + 3 = 17810

quoteremainder

....

....

Conversion from Base 5 to base 10

Page 13: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 13

Conversion from Base k to Base 10

General Rule: To convert a number n from base k to base

10 you must:

1. Multiply each digit by kp where p is the position of the digit.

Since the digits that are 0 will produce a product equal to zero, you can skip them.

2. Add all the products obtained in Step 1.

Page 14: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 14

Conversion from Base k to Base t with k and t different from 2 or 10

To convert a number n from base k to base t you must perform two conversions:

1. Convert n from base k to base 10 2. Convert the result from base 10 to base t

Page 15: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 15

Conversion from Base k to Base t with k and t different from 2 or 10Example: What is 1536 in base 4?

1536=6910

1*62 + 5*61 +3*60 = 36 + 30 + 3 = 6910

69 117 14 01 10

4

First: Convert from Base 6 to base 10

Second: Convert the result from Base 10 to base 4

6910 = 10114

1536= 10114

Answer

Page 16: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 16

Conversion Algorithms

Base 10 is convenient for us while reading and writing numbers….however for the machine: The algorithm to convert to base 10 (to display results -

output) and the algorithm to convert from base 10 (to insert data - input) require time.

Can we display or insert data in a base different from 10? Base 2 –

Binary numbers are too long. Better a base closer to 10 What about 8 or 16?

Page 17: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 17

Fast conversion between bases that are power of 2

The power of 2 are special bases (ex. 4, 8, 16,..) the conversion between these bases is immediate

Example: Convert 37628 to base 2 Since 8 = 23 > 2 we expand each digit in 3 binary digits

This is possible because with 3 bits we can represent 8 distinct numbers in binary

3 7 6 2 8

0 1 1 1 1 1 1 1 0 0 1 0 2

37628 = 111 1111 00102

Answer

Page 18: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 18

Fast conversion between bases that are power of 2

Example: Convert 3B016 to base 2 Since 16 = 24 > 2 we expand each digit in 4 binary digits

This is possible because with 4 bits we can represent 16 distinct numbers in binary

In Base 16 there are 16 distinct symbols: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F

3 B 0 16

3B016 = 11 1011 00002

Answer0 0 1 1 1 0 1 1 0 0 0 0 2

Page 19: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 19

Example 2: Convert 1110 10102 to base 16 Since 2< 16 =24 we GROUP 4 binary digit in 1 hexadecimal digits

E A 16

Fast conversion between bases that are power of 2

Example 1: Convert 1110 10102 to base 8 Since 2 < 8=23 we GROUP 3 binary digit in 1 octal digits

3 5 2 81110 10102 = 3528

0 1 1 1 0 1 0 1 0 2

1 1 1 0 1 0 1 0 2

1110 10102 = EA16

Page 20: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 20

Converting decimal numbers in binary

To convert a decimal number n= int.dec in base 2 you must:

1. Convert the integer part int as shown so far2. Convert the decimal part in this way:

1. Multiply the decimal part dec by 2, then the decimal part of the product just obtained by 2 and continue this way until the product obtained already occurred or is 0.

2. Collect all the integer parts produced during the multiplication in Step 2.1

3. Write the integers in the same order they have been produced

Page 21: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 21

Converting decimal numbers in binary

Example: What is 178.2510 in base 2?Step 1: Step 2:

178.2510= 1011 0010. 012

178 089 144 022 011 15 12 01 10

2

0.25 × 2 = 0.5 00.5 × 2 = 1.0 10

2

Page 22: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 22

Converting decimal numbers in binary

Example: What is 0.32510 in base 2?

0.32510= 0. 0101001 2

0.325 × 2 = 0.65 00.65 × 2 = 1.30 10.30 × 2 = 0.60 00.60 × 2 = 1.20 10.20 × 2 = 0.40 00.40 × 2 = 0.80 00.80 × 2 = 1.60 10.60

2

period

Page 23: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 23

Summary

Conversion of an integer from Base 10 to Base 2 and vice versa

Conversion of an integer from Base 10 to Base k and vice versa

Conversion of an integer from Base k to Base t with k and t different from 2 or 10

Fast conversion between bases that are power of 2

Converting decimal numbers in binary

Page 24: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 24

End of Lesson

Page 25: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 25

Representing integers Decimal integers are converted to binary integers

Given k bits, the largest unsigned integer is 2k – 1; the smallest is 0

with 4 bits, the largest unsigned number is 24-1 = 15

Signed integers must also represent the sign (positive or negative). 1 bit is used for the sign. Given k bits, the largest signed integer is 2k-1 - 1; the smallest

is –(2k-1 – 1)

with 4 bits, the largest signed number is 23-1 = 7

Binary Representation of Numbers

Page 26: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 26

Representation of Numbers Signed integer representation Sign/magnitude notation

1 bit to represent the sign N bits to represent the integer

Example: if n=8+ 33 is represented as 0 0100001- 33 is represented as 1 0100001+ 1 is represented as 0 0000001- 1 is represented as 1 0000001

What about 0?+ 0 is represented as 0 0000000- 0 is represented as 1 0000000 Big Problem!!!

There are two representations for 0!!!

Sign Magnitude1 111 -7 1 110 -61 101 -51 100 -41 011 -31 010 -21 001 -11 000 -00 000 +00 001 +10 010 +20 011 +30 100 +40 101 +50 110 +60 111 +7

Page 27: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 27

Representation of Numbers Signed integer representation

Two’s complement notationa) a positive number follows the sign/magnitude

notationb) a negative number is obtained from the

corresponding positive number in two steps:1. Flip all the bits (0 becomes 1 and 1 becomes 0)2. Add one to the result

Page 28: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 28

Representation of Numbers Two’s complement notationExample: assume we use 8 bits precision

Q. What is +3310 in two’s complement?A. + 3310 is represented as 0 0100001

Q. What is -3310 in two’s complement?A. - 33 is represented as 1 1011111

Step 1: take +3310 (i.e. 0 0100001) Step 2: flip it (i.e. 1 1011110)Step 3: add 1 to it

1 1011110+ 1

1 1011111 = - 3310

Page 29: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 29

Representation of Numbers Advantages of the two’s complementrepresentation One representation for the zero Easy subtractions

Subtractions are simply additions The sign is embedded in the number!

Example: Suppose we use 8 bits precision15 0000 1111- 5 + 1111 1011+ 10 10000 1010

Two’s complement1 1111 -1 1 1110 -21 1101 -31 1100 -41 1011 -51 1010 -61 1001 -71 1000 -81 0111 -91 0110 -101 0101 -111 0100 -121 0011 -131 0010 -141 0001 -151 0000 -1600000 +00 0001 +10 0010 +20 0011 +30 0100 +40 0101 +50 0110 +60 0111 +70 1000 +80 1001 +90 1010 +100 1011 +110 1100 +120 1101 +130 1110 +140 1111 +15

- 9 111 10111- 5 + 111 11011- 14 1111 10010

overflow

Page 30: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 30

Representation of Numbers

Q: Given n bits precision, what is the interval of numbers that can be represented?

A:

In sign/magnitude the interval is (-2n-1, 2n-1) Note: the interval is open on both sides! There are 2 distinct representations of the 0!

In two’s complement the interval is [-2n-1, 2n-1) Note: the interval is open on one side!

Q: With n bits, how many distinct numbers can be represented?

A :

2n distinct numbers

Page 31: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 31

Representation of Numbers Representing real numbers

Real numbers may be put into binary Scientific Notation ± Mantissa × Bases ± Exponent

Number then are normalized so that first significant digit is immediately to the right of the binary point

Then only Mantissa and Exponent are stored.

Example: Consider Binary number:

+6.2510= +110.012is Normalized as:

+.110012 × 23

Page 32: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 32

Physical Representation of NumbersHow to store numbers in memory:

Assume that a number is stored in 16 bits storage location 1 bit for the sign 9 bits for the mantissa 1 bit for the sign of the exponent 5 bits for the exponent

+6.2510 = +110.012 = +.110012 × 23

has 0 for the sign 1101 for the mantissa 0 for the sign of the exponent 3 for the exponent

and in a 16 bits storage location is represented as 0110100000000011

Decimal point

0 1 1 0 0 1 0 0 0 0 0 0 0 0 1 1Sign Mantissa Sign Exponent

Page 33: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 33

Physical Representation of Numbers: Examples3/1610= 1/8+1/1610 = +0.00112 = +.112 × 2-2

has 0 for the sign 11 for the mantissa 1 for the sign of the exponent 2 for the exponent

and in a 16 bits storage location is represented as 0110000000100010

-19/6410= 1/4+1/32+1/6410 = -0.0100112 = -.100112 × 2-1

has 1 for the sign 10011 for the mantissa 1 for the sign of the exponent 1 for the exponent

and in a 16 bits storage location is represented as 1100110000100001

1 1 0 0 1 1 0 0 0 0 1 0 0 0 0 1Sign Mantissa Sign Exponent

0 1 1 0 0 0 0 0 0 0 1 0 0 0 1 0Sign Mantissa Sign Exponent

Page 34: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 34

Characters are mapped onto binary numbers

ASCII code set (look for the code in your book)

8 bits per character; 256 character codes

UNICODE code set

16 bits per character; 65,536 character codes

Text strings are sequences of characters in some encoding

http://ascii-table.com/

Binary Representation of Numeric and Textual Information (continued)

Page 35: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 35

Binary Representation of Sound

Multimedia data is sampled to store a digital form, with or without detectable differences

Representing sound data

Sound data must be digitized for storage in a computer

Digitizing means periodic sampling of amplitude values

Page 36: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 36

Binary Representation of Sound (cont.)

From samples, original sound may be approximated

To improve the approximation:

Sample more frequently

Use more bits for each sample value

Page 37: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 37

Figure 4.5Digitization of an Analog

Signal

(a) Sampling the Original Signal

(b) Recreating the Signal from the Sampled Values

Page 38: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 38

Representing image data

Images are sampled by reading color and intensity values at even intervals across the image

Each sampled point is a pixel

Image quality depends on number of bits at each pixel

Binary Representation of Images

Page 39: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 39

Binary Representation of Images (cont.) RGB – Encoding scheme

8 bits per color [0, 255] distinct values

A pixel in RGB uses 24 bits Black – absence of colors

0, 0, 0 White – all colors together

255, 255, 255

Example: representation of the above image in RGB 0,0,0 0,0,0 0,0,0 0,0,0 0,0,0 0,0,0 0,0,0 255,255,255 0,0,0 0,0,00,0,0 255,255,255 255,255,255 255,255,255 0,0,00,0,0 0,0,0 255,255,255 0,0,0 0,0,00,0,0 0,0,0 0,0,0 0,0,0 0,0,0

Page 40: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 40

Binary Representation of Images (cont.)

000000000000000000000000

000000000000000000000000

000000000000000000000000

000000000000000000000000

000000000000000000000000

000000000000000000000000

000000000000000000000000

111111111111111111111111

000000000000000000000000

000000000000000000000000

000000000000000000000000

111111111111111111111111

111111111111111111111111

111111111111111111111111

000000000000000000000000

000000000000000000000000

000000000000000000000000

111111111111111111111111

000000000000000000000000

000000000000000000000000

000000000000000000000000

000000000000000000000000

000000000000000000000000

000000000000000000000000

000000000000000000000000

Page 41: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 41

Storage size of Sound, Images, Movies Q. What is the total number of bits required to store 3 minutes of

standard recording in MP3 over two channels? 44.100 samples/sec * 16 bits/sec * 180 sec = 127,008,000 bits = 15,876,000

bytes = 15,504 KB = 15.1MB per channel 15.1MB * 2 channels = 30.2 MB

Q. What is the total number of bits required to store a picture taken from a digital camera with 3,500,000 pixels?

3,500,000 pixels/image * 24 bits/pixel * 1 image = 84,000,000 bits = 10,500,000 bytes = 10,254 KB = 10MB

Q. What is the total number of bits required to store 5 minutes a movie that shows 30 frames per second and has 3 million of pixels per frame?

3,000,000 pixels/frame * 24 bits/pixel * 30 frame/sec * 300 sec = 648,000,000,000 bits = 81,000,000,000 bytes = 79,101,562.5 KB = 77,247.6 MB = 75.4 GB

Page 42: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 42

End of Lesson

Page 43: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 43

The Reliability of Binary Representation Electronic devices are most reliable in a bistable

environment Bistable environment Distinguishing only two electronic states Current flowing or not Direction of flow

Computers are bistable: hence binary representations

Page 44: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 44

Magnetic core

Historic device for computer memory

Tiny magnetized rings: flow of current sets the direction of magnetic field

Binary values 0 and 1 are represented using the direction of the magnetic field

Binary Storage Devices

Page 45: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 45

Figure 4.9Using Magnetic Cores to Represent Binary Values

Page 46: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 46

Transistors

Solid-state switches: either permits or blocks current flow

A control input causes state change

Constructed from semiconductors

Binary Storage Devices (continued)

Page 47: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 47

Figure 4.11Simplified Model of a Transistor

Page 48: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 48

Boolean Logic and Gates: Boolean Logic

Boolean logic describes operations on true/false values

True/false maps easily onto bistable environment

Boolean logic operations on electronic signals may be built out of transistors and other electronic devices

Page 49: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 49

Boolean Logic (continued)

Boolean operations

a AND b

True only when a is true and b is true

a OR b

True when either a is true or b is true, or both are true

NOT a

True when a is false, and vice versa

Page 50: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 50

Boolean expressions

Constructed by combining together Boolean operations

Example: (a AND b) OR ((NOT b) AND (NOT a))

Truth tables capture the output/value of a Boolean expression

A column for each input plus the output

A row for each combination of input values

Boolean Logic (continued)

Page 51: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 51

a b Value0 0 10 1 01 0 01 1 1

Example:(a AND b) OR ((NOT b) and (NOT a))

From Boolean Expression to Truth Table

Page 52: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 52

Gates

Gates

Hardware devices built from transistors to mimic Boolean logic

AND gate

Two input lines, one output line

Outputs a 1 when both inputs are 1

Page 53: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 53

Gates (continued)

OR gate

Two input lines, one output line

Outputs a 1 when either input is 1

NOT gate

One input line, one output line

Outputs a 1 when input is 0 and vice versa

Page 54: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 54

Figure 4.15The Three Basic Gates and Their Symbols

Page 55: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 55

Abstraction in hardware design

Map hardware devices to Boolean logic

Design more complex devices in terms of logic, not electronics

Conversion from logic to hardware design may be automated

Gates (continued)

Page 56: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 56

Building Computer Circuits: Introduction

A circuit is a collection of logic gates:

Transforms a set of binary inputs into a set of binary outputs

Values of the outputs depend only on the current values of the inputs

Combinational circuits have no cycles in them (no outputs feed back into their own inputs)

Page 57: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 57

Figure 4.19Diagram of a Typical Computer Circuit

Page 58: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 58

A Circuit Construction Algorithm

Sum-of-products algorithm is one way to design circuits:

Step1: From Truth Table to Boolean Expression

Step 2: From Boolean Expression to Gate Layout

Page 59: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 59

Figure 4.21The Sum-of-Products Circuit Construction Algorithm

Page 60: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 60

Sum-of-products algorithm Truth table captures every input/output possible

for circuit

Repeat process for each output line Build a Boolean expression using AND and NOT for

each 1 of the output line

Combine together all the expressions with ORs

Build circuit from whole Boolean expression

A Circuit Construction Algorithm (continued)

Page 61: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 61

An application of Step 1 of the SOP algorithm: From Truth Table to Boolean Expression

1. Ignore rows where the output is 0.2. Create the product of the

selections in this way: for each row with output 1:

- if the input is 1, select the variable name.

- if the input is 0, select the NOT of the variable name.

3. Create the Boolean expression for the circuit by summing each of the products created in step 2.

==> This is the Boolean expression for the circuit to be constructed.

a b a XOR b0 0 01 0 10 1 11 1 0

ab’+a’bNote: a’=NOT a

b’=NOT b

Page 62: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 62

Another example a b c output0 0 0 10 0 1 10 1 0 00 1 1 01 0 0 11 0 1 01 1 0 11 1 1 1

a'b'c' +a'b’c +

ab'c' +

abc' +abc

or output = a'b'c' + a'b’c +ab'c' + abc' + abc

Page 63: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 63

Examples Of Circuit Design And Construction

Compare-for-equality circuit

Addition circuit

Both circuits can be built using the sum-of-products algorithm

Page 64: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 64

Compare-for-equality circuit

CE compares two unsigned binary integers for equality Given two n-bit numbers, a = a0a1a2…an-1 and b =

b0b1b2…bn-1 output 1 if the numbers are the same and 0 if they are not

Built by combining together 1-bit comparison circuits (1-CE)

Integers are equal if corresponding bits are equal (AND together 1-CD circuits for each pair of bits)

A Compare-for-equality Circuit

Page 65: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 65

1-CE circuit truth table

Or output = a’b’+ab

a b Output0 0 10 1 01 0 01 1 1

A Compare-for-equality Circuit (continued)

Page 66: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 66

Figure 4.22One-Bit Compare for Equality Circuit

Page 67: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 68

A Compare-for-equality Circuit (cont.)

1-CE

•Generalize the process by AND-ing together 1-CD circuits for each pair of bits

•The 1-CE circuit is represented by the box

o

o

o

o o o

a0

b0

a1b1

a2b2

an-1bn-1

out

1-CE

1-CE

1-CE

1-CE

1-CE

Page 68: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 69

An Addition Circuit

Addition circuit

Adds two unsigned binary integers, setting output bits and an overflow

Built from 1-bit adders (1-ADD)

Starting with rightmost bits, each pair produces

A value for that order (i.e. si=ai+bi)

A carry bit for next place to the left (i.e. ci)

Page 69: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 70

1-ADD truth table

Input

One bit from each input integer

One carry bit (always zero for rightmost bit)

Output

One bit for output place value

One “carry” bit

An Addition Circuit (continued)

Page 70: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 71

Figure 4.24The 1-ADD Circuit and Truth Table

Page 71: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 72

The 1-bit adder

Page 72: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 73

• Put rightmost bits into 1-ADD, with zero for the input carry• Send 1-ADD’s output value to output, and put its carry value as

input to 1-ADD for next bits to left. Repeat process for all bits.

Building the full adder

s0=a0+b0

s1= a1+b1

s2= a2+b2

sn-1= an-1 +bn-1

sn= cn

c0=0a0b0

a1b1

a2b2

an-1bn-1

o

o

o o o

1-ADD

1-ADD

1-ADD

1-ADD

c1

c2

cn-1

c3

Page 73: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 74

Control Circuits

Do not perform computations Choose order of operations or select among data values Major types of controls circuits

Decoders Sends a 1 on one output line, based on what input line

indicates Multiplexors

Select one of inputs to send to output

We will use these control circuits while studying next chapter.

Page 74: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 75

Decoder

Form

N input lines

2N output lines

N input lines indicate a binary number, which is used to select one of the output lines

Selected output sends a 1, all others send 0

Control Circuits (Decoder)

Page 75: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 76

Decoder purpose

Given a number code for some operation, trigger just that operation to take place

Numbers might be codes for arithmetic: add, subtract, etc.

Decoder signals which operation takes place next

Control Circuits (continued)

Page 76: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 77

Figure 4.29A 2-to-4 Decoder Circuit

Page 77: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 78

Decoder Circuit A decoder is a control circuit which has N input and 2N output

A 3 to 23 example of decoder

a b c o0 o1 o2 o3 o4 o5 o6 o70 0 0 1 0 0 0 0 0 0 00 0 1 0 1 0 0 0 0 0 00 1 0 0 0 1 0 0 0 0 00 1 1 0 0 0 1 0 0 0 01 0 0 0 0 0 0 1 0 0 0 1 0 1 0 0 0 0 0 1 0 01 1 0 0 0 0 0 0 0 1 01 1 1 0 0 0 0 0 0 0 1

a b c

01234567

Page 78: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 79

Multiplexor form 2N regular input lines N selector input lines 1 output line

Multiplexor purpose Given a code number for some input, selects that

input to pass along to its output Used to choose the right input value to send to a

computational circuit

Control Circuits (Multiplexor)

Page 79: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 80

Figure 4.28A Two-Input Multiplexor Circuit

Page 80: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 81

Multiplexor Circuit with N input

multiplexorcircuit

2N input lines

012

2N-1

N selector lines

output

Interpret the selector lines as a binary number.

Suppose that the selector line write the number 00….01 which is equal to 1.

For our example, the output is the value on the line numbered 1 . . . . .

Page 81: Chapter 4: The Building Blocks: Binary Numbers, Boolean ...personal.kent.edu/~aguercio/CS10051Slides/chapter04expanded.pdfChapter 4: The Building Blocks: Binary Numbers, Boolean Logic,

Invitation to Computer Science, C++ Version, Third Edition 82

Summary

Digital computers use binary representations of data: numbers, text, multimedia

Binary values create a bistable environment, making computers reliable

Boolean logic maps easily onto electronic hardware

Circuits are constructed using Boolean expressions as an abstraction

Computational and control circuits may be built from Boolean gates