Number Theory Algorithms

Similar documents
Binary Representations and Arithmetic

CS & IT Conversions. Magnitude 10,000 1,

SCHOOL OF ENGINEERING & BUILT ENVIRONMENT. Mathematics. Numbers & Number Systems

COMP Overview of Tutorial #2

World Inside a Computer is Binary

Objectives. Connecting with Computer Science 2

in this web service Cambridge University Press

Introduction to Numbering Systems

Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

Number System. Introduction. Decimal Numbers

Arithmetic Processing

Topic Notes: Bits and Bytes and Numbers

Digital Arithmetic. Digital Arithmetic: Operations and Circuits Dr. Farahmand

Slide Set 1. for ENEL 339 Fall 2014 Lecture Section 02. Steve Norman, PhD, PEng

Numeral Systems. -Numeral System -Positional systems -Decimal -Binary -Octal. Subjects:

Chapter 2: Number Systems

add and subtract whole numbers with more than 4 digits, including using formal written methods (columnar addition and subtraction)

Representation of Non Negative Integers

Number representations

1. Let n be a positive number. a. When we divide a decimal number, n, by 10, how are the numeral and the quotient related?

Topic Notes: Bits and Bytes and Numbers

Hash Tables (Cont'd) Carlos Moreno uwaterloo.ca EIT

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: Bits and Bytes and Numbers

6. Binary and Hexadecimal

1010 2?= ?= CS 64 Lecture 2 Data Representation. Decimal Numbers: Base 10. Reading: FLD Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Groups of two-state devices are used to represent data in a computer. In general, we say the states are either: high/low, on/off, 1/0,...

Computer Sc. & IT. Digital Logic. Computer Sciencee & Information Technology. 20 Rank under AIR 100. Postal Correspondence

Binary Adders: Half Adders and Full Adders

DLD VIDYA SAGAR P. potharajuvidyasagar.wordpress.com. Vignana Bharathi Institute of Technology UNIT 1 DLD P VIDYA SAGAR

TOPIC: NUMBER SYSTEMS

A complement number system is used to represent positive and negative integers. A complement number system is based on a fixed length representation

BINARY SYSTEM. Binary system is used in digital systems because it is:

1. NUMBER SYSTEMS USED IN COMPUTING: THE BINARY NUMBER SYSTEM

Number Systems and Binary Arithmetic. Quantitative Analysis II Professor Bob Orr

CS 31: Introduction to Computer Systems. 03: Binary Arithmetic January 29

Number Systems CHAPTER Positional Number Systems

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Euclid's Algorithm. MA/CSSE 473 Day 06. Student Questions Odd Pie Fight Euclid's algorithm (if there is time) extended Euclid's algorithm

Divisibility Rules and Their Explanations

Number System. Introduction. Natural Numbers (N) Whole Numbers (W) Integers (Z) Prime Numbers (P) Face Value. Place Value

Data Representation 1

N.B. These pastpapers may rely on the knowledge gained from the previous chapters.

COMP2611: Computer Organization. Data Representation

Internal Data Representation

Common Core State Standards Mathematics (Subset K-5 Counting and Cardinality, Operations and Algebraic Thinking, Number and Operations in Base 10)

MAT 243 Test 2 SOLUTIONS, FORM A

Chapter 5: Computer Arithmetic. In this chapter you will learn about:

Number Systems Using and Converting Between Decimal, Binary, Octal and Hexadecimal Number Systems

Number Systems (2.1.1)

THE LOGIC OF COMPOUND STATEMENTS

Arithmetic Operations

When using computers, it should have a minimum number of easily identifiable states.

Unit 7 Number System and Bases. 7.1 Number System. 7.2 Binary Numbers. 7.3 Adding and Subtracting Binary Numbers. 7.4 Multiplying Binary Numbers

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

4/8/17. Admin. Assignment 5 BINARY. David Kauchak CS 52 Spring 2017

Learning Objectives. Binary over Decimal. In this chapter you will learn about:

Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2

Chapter 3: Number Systems and Codes. Textbook: Petruzella, Frank D., Programmable Logic Controllers. McGraw Hill Companies Inc.

The type of all data used in a C++ program must be specified

1.1. INTRODUCTION 1.2. NUMBER SYSTEMS

MATH 104B OCTAL, BINARY, AND HEXADECIMALS NUMBERS

Signed umbers. Sign/Magnitude otation

Module 1: Information Representation I -- Number Systems

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING, THE UNIVERSITY OF NEW MEXICO ECE-238L: Computer Logic Design Fall 2013.

For more information, see the Math Notes box in Lesson of the Core Connections, Course 1 text.

Computer Arithmetic. In this article we look at the way in which numbers are represented in binary form and manipulated in a computer.

DIGITAL SYSTEM DESIGN

Number Systems. Both numbers are positive

9/23/15. Agenda. Goals of this Lecture. For Your Amusement. Number Systems and Number Representation. The Binary Number System

Hexadecimal Numbers. Journal: If you were to extend our numbering system to more digits, what digits would you use? Why those?

Digital Fundamentals

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

Level ISA3: Information Representation

The Design of C: A Rational Reconstruction"

NUMBER OPERATIONS. Mahdi Nazm Bojnordi. CS/ECE 3810: Computer Organization. Assistant Professor School of Computing University of Utah

Excerpt from: Stephen H. Unger, The Essence of Logic Circuits, Second Ed., Wiley, 1997

Lecture 3.4: Recursive Algorithms

Semester Transition Point. EE 109 Unit 11 Binary Arithmetic. Binary Arithmetic ARITHMETIC

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

Decimal Binary Conversion Decimal Binary Place Value = 13 (Base 10) becomes = 1101 (Base 2).

Data Representation Type of Data Representation Integers Bits Unsigned 2 s Comp Excess 7 Excess 8

Integers. N = sum (b i * 2 i ) where b i = 0 or 1. This is called unsigned binary representation. i = 31. i = 0

CS 64 Week 1 Lecture 1. Kyle Dewey

Binary Representation. Jerry Cain CS 106AJ October 29, 2018 slides courtesy of Eric Roberts

COMPETENCY 1.0 UNDERSTAND THE STRUCTURE OF THE BASE TEN NUMERATION SYSTEM AND NUMBER THEORY

Section A Arithmetic ( 5) Exercise A

Chapter 5: Computer Arithmetic

Princeton University Computer Science 217: Introduction to Programming Systems. Goals of this Lecture. Number Systems and Number Representation

Chapter 3: Arithmetic for Computers

Chapter 4. Operations on Data

Lecture 8: Addition, Multiplication & Division

Chapter 4 Arithmetic Functions

Chapter 2. Positional number systems. 2.1 Signed number representations Signed magnitude

Bits, Words, and Integers

Lecture 5: Arithmetic and Algebra Steven Skiena. skiena

Memory Addressing, Binary, and Hexadecimal Review

FUNDAMENTAL ARITHMETIC

Lecture 1: What is a computer?

Number Systems and Their Representations

Chapter 2 Exercises and Answers

Transcription:

Number Theory Algorithms Zeph Grunschlag Copyright Zeph Grunschlag, 2 22.

Agenda Euclidean Algorithm for GCD Number Systems Decimal numbers (base ) Binary numbers (base 2) One s complement Two s complement General base b number systems Arithmetic Algorithms Addition Multiplication Subtraction s and 2 s complement L 2

Euclidean Algorithm m, n Euclidean Algorithm gcd(m,n) integer euclid(pos. integer m, pos. integer n) = m, y = n while(y > ) r = mod y = y y = r return L 3

Euclidean Algorithm. Eample gcd(33,77): Step r = mod y y - 33 77 L 4

Euclidean Algorithm. Eample gcd(33,77): Step r = mod y y - 33 77 33 mod 77 = 33 77 33 L 5

Euclidean Algorithm. Eample gcd(33,77): Step r = mod y y - 33 77 33 mod 77 = 33 77 33 2 77 mod 33 = 33 L 6

Euclidean Algorithm. Eample gcd(33,77): Step r = mod y y - 33 77 2 3 33 mod 77 = 33 77 mod 33 = 33 mod = 77 33 33 L 7

Euclidean Algorithm. Eample gcd(244,7): Step r = mod y y - 244 7 L 8

Euclidean Algorithm. Eample gcd(244,7): Step r = mod y y - 244 7 244 mod 7 = 7 L 9

Euclidean Algorithm. Eample gcd(244,7): Step r = mod y y - 244 7 244 mod 7 = 7 2 7 mod = 7 7 L

gcd(244,7): Euclidean Algorithm. Eample Step r = mod y y - 244 7 244 mod 7 = 7 2 7 mod = 7 7 3 mod 7 = 3 7 3 L

gcd(244,7): Euclidean Algorithm. Eample Step r = mod y y - 244 7 244 mod 7 = 7 2 7 mod = 7 7 3 mod 7 = 3 7 3 4 7 mod 3 = 3 L 2

gcd(244,7): Euclidean Algorithm. Eample Step r = mod y y - 244 7 244 mod 7 = 7 2 7 mod = 7 7 3 mod 7 = 3 7 3 4 7 mod 3 = 3 5 3 mod = By definition 244 and 7 are rel. prime. L 3

Euclidean Algorithm Correctness The reason that Euclidean algorithm works is gcd(,y ) is not changed from line to line. If, y denote the net values of, y then: gcd(,y ) = gcd(y, mod y) = gcd(y, qy) (the useful fact) = gcd(y, ) (subtract y multiple) = gcd(,y) L 4

Euclidean Algorithm Running Time EX: Compute the asymptotic running time of the Euclidean algorithm in terms of the number of mod operations: L 5

Euclidean Algorithm Running Time Assuming mod operation is O (): integer euclid(m, n) = m, y = n while( y > ) r = mod y = y y = r return O () +? ( O () + O () + O () + O () ) + O () =? O() Where? is the number of while loop iterations. L 6

Euclidean Algorithm Running Time Facts: ( = net value of, etc. ) 2. can only be less than y at very beginning of algorithm once > y, = y > y = mod y 4. When > y, two iterations of while loop guarantee that new is < ½ original because = y = mod y. Two cases: I. y > ½ mod y = y < ½ II. y ½ mod y < y ½ L 7

Euclidean Algorithm Running Time (&2) After first iteration, size of decreases by factor > 2 every two iterations. I.e. after 2m+ iterations, < original_ / 2 m Q: When in terms of m does this process terminate? L 8

Euclidean Algorithm Running Time After 2m+ steps, < original_ / 2 m A: While loop eits when y is, which is right before would have gotten =. Eiting while loop happens when 2 m > original_, so definitely by: m = log 2 ( original_ ) Therefore running time of algorithm is: O(2m+) = O(m) = O (log 2 (ma(a,b)) ) L 9

Euclidean Algorithm Running Time Measuring input size in terms of n = number of digits of ma(a,b): n = Θ(log (ma(a,b)) ) = Θ(log 2 (ma(a,b)) ) Therefore running time of algorithm is: O(log 2 (ma(a,b)) ) = O(n) (assumed naively that mod is an O() operation, so estimate only holds for fied size integers such as int s and long s) L 2

Number Systems Q: What does the string of symbols 234 really mean as a number and why? L 2

Number Systems A: 2 thousands hundreds 3 tens and 4 = 2 3 + 2 + 3 + 4 But on the planet Ogg, the intelligent life forms have only one arm with 5 fingers. L 22

Number Systems So on Ogg, numbers are counted base 5. I.e. on Ogg 234 means: 2 5 3 + 5 2 + 3 5 + 4 5 To distinguish between these systems, subscripts are used: (234) for Earth (234) 5 for Ogg L 23

Number Systems DEF: A base b number is a string of symbols u = a k a k a k 2 a 2 a a With the a i in {,,2,3,,b 2,b }. The string u represents the number (u ) b = a k b k + a k b k +... + a b + a NOTE: When b >, run out of decimal number symbols so after 7, 8, 9 use capital letters, starting from A, B, C, L 24

Number Systems EG: base 2 (binary), base 8 (octal ) 74, 472 base 6 (headecimal ) 2F, ABCD Q: Compute the base version of these. L 25

Number Systems A: base 2 (binary), () 2 = 2 2 + 2 + 2 = 5 () 2 = (2 4 +2 3 +2 2 +2 ) + 2 = 2 base 8 (octal ) 74, 472 (74) 8 = 7 8 + 4 8 = 6 (472) 8 = 4 8 2 + 7 8 + 2 8 = 34 base 6 (headecimal ) 2F, ABCD (2F) 6 = 6 2 +2 6 +5 6 = 33 (ABCD) 6 = 6 3 + 6 2 +2 6 +3 6 L 26

Number Systems Binary most natural system for bit strings and headecimal compactifies byte strings ( byte = 2 headecimals) EG in HTML: <font color="ffff"> Nice Color </font> Q: What color will this become? L 27

Number Systems A: "ffff" represents the rgb value: The first byte is for redness, the second byte is for green ness, and the last for blue ness. The HTML above specifies that 5 6 + 5 = 255 redness and blueness values, but 6 + = green ness. Red and blue give purple, and 255 is the top brightness so this is bright purple. L 28

Number Systems Reverse Conversion Convert arbitrary decimal numbers into various bases, (calculator functions typically limited to base 2, 8, 6 and ). EG: Back at Ogg. Convert 646 to base 5. Try to do all operations base 5. L 29

Number Systems Reverse Conversion Back at Ogg. Convert 646 to base 5. Try to do all operations as an Oggian (base 5): (646) = (6) () 2 + (4) () + (6) Each quantity easy to convert into base 5: (6) =() 5 since 6 = 5 + (4) =(4) 5 since 4 < 5 () =(2) 5 since = 2 5 + So convert whole epression and do Oggian arithmetic: L 3

Number Systems Reverse Conversion Back at Ogg. Convert 646 to base 5. Try to do all operations as an Oggian (base 5): (646) = () 5 (2) 5 2 + (4) 5 (2) 5 + () 5 = L 3

Number Systems Reverse Conversion Back at Ogg. Convert 646 to base 5. Try to do all operations as an Oggian (base 5): (646) = () 5 (2) 5 2 + (4) 5 (2) 5 + () 5 = () 5 (4) 5 + (3) 5 + () 5 = L 32

Number Systems Reverse Conversion Back at Ogg. Convert 646 to base 5. Try to do all operations as an Oggian (base 5): (646) = () 5 (2) 5 2 + (4) 5 (2) 5 + () 5 = () 5 (4) 5 + (3) 5 + () 5 = (44) 5 + (4) 5 = L 33

Number Systems Reverse Conversion Back at Ogg. Convert 646 to base 5. Try to do all operations as an Oggian (base 5): (646) = () 5 (2) 5 2 + (4) 5 (2) 5 + () 5 = () 5 (4) 5 + (3) 5 + () 5 = (44) 5 + (4) 5 = (4) 5 Thinking like an Oggian hurts brain too much L 34

Number Systems Reverse Conversion Given an integer n and a base b find the string u such that (u ) b = n. Pseudocode: string represent(pos. integer n, pos. integer b) q = n, i = while( q > ) u i = q mod b q = q/b i = i + return u i u i u i 2 u 2 u u L 35

Number Systems Reverse Conversion EG: Convert 646 to Oggian (base 5): i u i = q mod b - q = q/b 646 L 36

Number Systems Reverse Conversion EG: Convert 646 to Oggian (base 5): i u i = q mod b - 646 mod 5 = q = q/b 646 646/5 =29 L 37

Number Systems Reverse Conversion EG: Convert 646 to Oggian (base 5): i u i = q mod b - 646 mod 5 = 29 mod 5 = 4 q = q/b 646 646/5 =29 29/5 =25 L 38

Number Systems Reverse Conversion EG: Convert 646 to Oggian (base 5): i u i = q mod b - 646 mod 5 = 29 mod 5 = 4 2 25 mod 5 = q = q/b 646 646/5 =29 29/5 =25 25/5 =5 L 39

Number Systems Reverse Conversion EG: Convert 646 to Oggian (base 5): i u i = q mod b - 646 mod 5 = 29 mod 5 = 4 2 25 mod 5 = 3 5 mod 5 = q = q/b 646 646/5 =29 29/5 =25 25/5 =5 5/5 = L 4

Number Systems Reverse Conversion EG: Convert 646 to Oggian (base 5): i 4 u i = q mod b - 646 mod 5 = 29 mod 5 = 4 2 25 mod 5 = 3 5 mod 5 = mod 5 = Reading last column in reverse: 4 q = q/b 646 646/5 =29 29/5 =25 25/5 =5 5/5 = /5 = L 4

Number Systems In Class Eercise Some number theory facts are base dependent. For eample First Grade Teacher s Rule: A base number is divisible by 3 iff the sum of its digits are. Formally, let n = (u k u k u k 2 u 2 u u ). Then: n mod 3= u i mod 3 i= EG: 3 235 because 3 (+2++3+5 = 2) k L 42

Arithmetical Algorithms Let s write down some familiar arithmetical algorithms. Conveniently, they run the same in any number base. In some cases, such as addition, there are asymptotically faster approaches, but these are the simplest procedures and tend to be fastest for relatively small (e.g. < bits) number sizes. L 43

Arithmetical Algorithms Addition Numbers are added from least significant digit to most, while carrying any overflow resulting from adding a column: base base 6 Carry: 7 4 6 3 A 4 F + y + 2 9 9 + C B 9 L 44

Arithmetical Algorithms Addition Numbers are added from least significant digit to most, while carrying any overflow resulting from adding a column: Carry: + y base base 6 7 4 6 3 A 4 F + 2 9 9 + C B 9 2 L 45 9

Arithmetical Algorithms Addition Numbers are added from least significant digit to most, while carrying any overflow resulting from adding a column: Carry: + y base base 6 7 4 6 3 A 4 F + 2 9 9 + C B 9 7 2 L 46 F 9

Arithmetical Algorithms Addition Numbers are added from least significant digit to most, while carrying any overflow resulting from adding a column: Carry: + y base base 6 7 4 6 3 A 4 F + 2 9 9 + C B 9 3 7 2 L 47 F F 9

Arithmetical Algorithms Addition Numbers are added from least significant digit to most, while carrying any overflow resulting from adding a column: Carry: + y 7 4 6 3 A 4 F + 2 9 9 + C B 9 base base 6 3 7 2 L 48 6 F F 9

Arithmetical Algorithms Addition Numbers are added from least significant digit to most, while carrying any overflow resulting from adding a column: Carry: + y 7 4 6 3 A 4 F + 2 9 9 + C B 9 base base 6 3 7 2 L 49 6 F F 9

Arithmetical Algorithms Addition of Positive Numbers string add(strings k k, y k y k y y, int base) carry =, k+ = y k+ = for(i = to k+) digitsum = carry + i + y i z i = digitsum mod base carry = digitsum /base return z k+ z k z k z z L 5

s Complement 2 s Complement The binary number system makes some operations especially simple and efficient under certain representations. Two such representations are s complement 2 s complement Each makes subtraction much simpler. Each has disadvantage that number length is predetermined. L 5

s Complement Fi k bits. (EG, k = 8 for bytes) Represent numbers with < 2 k Left most bit tells the sign positive (so positive no. s as usual) negative (but other bits change too!) Positive numbers the same as standard binary epansion Negative numbers gotten by taking the boolean complement, hence nomenclature L 52

s Complement Eamples k = 8: represents 8 represents 8 Notice: when add these representations as usual get, i.e. negative or =. Guess: adding numbers with mied sign works the same as adding positive numbers Trade off: not unique L 53

s Complement Addition Addition is the same as usual binary addition ecept: if the final carry is, cycle the carry to the least significant digit: represents 8, represents 2 Sum represents 6: Carry: +y pre sum overflow answer L 54

s Complement Addition Addition is the same as usual binary addition ecept: if the final carry is, cycle the carry to the least significant digit: represents 8, represents 2 Sum represents 6: Carry: +y pre sum overflow answer L 55

s Complement Addition Addition is the same as usual binary addition ecept: if the final carry is, cycle the carry to the least significant digit: represents 8, represents 2 Sum represents 6: Carry: +y pre sum overflow answer L 56

s Complement Addition Addition is the same as usual binary addition ecept: if the final carry is, cycle the carry to the least significant digit: represents 8, represents 2 Sum represents 6: Carry: +y pre sum overflow answer L 57

s Complement Addition Addition is the same as usual binary addition ecept: if the final carry is, cycle the carry to the least significant digit: represents 8, represents 2 Sum represents 6: Carry: +y pre sum overflow answer L 58

s Complement Addition Addition is the same as usual binary addition ecept: if the final carry is, cycle the carry to the least significant digit: represents 8, represents 2 Sum represents 6: Carry: +y pre sum overflow answer L 59

s Complement Addition Addition is the same as usual binary addition ecept: if the final carry is, cycle the carry to the least significant digit: represents 8, represents 2 Sum represents 6: Carry: +y pre sum overflow answer L 6

s Complement Addition Addition is the same as usual binary addition ecept: if the final carry is, cycle the carry to the least significant digit: represents 8, represents 2 Sum represents 6: Carry: +y pre sum overflow answer L 6

s Complement Addition Addition is the same as usual binary addition ecept: if the final carry is, cycle the carry to the least significant digit: represents 8, represents 2 Sum represents 6: Carry: +y pre sum overflow answer L 62

s Complement Addition Addition is the same as usual binary addition ecept: if the final carry is, cycle the carry to the least significant digit: represents 8, represents 2 Sum represents 6: Carry: +y pre sum overflow answer L 63

s Complement Addition Addition is the same as usual binary addition ecept: if the final carry is, cycle the carry to the least significant digit: represents 8, represents 2 Sum represents 6: Carry: +y pre sum overflow answer L 64

s Complement Addition Addition is the same as usual binary addition ecept: if the final carry is, cycle the carry to the least significant digit: represents 8, represents 2 Sum represents 6: Carry: +y pre sum overflow answer L 65

s Complement Addition Addition is the same as usual binary addition ecept: if the final carry is, cycle the carry to the least significant digit: represents 8, represents 2 Sum represents 6: Carry: +y pre sum overflow answer L 66

s Complement Addition Addition is the same as usual binary addition ecept: if the final carry is, cycle the carry to the least significant digit: represents 8, represents 2 Sum represents 6: Carry: +y pre sum overflow answer L 67

2 s Complement Fies the non uniqueness of zero problem Adding mied signs still easy No cycle overflow (pre computed) Java s approach (under the hood) Same fied length k, sign convention, and definition of positive numbers as with s complement Represent numbers with 2 (k ) < 2 (k ) EG. Java s byte ranges from 28 to +27 L 68

2 s Complement Negatives (slightly harder than s comp.): Compute s complement Add Summarize: = +. represents 8 + = represents 8. Add together without over flow: Q: What are the ranges of Java s 32 bit int and 64 bit long? (All of Java s integer types use 2 s complement) L 69

2 s Complement A: 2) 32 bit int s: Largest int =. = 2 3 = 2,47,483,647 Smallest int =. = 2 3 = 2,47,483,648 2) 64 bit long s: Largest long =. = 2 63 = 9,223,372,36,854,775,87 Smallest int =. = 2 63 = 9,223,372,36,854,775,88 L 7

2 s Complement Addition Addition is the same as usual binary addition no eceptions!! = ( 8), = ( 2) Sum together () = ( 3) : Carry: +y L 7

2 s Complement Addition Addition is the same as usual binary addition no eceptions!! = ( 8), = ( 2) Sum together () = ( 3) : Carry: +y L 72

2 s Complement Addition Addition is the same as usual binary addition no eceptions!! = ( 8), = ( 2) Sum together () = ( 3) : Carry: +y L 73

2 s Complement Addition Addition is the same as usual binary addition no eceptions!! = ( 8), = ( 2) Sum together () = ( 3) : Carry: +y L 74

2 s Complement Addition Addition is the same as usual binary addition no eceptions!! = ( 8), = ( 2) Sum together () = ( 3) : Carry: +y L 75

2 s Complement Addition Addition is the same as usual binary addition no eceptions!! = ( 8), = ( 2) Sum together () = ( 3) : Carry: +y L 76

2 s Complement Addition Addition is the same as usual binary addition no eceptions!! = ( 8), = ( 2) Sum together () = ( 3) : Carry: +y L 77

2 s Complement Addition Addition is the same as usual binary addition no eceptions!! = ( 8), = ( 2) Sum together () = ( 3) : Carry: +y L 78

2 s Complement Addition Addition is the same as usual binary addition no eceptions!! = ( 8), = ( 2) Sum together () = ( 3) : Carry: +y L 79

2 s Complement Addition Addition is the same as usual binary addition no eceptions!! = ( 8), = ( 2) Sum together () = ( 3) : Carry: +y As a final check take the negative to see if get 3: ( +) = (+) =. YES! L 8

Arithmetical Algorithms Positive Binary Multiplication Long multiplication simplifies in binary because multiplying by 2 k amounts to left shifting k places (<<k), and each time multiply either by 2 k or 2 k. EG: y (<<) (<<) (<<2) (<<3) Add rows: L 8

Arithmetical Algorithms Positive Binary Multiplication Long multiplication simplifies in binary because multiplying by 2 k amounts to left shifting k places (<<k), and each time multiply either by 2 k or 2 k. EG: y (<<) (<<) (<<2) (<<3) Add rows: L 82

Arithmetical Algorithms Positive Binary Multiplication Long multiplication simplifies in binary because multiplying by 2 k amounts to left shifting k places (<<k), and each time multiply either by 2 k or 2 k. EG: y (<<) (<<) (<<2) (<<3) Add rows: L 83

Arithmetical Algorithms Positive Binary Multiplication Long multiplication simplifies in binary because multiplying by 2 k amounts to left shifting k places (<<k), and each time multiply either by 2 k or 2 k. EG: y (<<) (<<) (<<2) (<<3) Add rows: L 84

Arithmetical Algorithms Positive Binary Multiplication Long multiplication simplifies in binary because multiplying by 2 k amounts to left shifting k places (<<k), and each time multiply either by 2 k or 2 k. EG: y (<<) (<<) (<<2) (<<3) Add rows: L 85

Arithmetical Algorithms Positive Binary Multiplication Long multiplication simplifies in binary because multiplying by 2 k amounts to left shifting k places (<<k), and each time multiply either by 2 k or 2 k. EG: y (<<) (<<) (<<2) (<<3) Add rows: L 86

Arithmetical Algorithms Binary Multiplication bitstring multiply(bitstrings k k, y k y k y y ) = k k p = // the partial product for(i = to k+) if(y i == ) return p p = add(p, << i ) // prev. algorithm L 87