Foundations of Computer Science Midterm

Ace your homework & exams now with Quizwiz!

Chapter 2 Examples of Algorithmic Problem Solving Example 2: Looking, Looking, Looking "Assume that we have a list of 10,000 names that we define as N1, N2, N3, . . . , N10,000, along with the 10,000 telephone numbers of those individuals, denoted as T1, T2, T3, . . . , T10,000. To simplify the problem, we initially assume that all names in the book are unique and that the names need not be in alphabetical order."

Chapter 2 Examples of Algorithmic Problem Solving Example 2: Looking, Looking, Looking (cont'd) Three versions here illustrate algorithm discovery, working toward a correct, efficient solution - A sequential algorithm (no loops or conditionals) - An incomplete iterative algorithm - A correct algorithm

Chapter 2 Examples of Algorithmic Problem Solving Example 1: Go Forth and Multiply (cont'd)

Chapter 2 Get input values - Get values for a and b • Compute the answer - Loop b times, adding each time* • Output the result - Print the final value* * steps need elaboration

Chapter 2 Introduction Algorithms for everyday may not be suitable for computers to perform (as in Chapter 1) • Algorithmic problem solving focuses on algorithms suitable for computers • Pseudocode is a tool for designing algorithms • This chapter will use a set of problems to illustrate algorithmic problem solving

Chapter 2 Representing Algorithms Pseudocode is used to design algorithms • Natural language is: - Expressive, easy to use - Verbose, unstructured, and ambiguous • Programming languages are: - Structured, designed for computers - Grammatically fussy, cryptic

Chapter 4 Building Computer Circuits (cont'd) • Circuit optimization - Reduce the number of gates needed to implement a circuit - Important part of hardware design

Chapter 4 Building Computer Circuits (cont'd) • Compare-for-equality (CE) circuit - Input is two unsigned binary numbers - Output is 1 if inputs are identical, and 0 otherwise - Start with one-bit version (1-CE) and build general version from that • Produce 1 as output if both its inputs are 0 or both its inputs are 1. Otherwise, 1-CE produces a 0

Chapter 4 Building Computer Circuits (cont'd) • 1-CE circuit: compare two input bits for equality • Truth table: • Boolean expression: (a b) + (~a ~b)

Chapter 4 Building Computer Circuits (cont'd) • N-bit CE circuit • Input: a0a1...an-1 and b0b1...bn-1, where ai and bi are individual bits • Pair up corresponding bits: a0 with b0, a1 with b1, etc. • Run a 1-CE circuit on each pair • AND the results

Chapter 4 The Binary Numbering System (cont'd) Invitation to Computer Science, 7th Edition 41 • Each transistor contains three lines: - Two input lines (control and collector) - One output line (emitter)

Chapter 4 Boolean Logic and Gates • Boolean logic: rules for manipulating true/false • Boolean expressions is used to construct circuits that perform operations - adding numbers, compare numbers and fetching instructions • Hardware design/logic design pertains to the design and construction of new circuits

Chapter 3 Summary We must evaluate the quality of algorithms, and compare competing algorithms to each other • Attributes: correctness, efficiency, elegance, and ease of understanding • Compare competing algorithms for time and space efficiency (time/space tradeoffs are common) • Orders of magnitude capture work as a function of input size: Θ(lg n), Θ(n), Θ(n2), Θ(2n) • Problems with only exponential algorithms are intractable

That's the end of Chapter 3 Slides

That's It.

That is all to study for the Midterm.

Chapter 4 Summary • Sum-of-products is a circuit design algorithm: takes a specification and ends with a circuit • We can build circuits for basic algorithmic tasks: - Comparisons (compare-for-equality circuit) - Arithmetic (adder circuit) - Control (multiplexor and decoder circuits)

That's all from Chapter 4 Power-point Slides

Chapter 1 Introduction Misconceptions - Computer science is: • The study of computers • The study of how to write computer programs • The study of the uses and applications of computers and software

Chapter 1 Introduction Misconceptions Computer science is: • The study of computers - In theoretical computer science, researchers study the logical and mathematical properties of problems and their solutions rather than investigating problems with actual computers

Chapter 1 Introduction Misconceptions - Computer science is: • The study of how to write computer programs - Programming is an important tool to researchers to study new ideas, build and test new solutions

Chapter 1 Introduction Misconceptions - Computer science is: • The study of the uses and applications of computers and software - Some courses teach the use of a number of popular packages, such as database systems, spreadsheets... - The computer scientists specify, design, build and test software packages

Chapter 1 The Definition of Computer Science Computer science is the study of algorithms, including: - Their formal and mathematical properties • Study the behavior of algorithms to determine its correctness - Their hardware realizations • Design and build computer system that can execute algorithms

Chapter 1 The Definition of Computer Science (continued) Algorithm - Informally, "an ordered sequence of instructions that is guaranteed to solve a specific problem." • Operations used to construct algorithms - Sequential operations • A sequential instruction carries out a single welldefined task - Conditional operations • The "question-asking" instructions of an algorithm - Iterative operations • The "looping" instructions of an algorithm

Chapter 2 Representing Algorithms (cont'd) Do/while, alternate iterative operation - Posttest loop: Continuation condition is tested at the end of loop body - Loop body always performed at least once • Primitive operations: sequential, conditional, and iterative are all that is needed - These are instructions that we assume computer understands - They are capable of executing without further explanation or simplification

Chapter 2 Examples of Algorithmic Problem Solving Example 1: Go Forth and Multiply "Given two nonnegative integer values, a ≥ 0, b ≥ 0, compute and output the product (a × b) using the technique of repeated addition. That is, determine the value of the sum a + a + a + . . . + a (b times)."

Chapter 2 Examples of Algorithmic Problem Solving Example 1: Go Forth and Multiply (cont'd) Loop b times, adding each time - Get values for a and b - Set the value of count to 0 - While (count < b) do • ... the rest of the loop* • Set the value of count to count + 1 - End of loop * steps need elaboration

Chapter 2 Examples of Algorithmic Problem Solving Example 1: Go Forth and Multiply (cont'd) Loop b times, adding each time - Get values for a and b - Set the value of count to 0 - Set the value of product to 0 - While (count < b) do • Set the value of product to (product + a) • Set the value of count to count + 1 - End of loop • Output the result - Print the value of product

Chapter 2 Examples of Algorithmic Problem Solving Example 3: Big, Bigger, Biggest A "building-block" algorithm used in many libraries • Library: A collection of pre-defined useful algorithms "Given a value n ≥ 1 and a list containing exactly n unique numbers called A1, A2, . . . , An, find and print out both the largest value in the list and the position in the list where that largest value occurred."

Chapter 2 Examples of Algorithmic Problem Solving Example 4: Meeting Your Match Pattern-matching: common across many applications, such as: - Word processor search, web search, image analysis, and human genome project "You will be given some text composed of n characters that will be referred to as T1 T2 . . . Tn. You will also be given a pattern of m characters, m ≤ n, that will be represented as P1 P2 . . . Pm. The algorithm must locate every occurrence of the given pattern within the text. The output of the algorithm is the location in the text where each match occurred."

Chapter 2 Representing Algorithms (cont'd) Control operation: changes the normal flow of control • Conditional statement: asks a question and selects among alternative options 1. Evaluate the true/false condition 2. If the condition is true, then do the first set of operations and skip the second set 3. If the condition is false, skip the first set of operations and do the second set 4. Once the appropriate set of operations has been completed, continue executing the algorithm with the operation that follows the if/then/else instruction.

Chapter 2 Representing Algorithms (cont'd) Iteration: an operation that causes looping, repeating a block of instructions • While statement repeats while a condition remains true - Continuation condition: a test to see if while loop should continue - Loop body: instructions to perform repeatedly • Example: repeated mileage calculations

Chapter 2 Representing Algorithms Pseudocode lies somewhere between these two - It contains only statements that have a well-defined structure, it is easier to visualize the organization of a pseudocode algorithm - It closely resembles many popular programming languages, the subsequent translation of the algorithm into a computer program is relatively simple

Chapter 2 Representing Algorithms (cont'd) • Sequential operations perform a single task - Computation: a single numeric calculation - Input: gets data values from outside the algorithm - Output: sends data values to the outside world • A variable is a named location to hold a value • A sequential algorithm is made up only of sequential operations • Example: computing average miles per gallon

Chapter 2 Examples of Algorithmic Problem Solving Example 4: Meeting Your Match (cont'd) Algorithm has two parts: 1. Sliding the pattern along the text, aligning it with each position in turn 2. Given a particular alignment, determine if there is a match at that location • Solve parts separately and use - Abstraction, focus on high level, not details - Top-down design, start with big picture, gradually elaborate parts

Chapter 2 Summary Pseudocode is used for algorithm design: structured like code, but allows English and math phrasing and notation • Pseudocode is made up of: sequential, conditional, and iterative operations • Algorithmic problem solving involves: - Step-by-step development of algorithm pieces - Use of abstraction, and top-down design

Chapter 3 Analysis of Algorithms Data Cleanup Algorithms "Given a collection of age data, where erroneous zeros occur, find and remove all the zeros from the data, reporting the number of legitimate age values that remain" • Illustrates multiple solutions to a single problem • Use of analysis to compare algorithms

Chapter 3 Analysis of Algorithms Data Cleanup Algorithms (cont'd) Shuffle-left algorithm - Search for zeros from left to right - When a zero is found, shift remaining values in the list one cell to the left - Example: [55, 0, 32, 19, 0, 27] - Finds 0 at position 2: [55, 32, 19, 0, 27, 27] - Finds 0 at position 4: [55, 32, 19, 27, 27, 27]

Chapter 3 Measuring Efficiency Order of Magnitude--Oder n^2 Order n2 , Θ(n2): the set of functions whose growth is on the order of n2 Eventually, every function with order n2 has greater values than any function with order n

Chapter 3 Analysis of Algorithms Notes It is not simple that people use the one "best" sorting algorithm - Some algorithms(unlike the selection sort) are sensitive to what the original input looks like - One algorithm might work well if the input is already to being sorted, whereas another algorithm might work better if the input is random

Chapter 3 Introduction • Many solutions to any given problem • How can we judge and compare algorithms? • For example: purchasing a car - Ease of handling - Style - Fuel efficiency • Evaluating an algorithm - Ease of understanding - Elegance - Time/space efficiency

Chapter 3 Attributes of Algorithms Attributes of interest: correctness, ease of understanding, elegance, and efficiency • Correctness - Is the problem specified correctly? - Does the algorithm produce the correct result? • Example: pattern matching - Problem spec: "given pattern p and text t, determine the location, if any, of pattern p occurring in text t" - Algorithm correct: does it always work?

Chapter 3 Analysis of Algorithms Data Cleanup Algorithms (cont'd) Time efficiency for converging-pointers - Best case: no zeros, left pointer just moves across to pass the right pointers, examines each value: Θ(n) - Worst case: all zeros, examines each value and copies a value over it, right pointer moves left towards left pointer: Θ(n) • Space efficiency for converging-pointers - No significant extra space needed

Chapter 3 Analysis of Algorithms Binary Search Binary Search Algorithm - The binary search algorithm is more efficient but it works only when the search list is already sorted - Given a target value and an ordered list of values, find the location of the target in the list, if it occurs, by starting in the middle and splitting the range in two with each comparison - The algorithm halts when target is found or when the sublist becomes empty

Chapter 3 Analysis of Algorithms Binary Search (cont'd) • Central unit of work: comparisons against target • Best case efficiency - Value is the first middle value: 1 comparison • Worst case efficiency - Value does not appear, repeats as many times as we can divide the list before running out of values: Θ(lg n) - The number of times a number n can be cut in half and not go below 1 is called the logarithm of n to the base 2, which is lg n

Chapter 3 Analysis of Algorithms Binary Search (cont'd) An unsorted list could be sorted before using a binary search, but sorting also takes a lot of work. - If a list is to be searched only a few times for a few particular items, then it is more efficient to do sequential search: a few Θ(n) tasks - If a list is to be searched repeatedly, it is more efficient to sort it and then use binary search: one Θ(n2) task and many Θ(lg n) tasks • Binary search requires a small amount of additional storage to keep track of beginning, end, and midpoint position in the list.

Chapter 3 Analysis of Algorithms Binary Search (cont'd) When there is an odd number of elements in the list, there is a middle position between beginning and end. • If there is an even number of elements in the list, we define the "middle" of an even number of entries as the end the fist half of the list - e.g. 1 2 3 4 5 6 • It is easier to see how the binary search algorithm operates if we list the locations of the numbers checked in a treelike structure.

Chapter 3 Analysis of Algorithms Binary Search (cont'd) • Programming tips: - Middle position: int mid = left + (right - left) / 2 int mid = (left + right) / 2 : may cause overflow - left > right: could not find target number in the list - left = right: there is only one number to compare

Chapter 3 Analysis of Algorithms Data Cleanup Algorithms (cont'd) Analysis of shuffle-left for time efficiency - Count comparisons looking for zero AND movements of values - Examine each of the n elements in the list: at least Θ(n) - Copy numbers: every value is a zero, n passes, during each of which n-1 copies are done. • Total copies: n (n - 1) = n2 - n

Chapter 3 Analysis of Algorithms Data Cleanup Algorithms (cont'd) Analysis of shuffle-left for time efficiency - Best case: no zeros occur, check each value and nothing more: Θ(n) - Worst case: every value is a zero, move n-1 values, then n-2 values, etc.: Θ(n2) • Analysis of shuffle-left for space efficiency - Uses no significant space beyond input

Chapter 3 Analysis of Algorithms Converging-pointers algorithm - Keep track of two pointers at the data - Left pointer moves left to right and stops when it sees a zero value - Right pointer stays put until a zero is found - Then its value is copied on position of the zero, and it moves one cell to the left - Stop when the left crosses the right

Chapter 3 Analysis of Algorithms Data Cleanup Algorithms (cont'd) Example: [55, 0, 32, 19, 15, 27] [55, 0, 32, 19, 15, 27] L R [55, 0, 32, 19, 15, 27] L R [55, 27, 32, 19, 15, 27] L R [55, 27, 32, 19, 15, 27] LR

Chapter 3 Analysis of Algorithms (cont'd) Copy-over algorithm - Create a second, initially empty, list - Look at each value in the original - If it is non-zero, copy it to the second list • Example: [55, 0, 32, 19, 0, 27] 1. answer = [55] 4. answer = [55, 32, 19] 2. answer = [55] 5. answer = [55, 32, 19] 3. answer = [55, 32] 6. answer = [55, 32, 19, 27]

Chapter 3 Analysis of Algorithms Data Cleanup Algorithms (cont'd) Time efficiency for copy-over - Best case: all zeros, checks each value but doesn't copy it: Θ(n) - Worst case: no zeros, checks each value and copies it: Θ(n) • Space efficiency for copy-over - Best case: all zeros, uses no extra space - Worst case: no zeros, uses n extra spaces • More time efficient and less space efficient.

Chapter 3 Analysis of Algorithms Pattern Matching Algorithm from chapter 2 • Best case: when first symbol of pattern does not appear in text • Worst case: when all but last symbol of pattern make up the text

Chapter 3 Analysis of Algorithms Pattern Matching (cont'd) Best case example - Pattern = "xyz" text = "aaaaaaaaaaaaaaa" - At each step, compare 'x' to 'a' and then move on - Θ(n) comparisons • Worst case example - Pattern = "aab" text = "aaaaaaaaaaaaaaa" - At each step, compare m symbols from pattern against text before moving on - Θ(m*n) comparisons

Chapter 4 The Binary Numbering System (cont'd) • It is critical to remember that every pattern of binary digits has multiple interpretations • The correct interpretation is determined only when that binary pattern is used in a specific way

Chapter 4 The Binary Numbering System (cont'd) • Floating point numbers use binary scientific notation - Scientific notation, base 10: 1.35 × 10-5 - Base 2: 3.2510 = 11.012 = 1.101 × 21 • Characters and text: map characters onto binary numbers in a standard way - ASCII (8-bit numbers for each character) - Unicode (16-bit numbers for each character) • Unicode table: https://unicodetable.com/en/#controlcharacter

Chapter 3 Attributes of Algorithms (cont'd) Ease of understanding, useful for: - Checking correctness - Program maintenance • Elegance: using a clever or non-obvious approach - Example: Gauss' summing of 1 + 2 + ... + 100 • Attributes may conflict: elegance often conflicts with ease of understanding • Attributes may reinforce each other: ease of understanding supports correctness

Chapter 3 Attributes of Algorithms (cont'd) • Efficiency: an algorithm's use of time and space resources - Timing an algorithm is not always useful - Confounding factors: machine speed, size of input - It's an extremely desirable attribute of an algorithm - Space efficiency • Determined by amount of information the algorithm must store in computer's memory to do this job • And initial data on which the algorithm is operating - Time efficiency • Number of instructions

Chapter 3 Measuring Efficiency Sequential Search The work an algorithm does can usually be expressed in terms of a formula that depends on the size of the problem input • Sequential search is not sufficiently time efficient for large values of n • Sequential search is very space efficient - uses essentially no more memory storage than the original input requires

Chapter 3 Measuring Efficiency Order of Magnitude--Order n Order of magnitude n, Θ(n): the set of functions that grow in a linear fashion • Simplification: - ignore the constants, look only at the order of magnitude - n, 0.5n, 2n, 4n, 3n+5, 2n+100, 0.1n+3 ....are all linear - we say that their order of magnitude is n • 3n+5 is order of magnitude n:3n+5 = Q(n) • 2n +100 is order of magnitude n: 2n+100=Q(n) • 0.1n+3 is order of magnitude n: 0.1n+3=Q(n) • ...

Chapter 3 Measuring Efficiency Order of Magnitude-Order n -Order of magnitude n, Θ(n): the set of functions that grow in a linear fashion -Change in growth as n increases is constant size

Chapter 3 Measuring Efficiency Selection Sort Sorting: the task of putting a list of values into numerical or alphabetical order • Key idea - Pass repeatedly over the unsorted portion of the list - Each pass select the largest remaining value - Move that value to the end of the unsorted values

Chapter 3 Attributes of Algorithms (con'd) Benchmarking: timing an algorithm on standard data sets - Testing hardware and operating system, etc. • Rating one machine against another - Testing real-world performance limits • Rating how sensitive a particular algorithm is with respect to variations in input on one particular machine • To measure time efficiency, we identify the fundamental unit of work of an algorithm and count how many times the work unit is executed

Chapter 3 Measuring Efficiency Sequential Search -Analysis of algorithms: the study of the efficiency of algorithms • Searching: the task of finding a specific value in a list of values, or deciding it is not there • Sequential search algorithm (from Ch. 2) - Given a target value and a random list of values, find the location of the target in the list, if it occurs, by checking each value in the list in turn

Chapter 3 Measuring Efficiency Sequential Search (cont'd) In sequential search, comparison of target name to each name in the list • Given a big input list: - Best case is smallest amount of work algorithm does - Worst case is greatest amount of work algorithm does - Average case depends on likelihood of different scenarios occurring

Chapter 3 Measuring Efficiency Sequential Search (cont'd) Best case: target found with the first comparison • Worst case: target never found or last value • Average case: if each value is equally likely to be searched, work done varies from 1 to n, averages to n/2

Chapter 3 Measuring Efficiency Selection Sort: (cont'd) Example: Selection Sort on [5, 1, 3, 9, 4] • Pass 1 - Select 9 as the largest in the whole list - Swap with 4 to place in last slot - [5, 1, 3, 4, 9] • Pass 2 - Select 5 as the largest in the first four values - Swap with 4 to place in last remaining slot - [4, 1, 3, 5, 9]

Chapter 3 Measuring Efficiency Slection Sort: (cont'd) Example: Selection Sort on [5, 1, 3, 9, 4] • Pass 3 - Select 4 as the largest in the first three - Swap with 3 to place in last slot - [3, 1, 4, 5, 9] • Pass 4 - Select 3 as the largest in the first two values - Swap with 1 to place in last remaining slot - [1, 3, 4, 5, 9]

Chapter 3 Measuring Efficiency Selection Sort (cont'd) Central unit of work: hidden in "find largest" step • Work done to find largest changes as unsorted portion shrinks • (n-1) + (n-2) + ... + 2 + 1 = ((n-1) / 2) n = n (n-1) / 2

Chapter 3 Swap Example void swap(int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; } int num1 = 2, num2 = -3; swap(&num1, &num2);

Chapter 3 When Things Get Out of Hand • Polynomially bounded: an algorithm that does work on the order of Θ(nk) • Most common problems are polynomially bounded • Hamiltonian circuit is NOT - Given a graph, find a path that passes through each vertex exactly once and returns to its starting point

Chapter 3 When Things Get Out of Hand (cont'd) Possible paths in the graph are paths through a tree of choices • Simplest case has exactly two choices per vertex • Number of paths to examine = number of leaves in the tree • Height of the tree = n+1 (n is the number of vertices in the graph) • Number of leaves = 2n

Chapter 3 When Things Get Out Of Hands (cont'd) Exponential algorithm: an algorithm whose order of growth is Θ(2n) • Intractable: problems with no polynomially bounded solutions - Hamiltonian circuit - Traveling Salesperson - Bin packing - Chess

Chapter 3 When Things Get Out of Hands (cont'd) Approximation algorithms: algorithms that partially solve, or provide sub-optimal solutions to, intractable problems • Example: bin packing • For each box to be packed: - Check each current bin • If new box fits in the bin, place it there - If no bin can hold the new box, add a new bin

Chapter 4 Building Computer Circuits Full adder circuit • Input is two unsigned N-bit numbers • Output is one unsigned N-bit number, the result of adding inputs together • Example: • Start with one-bit adder (1-ADD) Invitation to Computer Science, 7th Edition 70 1 0 0 1 0 1 + 0 1 0 0 1 0 1 1

Chapter 4 Building Computer Circuits (cont'd) • Sum digit, si , has Boolean expression: (~ai ~bi ci ) + (~ai bi ~ci ) + (ai ~bi ~ci ) + (ai bi ci ) • Carry digit, ci+1, has Boolean expression: (~ai bi ci ) + (ai ~bi ci ) + (ai bi ~ci ) + (ai bi ci )

Chapter 4 Building Computer Circuits (cont'd) Example from textbook • Step 1: build truth table: in this example, the output values are completely arbitrary

Chapter 4 Building Computer Circuits (cont'd) Step 2: - Choose any one output column of the truth table built in Step 1 and scan down that column - Every place that you find a 1 in the output column, you build a Boolean subexpression that produces the value 1 for exactly that combination of input values and no other • If input is a 1, use that input value in subexpression • If the input is a 0, use that the NOT of that input, changing it from a 0 to a 1, and then use that complement input value in subexpression • ANDed input together, then the output value is a 1

Chapter 4 Building Computer Circuits • Circuit: a collection of logic gates that transforms a set of binary inputs into a set of binary outputs - The values of the outputs depend only on the current values of the input: no state - Has input wires, contains gates connected by wires, and has output wires

Chapter 4 Building Computer Circuits (cont'd) • Direct relationship between Boolean expressions and circuit diagrams of this type - Every Boolean expression can be represented pictorially as a circuit diagram - Every output value in a circuit diagram can be written as a Boolean expression • A website to draw circuit diagram: - https://academo.org/demos/logic-gate-simulator/

Chapter 4 Control Circuits • Each input line corresponds to a unique pattern on selector lines - Each of the N selector lines can be set to 0 or 1 - Use the N selector lines to represent all binary values from 000...0 (N zeros) to 111...1 (N ones), which represent integer value from 0 to 2N -1. These numbers correspond to the numbers of the input lines - Thus, the binary number that appears on the selector lines can be interpreted as the identification number of the input line that to be selected

Chapter 4 Control Circuits (cont'd) • Decoder sends a signal out only one output, chosen by its input - N input lines - 2N output lines • Each output line corresponds to a unique pattern on input lines • Only the chosen output line produces 1, all others output 0

Chapter 4 Introduction • This chapter explains how computers work • All computing devices are built on the ideas in this chapter - Laptops, desktops - Servers, supercomputers - Game systems, cell phones, MP3 players - Calculators, singing get-well cards - Embedded systems, in toys, cars, microwaves, etc

Chapter 4 The Binary Numbering System • How can an electronic (or magnetic) machine represent information? • Key requirements: clear, unambiguous, reliable • External representation is human-oriented - Base-10 numbers - Keyboard characters or virtual keypad • Internal representation is computer-oriented - Base-2 numbers - Base-2 codes for characters

Chapter 4 The Binary Numbering System (cont'd) • Sounds and images require converting naturally analog representations to digital representations • Sound waves characterized by: - Amplitude: height of the wave at a moment in time is a measure of its loudness - Period: designated as T, is the time it takes for the wave to make one complete cycle - Frequency: is the total number of periods (circles) per unit time measured in cycles/second, also called hertz, and defined as f = 1/T

Chapter 4 The Binary Numbering System (cont'd) • Digitize: to convert to a digital representation • Sampling: record sound wave values at fixed, discrete intervals • To reproduce sound, approximate using samples • Quality is determined by: - Sampling rate: number of samples per second • More samples ► more accurate wave form - Bit depth: number of bits used to encode per sample • More bits ► more accurate amplitude

Chapter 4 The Binary Numbering System (cont'd) • Image sampling: record color or intensity at fixed, discrete intervals in two dimensions • The sampling process, often called scanning, consists of measuring the intensity values of distinct points (pixels) located at intervals across the image • Pixels: individual recorded samples, more pixels used, the more accurate the encoding of the image

Chapter 4 The Binary Numbering System (cont'd) • Raster graphics store picture as 2-D grid of pixel values, each pixel is encoded as an unsigned binary value representing its gray scale intensity • RGB encoding scheme: - Colors are combinations of red, green, and blue - One byte, or 8 bits, for each color (red, green, blue) - Represent an intensity range of 0 to 255 for each color, 0 means no contribution from this color, 255 means full contribution • e.g. hot pink: 255 105 180, harvest gold: 218 165 32

Chapter 4 Boolean Logic and Gates (cont'd) • Gate: an electronic device that operates on a collection of binary inputs to produce a binary output • Each gate corresponds to a Boolean operator

Chapter 4 Boolean Logic and Gates (cont'd) • Gates are built from transistors - NOT gate: 1 transistor - AND gate: 3 transistors - OR gate: 3 transistors - NAND (connected in sequence) and NOR (connected in parallel): 2 transistors

Chapter 4 Boolean Logic and Gates • Binary 1/0 maps to true/false of Boolean logic • Boolean expressions: x ≤ 35, a = 12 • Boolean operators: - (0 ≤ x) AND (x ≤ 35), (a = 12) OR (a = 13), NOT (a = 12) - (0 ≤ x) (x ≤ 35), (a = 12) + (a = 13), ~(a = 12)

Chapter 4 Boolean Logic and Gates (cont'd) • Truth tables lay out true/false values for Boolean expressions, for each possible true/false input • Example: (a b) + (a ~b)

Chapter 4 Building Computer Circuits (cont'd) N-bit adder circuit • Input: a0a1...an-1 and b0b1...bn-1, where ai and bi are individual bits • a0 and b0 are least significant digits: ones place • Pair up corresponding bits: a0 with b0, a1 with b1, etc. • Run 1-ADD on a0 and b0, with fixed carry in c0 = 0 • Feed carry out c1 to next 1-ADD and repeat

Chapter 4 Building Computer Circuits Addition of two N-bit integer values requires N separate 1-ADD circuits - In Figure 4.31, each 1-ADD circuit uses 3 NOT gates, 16 AND gates, and 6 OR gates, in total 25 logic gates - Let's assume that N = 32 for modern computer, 32-bit binary addition is 32 x 25 = 800 gates - In Figure 4.18, each NOT gate requires 1 transistor, and Figure 4.19 shows each AND and OR gate requires 3 transistors - In total, 2208 transistors are needed to build a 32-bit adder circuit

Chapter 4 The Binary Numbering System (cont'd) • Binary multiplication: 0*0=0, 0*1=0, 1*0=0, 1*1=1 The Binary Numbering System (cont'd) • Binary division:

Chapter 4 The Binary Numbering System (cont'd) • Signed integers include negative numbers • Sign/magnitude notation uses 1 bit for the sign, the rest for the value +5 = 0101, -5 = 1101 0 = 0000 and 1000! • Two's complement representation: to make the negative of a number, flip every bit and add one +5 = 0101, -5 = 1010 + 1 = 1011 0 = 0000, -0 = 1111 + 1 = 0000

Chapter 4 Building Computer Circuits (cont'd) To convert a circuit to a Boolean expression: - Start with output and work backwards • Find next gate back, convert to Boolean operator • Repeat for each input, filling in left and/or right side • E.g. Figure 4.22 - c = (a OR b) - d = NOT ( (a OR b) AND (NOT b) ) • To convert a Boolean expression to a circuit: - Similar approach

Chapter 4 Building Computer Circuits (cont'd) -To build a circuit from desired outcomes: -Use standard circuit construction algorithm: - E.g., sum-of-products algorithm - Step 1: truth table construction >> N inputs, 2^N combination of input values - Step 2: subexpression construction using AND and NOT gates -Step 3: subexpression combination using OR gates - Step 4: circuit diagram production

Chapter 4 Building Computer Circuits (cont'd) For each true row, AND input to make 1 - a = 0, b = 1, c = 0: (~a b ~c) - a = 1, b = 1, c = 0: (a b ~c) • Step 3: - Take each of the subexpression produced in Step 2 and combine them, two at a time, using OR gates • (~a b ~c) + (a b ~c)

Chapter 4 Building Computer Circuits (cont'd) • Step 4: Build circuit from expression - Converting the Boolean expression produced at the end of Step 3 into a circuit diagram • And repeat for the other output (e.g. output-2)

Chapter 4 Control Circuits • Control circuits: are used not implement arithmetic operations, But - To determine the order in which operations are carried out - To select the correct data values to be processed • Control circuits: sequencing and decision-making circuits inside a computer • Two major types of control circuits: multiplexors and decoders - They can be described in terms of gates and the rules of logic

Chapter 4 Control Circuits Multiplexor selects one of its many input lines and copy the binary value on that input line onto its single output line - 2N input lines - N selector lines - 1 output line • A multiplexor chooses one specific input by using an additional set of N lines called Selector lines - Total inputs to the multiplexor circuit is N + 2

Chapter 4 Control Circuits (cont'd) • Decoder Example - 3 input lines, it has 8 output lines numbered 0 to 7 - These three input lines can represent all binary values from 000 to 111 (from 0 to 7 in decimal) - If the binary values on the three input lines are 101, then a signal (a binary 1) would be sent out by the decoder on output line 5 - All other output lines would contain a 0

Chapter 4 Control Circuits (cont'd) Decoder circuit uses - To select a single arithmetic instruction, given a code for that instruction - Code activates one output line; that line activates corresponding arithmetic circuit • Multiplexor circuit uses - To choose one data value from among a set, based on selector pattern - Many data values flow into the multiplexor, only the selected one comes out

Chapter 4 Control Circuits (cont'd) • A Decoder circuit can be used to select the correct instruction, a multiplexor can help ensure that the computer executes this instruction using the correct data.

Chapter 4 Summary • Computers use binary representations because they maximize reliability for electronic systems • Many kinds of data may be represented at least in an approximate digital form using binary values • Boolean logic describes how to build and manipulate expressions that are true/false • We can build logic gates that act like Boolean operators using transistors • Circuits may be built from logic gates: circuits correspond to Boolean expressions

Chapter 4 The Binary Numbering System (cont'd) Data compressions: storing data in a reduced-size form to save space/time Lossless: data can be perfectly restored Lossy: data cannot be perfectly restored Compression schemes are usually evaluated by their compression ratio, which measures how much they reduce the storage requirements of the data: - compression ratio = size of the uncompressed data/ size of the compressed data

Chapter 4 The Binary Numbering System (cont'd) Run-length encoding: replace a sequence of identical values v1, v2,..., vn by a pair of values (v, n), which indicates that value v is replicated n times. 255 255 255 255 0 0 255 255 255 255 0 0 255 255 255

Chapter 4 The Binary Numbering System (cont'd) • Computers use fixed-length binary numbers for integers, e.g., with 4 bits could represent 0 to 15 • Arithmetic overflow: when the computer tries to make a number that is too large, e.g. 14 + 2 with 4 bits - Computer science must deal with a finite or a limited set of representations, it must handle the errors that occur when those limits are exceeded • Binary addition: 0+0=0, 0+1=1, 1+0=1, 1+1=0 with carry of 1 • Example: 0101 + 0011 = 1000

Chapter 4 The Binary Numbering System (cont'd) • Binary subtraction: 0-0=0, 1-0=1, 1-1=0, 10-1=1 • Example: 1111 - 1001 = 110 1001 - 110 = 11 • Online tutorial: - https://www.youtube.com/watch?v=h_fY-zSiMtY

Chapter 4 The Binary Numbering System (cont'd) Variable-length code sets: is often used to compress text but can also be used with other forms of data • E.g. HAWAII can be coded to 010 00 110 00 10 10 • Compression ratio: 24 / 14 = 1.71

Chapter 4 The Binary Numbering System (cont'd) • Computers use binary not for theoretical reasons but for reasons of "bistable" systems are reliable - Current on/off - Magnetic field left/right - Magnetic cores were used to construct computer memories from roughly 1955 to 1975 - The two states (ON and OFF) used to represent the binary values 0 and 1 are based on the direction of the magnetic field of the core • The problem with base-10 representation is that it needs to store 10 unique symbols, it needs devices that have 10 stable states

Chapter 4 The Binary Numbering System (cont'd) • The binary numbering system is a base-2 positional numbering system • Base ten: - Uses 10 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 - Each place corresponds to a power of 10 - 1,943 = 1 * 103 + 9 * 102 + 4 * 101 + 3 * 100 • Base two: - Uses 2 digits: 0, 1 - Each place corresponds to a power of 2 - 1101 = 1 * 23 + 1 * 22 + 0 * 21 + 1 * 20 = 13

Chapter 4 The Binary Numbering System (cont'd) • Converting from binary to decimal - Add up powers of two where a 1 appears in the binary number • Converting from decimal to binary - Repeatedly divide by two and record the remainder - Example, convert 9: • 9/2 = 4 remainder 1, binary number = 1 • 4/2 = 2 remainder 0, binary number = 01 • 2/2 = 1 remainder 0, binary number = 001 • 1/2 = 0 remainder 1, binary number = 1001

Chapter 4 The Binary Numbering System (cont'd) True Color: using 3 bytes of information per pixel, allows to represent 2^24 distinct colors It is used in JPEG color imaging format HDMI (HIGH DEFINITION MULTIMEDIA INTERFACE): allows pixel bit depths of up to 48 bits, allowing for 2^48 distinct colors

Chapter 4 The Binary Numbering System (cont'd) • Sound and image data typically require huge amounts of storage, far more than is required for the numbers and text • Data size: how much to store - 1000 integer values - 10-page text paper - 60-second sound file - 480 by 640 image

Chapter 4 The Binary Numbering System (cont'd) Transistors - Constructed from semiconductors, e.g. silicon - Much like light switch • OFF state: does not allow electricity to flow • ON state: electricity can pass - Solid-state switches that has no mechanical or moving parts - Switching of a transistor is done electronically rather than mechanically, change on/off when given power on control line - Extremely small (billions per chip (integrated circuit))

Chapter 4 The Binary Numbering System (cont'd) • Transistors - Enable computers that work with gigabytes of data - The more transistor that can be packed into a fixed amount of space à the greater the processing power of the computer and the greater the amount of information than can be stored in memory


Related study sets

Microbiology - Preanalytic Procedures

View Set

Module 48: Anxiety-Related Disorders

View Set

Unit 8 Quiz for Alabama Hunter Education

View Set

MTEL: Communications and Literacy

View Set

AGAMA FORM 4 - PEL. 13 HAJI DAN UMRAH

View Set

Ordering the Chaos of the Contemporary World: An Introduction to Freakonomics

View Set