資訊科學數學 9 : Integers, Algorithms & Orders 陳光琦助理教授 (Kuang-Chi Chen)

Slides:



Advertisements
Similar presentations
Study Guides Quantitative - Arithmetic - Numbers, Divisibility Test, HCF and LCM Mycatstudy.com.
Advertisements

February 19, 2015Applied Discrete Mathematics Week 4: Number Theory 1 The Growth of Functions Question: If f(x) is O(x 2 ), is it also O(x 3 )? Yes. x.
Number Theory and Cryptography
Chapter 3 Growth of Functions
Level ISA3: Information Representation
Assembly Language and Computer Architecture Using C++ and Java
1 Section 2.5 Integers and Algorithms. 2 Euclidean algorithm for finding gcd Where L is a larger number, and S is a smaller number, to find gcd(L,S):
Fall 2002CMSC Discrete Structures1 Let us get into… Number Theory.
Dr. Bernard Chen Ph.D. University of Central Arkansas
Integers Number Theory = Properties of Integers
CSCI 1900 Discrete Structures
Chapter 2 The Fundamentals: Algorithms, the Integers, and Matrices
9/2/2015Discrete Structures1 Let us get into… Number Theory.
Module #8: Basic Number Theory
Slide 5-1 Copyright © 2005 Pearson Education, Inc. SEVENTH EDITION and EXPANDED SEVENTH EDITION.
Quantitative - Arithmetic - Numbers, Divisibility Test, HCF and LCM
Lecture for Week Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday.
Numbering systems.
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 3 (Part 3): The Fundamentals: Algorithms, the.
Mathematics of Cryptography Part I: Modular Arithmetic
February 24, 2015Applied Discrete Mathematics Week 4: Number Theory 1 Modular Arithmetic Let a be an integer and m be a positive integer. We denote by.
Copyright © Cengage Learning. All rights reserved. CHAPTER 2 THE LOGIC OF COMPOUND STATEMENTS THE LOGIC OF COMPOUND STATEMENTS.
3.4/3.5 The Integers and Division/ Primes and Greatest Common Divisors Let each of a and b be integers. We say that a divides b, in symbols a | b, provided.
MATH 224 – Discrete Mathematics
Information Representation. Digital Hardware Systems Digital Systems Digital vs. Analog Waveforms Analog: values vary over a broad range continuously.
CompSci 102 Discrete Math for Computer Science February 16, 2012 Prof. Rodger.
Computer Arithmetic and the Arithmetic Unit Lesson 2 - Ioan Despi.
Chapter 2: The Logic of Compound Statements 2.5 Application: Number Systems and Circuits for Addition 1 Counting in binary is just like counting in decimal.
The Integers. The Division Algorithms A high-school question: Compute 58/17. We can write 58 as 58 = 3 (17) + 7 This forms illustrates the answer: “3.
P.1 Real Numbers. 2 What You Should Learn Represent and classify real numbers. Order real numbers and use inequalities. Find the absolute values of real.
Topic 1 – Number Systems. What is a Number System? A number system consists of an ordered set of symbols (digits) with relations defined for addition,
Data Security and Encryption (CSE348) 1. Lecture # 12 2.
Copyright © 2009 Pearson Education, Inc. Chapter 5 Section 1 - Slide 1 Chapter 1 Number Theory and the Real Number System.
Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 1/37 Module.
Foundations of Discrete Mathematics Chapter 4 By Dr. Dalia M. Gil, Ph.D.
The Fundamentals. Algorithms What is an algorithm? An algorithm is “a finite set of precise instructions for performing a computation or for solving.
이산수학 (Discrete Mathematics) 2.5 정수와 알고리즘 (Integers and Algorithms) 2006 년 봄학기 문양세 강원대학교 컴퓨터과학과.
Module #9 – Number Theory 1/5/ Algorithms, The Integers and Matrices.
Greatest Common Divisors & Least Common Multiples  Definition 4 Let a and b be integers, not both zero. The largest integer d such that d|a and d|b is.
Analysis of Algorithms & Recurrence Relations. Recursive Algorithms Definition –An algorithm that calls itself Components of a recursive algorithm 1.Base.
Lecture 7. Asymptotic definitions for the analysis of algorithms 1.
Ch04-Number Theory and Cryptography 1. Introduction to Number Theory Number theory is about integers and their properties. We will start with the basic.
Chapter 3 Chapter Summary  Algorithms o Example Algorithms searching for an element in a list sorting a list so its elements are in some prescribed.
Number Theory Lecture 1 Text book: Discrete Mathematics and its Applications, 7 th Edition.
Slide Copyright © 2009 Pearson Education, Inc. Slide Copyright © 2009 Pearson Education, Inc. Chapter 1 Number Theory and the Real Number System.
Discrete Mathematics Chapter 2 The Fundamentals : Algorithms, the Integers, and Matrices. 大葉大學 資訊工程系 黃鈴玲.
Fuw-Yi Yang1 Textbook: Introduction to Cryptography 2nd ed. By J.A. Buchmann Chap 1 Integers Department of Computer Science and Information Engineering,
Agenda Review:  Relation Properties Lecture Content:  Divisor and Prime Number  Binary, Octal, Hexadecimal Review & Exercise.
Number Theory. Introduction to Number Theory Number theory is about integers and their properties. We will start with the basic principles of divisibility,
Analysis of Algorithms & Orders of Growth
CMSC Discrete Structures
Number Theory and Cryptography
Applied Discrete Mathematics Week 4: Number Theory
Module #8: Basic Number Theory
Foundations of Discrete Mathematics
Enough Mathematical Appetizers!
Discrete Mathematics and its Applications
Module #8: Basic Number Theory
Orders of Growth Rosen 5th ed., §2.2.
Module #6: Orders of Growth
Discrete Mathematics and its Applications
Copyright © Cengage Learning. All rights reserved.
Divisibility and Modular Arithmetic
Discrete Mathematics and its Applications
이산수학(Discrete Mathematics)  함수의 증가 (Growth of Functions)
이산수학(Discrete Mathematics)  함수의 증가 (Growth of Functions)
2.2 함수의 증가 (Growth of Functions)
이산수학(Discrete Mathematics)  함수의 증가 (Growth of Functions)
이산수학(Discrete Mathematics)  함수의 증가 (Growth of Functions)
Presentation transcript:

資訊科學數學 9 : Integers, Algorithms & Orders 陳光琦助理教授 (Kuang-Chi Chen)

Mini Review Methods

Useful Congruence Theorems Theorem 1: Let a, b  Z, m  Z +. Then: a  b (mod m)   k  Z a=b+km. Theorem 2: Let a, b, c, d  Z, m  Z +. Then if a  b (mod m) and c  d (mod m), then:. a+c  b+d (mod m), and. a+c  b+d (mod m), and. ac  bd (mod m). ac  bd (mod m)

Integers and Algorithms

Integers & Algorithms Topics: Euclidean algorithm for finding GCD’s. Euclidean algorithm for finding GCD’s. Base-b representations of integers. Base-b representations of integers. - Especially: binary, hexadecimal, octal. - Especially: binary, hexadecimal, octal. - Also: Two’s complement representation of negative numbers. - Also: Two’s complement representation of negative numbers. Algorithms for computer arithmetic: Algorithms for computer arithmetic: - Binary addition, multiplication, division. - Binary addition, multiplication, division.

Euclid ’ s Algorithm for GCD Finding GCDs by comparing prime factorizations can be difficult when the prime factors are not known! Finding GCDs by comparing prime factorizations can be difficult when the prime factors are not known! Euclid discovered: For all ints. a, b, gcd(a, b) = gcd((a mod b), b). Euclid discovered: For all ints. a, b, gcd(a, b) = gcd((a mod b), b). Sort a, b so that a>b, and then (given b>1) (a mod b) b, and then (given b>1) (a mod b) < a, so problem is simplified. Euclid B.C.

Euclid ’ s Algorithm Example gcd(372,164) = gcd(372 mod 164, 164). gcd(372,164) = gcd(372 mod 164, 164) mod 164 = 372  164[372/164] = 372  164·2 = 372  328 = mod 164 = 372  164[372/164] = 372  164·2 = 372  328 = 44. gcd(164, 44) = gcd(164 mod 44, 44). gcd(164, 44) = gcd(164 mod 44, 44) mod 44 = 164  44[164/44] = 164  44·3 = 164  132 = mod 44 = 164  44[164/44] = 164  44·3 = 164  132 = 32. gcd(44, 32) = gcd(44 mod 32, 32) = gcd(12, 32) = gcd(32 mod 12, 12) = gcd(8, 12) = gcd(12 mod 8, 8) = gcd(4, 8) = gcd(8 mod 4, 4) = gcd(0, 4) = 4. gcd(44, 32) = gcd(44 mod 32, 32) = gcd(12, 32) = gcd(32 mod 12, 12) = gcd(8, 12) = gcd(12 mod 8, 8) = gcd(4, 8) = gcd(8 mod 4, 4) = gcd(0, 4) = 4.

Euclid ’ s Algorithm Pseudocode procedure gcd(a, b: positive integers) while b  0 begin r ≔ a mod b; a ≔ b; b ≔ r; end return a Fast! Number of while loop iterations turns out to be O(log(max(a,b))). Sorting inputs not needed b/c order will be reversed each iteration.

Base-b Number Systems Ordinarily, we write base-10 representations of numbers, using digits 0-9. Ordinarily, we write base-10 representations of numbers, using digits 0-9. But, 10 isn’t special! Any base b>1 will work. But, 10 isn’t special! Any base b>1 will work. For any positive integers n,b, there is a unique sequence a k a k-1 … a 1 a 0 of digits a i <b such that: For any positive integers n,b, there is a unique sequence a k a k-1 … a 1 a 0 of digits a i <b such that: The “base b expansion of n”

Particular Bases of Interest Base b=10 (decimal): 10 digits: 0,1,2,3,4,5,6,7,8,9. Base b=10 (decimal): 10 digits: 0,1,2,3,4,5,6,7,8,9. Base b=2 (binary): 2 digits: 0,1. (“Bits”=“binary digits.”) Base b=2 (binary): 2 digits: 0,1. (“Bits”=“binary digits.”) Base b=8 (octal): 8 digits: 0,1,2,3,4,5,6,7. Base b=8 (octal): 8 digits: 0,1,2,3,4,5,6,7. Base b=16 (hexadecimal): 16 digits: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F Base b=16 (hexadecimal): 16 digits: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F Used internally in all modern computers Hex digits give groups of 4 bits Used only because we have 10 fingers Octal digits correspond to groups of 3 bits

Converting to Base b (An algorithm, informally stated.) To convert any integer n to any base b>1: To convert any integer n to any base b>1: 1. To find the value of the rightmost (lowest- order) digit, simply compute n mod b. 2. Now, replace n with the quotient [n/b]. 3. Repeat above two steps to find subsequent digits, until n is gone (=0).

Orders of Growth

For functions over numbers, we often need to know a rough measure of how fast a function grows. For functions over numbers, we often need to know a rough measure of how fast a function grows. If f(x) is faster growing than g(x), then f(x) always eventually becomes larger than g(x) in the limit (for large enough values of x). If f(x) is faster growing than g(x), then f(x) always eventually becomes larger than g(x) in the limit (for large enough values of x). Useful in engineering for showing that one design scales better or worse than another. Useful in engineering for showing that one design scales better or worse than another.

Orders of Growth - Motivation Suppose you are designing a web site to process user data (e.g., financial records). Suppose you are designing a web site to process user data (e.g., financial records). Suppose database program A takes f A (n)=30n+8 microseconds to process any n records, while program B takes f B (n)=n 2 +1 microseconds to process the n records. Suppose database program A takes f A (n)=30n+8 microseconds to process any n records, while program B takes f B (n)=n 2 +1 microseconds to process the n records. Q: Which program do you choose, knowing you’ll want to support millions of users?

Visualizing Orders of Growth On a graph, as you go to the right, the faster- growing function always eventually becomes the larger one... On a graph, as you go to the right, the faster- growing function always eventually becomes the larger one... f A (n)=30n+8 Increasing n  f B (n)=n 2 +1 Value of function 

Concept of Order of Growth We say f A (n) = 30n+8 is (at most) order n, or O(n). We say f A (n) = 30n+8 is (at most) order n, or O(n). - It is, at most, roughly proportional to n. - It is, at most, roughly proportional to n. f B (n) = n 2 +1 is order n 2, or O(n 2 ). f B (n) = n 2 +1 is order n 2, or O(n 2 ). - It is (at most) roughly proportional to n 2. - It is (at most) roughly proportional to n 2. Any function whose exact (tightest) order is O(n 2 ) is faster-growing than any O(n) function. Any function whose exact (tightest) order is O(n 2 ) is faster-growing than any O(n) function. - Later we’ll introduce Θ for expressing exact order. - Later we’ll introduce Θ for expressing exact order. For large numbers of user records, the exactly order n 2 function will always take more time. For large numbers of user records, the exactly order n 2 function will always take more time.

Definition: O(g), at most order g Let g be any function R  R. Define “at most order g”, written O(g), to be: {f: R  R |  c, k:  x>k: f(x)  cg(x)}. Define “at most order g”, written O(g), to be: {f: R  R |  c, k:  x>k: f(x)  cg(x)}. - “Beyond some point k, function f is at most a constant c times g (i.e., proportional to g).” - “Beyond some point k, function f is at most a constant c times g (i.e., proportional to g).” “f is at most order g”, or “f is O(g)”, or “f=O(g)” all just mean that f  O(g). “f is at most order g”, or “f is O(g)”, or “f=O(g)” all just mean that f  O(g). (Often the phrase “at most” is omitted.) (Often the phrase “at most” is omitted.)

Points about The Definition Note that f is O(g) so long as any values of c and k exist that satisfy the definition. Note that f is O(g) so long as any values of c and k exist that satisfy the definition. But, the particular c, k, values that make the statement true are not unique: Any larger value of c and/or k will also work. But, the particular c, k, values that make the statement true are not unique: Any larger value of c and/or k will also work. No need to find the smallest c and k values that work. (Indeed, in some cases, there may be no smallest values!) No need to find the smallest c and k values that work. (Indeed, in some cases, there may be no smallest values!) However, you should prove that the values you choose do work.

“ Big-O ” Proof Examples Show that 30n+8 is O(n). Show that 30n+8 is O(n). Show  c, k:  n>k: 30n+8  cn. Show  c, k:  n>k: 30n+8  cn. Let c=31, k=8. Assume n>k=8. Then cn = 31n = 30n + n > 30n+8, so 30n+8 k=8. Then cn = 31n = 30n + n > 30n+8, so 30n+8 < cn. Show that n 2 +1 is O(n 2 ). Show that n 2 +1 is O(n 2 ). Show  c, k:  n>k: n 2 +1  cn 2. Show  c, k:  n>k: n 2 +1  cn 2. Let c=2, k=1. Assume n>1. Then cn 2 = 2n 2 = n 2 +n 2 > n 2 +1, or n Then cn 2 = 2n 2 = n 2 +n 2 > n 2 +1, or n 2 +1< cn 2.

Note 30n+8 isn’t less than n anywhere (n>0). Note 30n+8 isn’t less than n anywhere (n>0). It isn’t even less than 31n everywhere. It isn’t even less than 31n everywhere. But it is less than 31n everywhere to the right of n=8. But it is less than 31n everywhere to the right of n=8. n>k=8  Big-O Example, Graphically Increasing n  Value of function  n 30n+8 cn = 31n 30n+8  O(n)

Useful Facts about Big O Big O, as a relation, is transitive: f  O(g)  g  O(h)  f  O(h) Big O, as a relation, is transitive: f  O(g)  g  O(h)  f  O(h) O with constant multiples, roots, and logs...  f (in  (1)) & constants a, b  R, with b  0, af, f 1-b, and (log b f) a are all O(f). O with constant multiples, roots, and logs...  f (in  (1)) & constants a, b  R, with b  0, af, f 1-b, and (log b f) a are all O(f). Sums of functions: If g  O(f) and h  O(f), then g+h  O(f). Sums of functions: If g  O(f) and h  O(f), then g+h  O(f).

More Big-O Facts  c>0, O(cf) = O(f+c) = O(f  c) = O(f)  c>0, O(cf) = O(f+c) = O(f  c) = O(f) f 1  O(g 1 )  f 2  O(g 2 )  f 1  O(g 1 )  f 2  O(g 2 )  - f 1 f 2  O(g 1 g 2 ) - f 1 f 2  O(g 1 g 2 ) - f 1 +f 2  O(g 1 +g 2 ) = O(max(g 1, g 2 )) - f 1 +f 2  O(g 1 +g 2 ) = O(max(g 1, g 2 )) = O(g 1 ) if g 2  O(g 1 ) (Very useful!) = O(g 1 ) if g 2  O(g 1 ) (Very useful!)

Orders of Growth - So Far For any g: R  R, “at most order g”, O(g)  {f: R  R |  c, k  x>k |f(x)|  |cg(x)|}. For any g: R  R, “at most order g”, O(g)  {f: R  R |  c, k  x>k |f(x)|  |cg(x)|}. - Often, one deals only with positive functions and can ignore absolute value symbols. - Often, one deals only with positive functions and can ignore absolute value symbols. “f  O(g)” often written “f is O(g)” or “f = O(g)”. “f  O(g)” often written “f is O(g)” or “f = O(g)”. - The latter form is an instance of a more general convention... - The latter form is an instance of a more general convention...

Order-of-Growth Expressions “O(f)” when used as a term in an arithmetic expression means: “some function f such that f  O(f)”. “O(f)” when used as a term in an arithmetic expression means: “some function f such that f  O(f)”. E.g., “x 2 +O(x)” means “x 2 plus some function that is O(x)”. Formally, we can think of any such expression as denoting a set of functions: x 2 +O(x) :  {g |  f  O(x): g(x) = x 2 +f(x)} Formally, we can think of any such expression as denoting a set of functions: x 2 +O(x) :  {g |  f  O(x): g(x) = x 2 +f(x)}

Order of Growth Equations Suppose E 1 and E 2 are order-of-growth expressions corresponding to the sets of functions S and T, respectively. Suppose E 1 and E 2 are order-of-growth expressions corresponding to the sets of functions S and T, respectively. Then the “equation” E 1 =E 2 really means  f  S,  g  T : f = g or simply S  T. Then the “equation” E 1 =E 2 really means  f  S,  g  T : f = g or simply S  T. Example: x 2 + O(x) = O(x 2 ) means  f  O(x):  g  O(x 2 ): x 2 +f(x) = g(x)

Useful Facts about Big O  f, g & constants a, b  R, with b  0,  f, g & constants a, b  R, with b  0, - af = O(f) (e.g. 3x 2 = O(x 2 )) - af = O(f) (e.g. 3x 2 = O(x 2 )) - f+O(f) = O(f) (e.g. x 2 +x = O(x 2 )) - f+O(f) = O(f) (e.g. x 2 +x = O(x 2 )) Also, if f =  (1) (at least order 1), then: Also, if f =  (1) (at least order 1), then: - |f| 1-b = O(f) (e.g. x  1 = O(x)) - |f| 1-b = O(f) (e.g. x  1 = O(x)) - (log b |f|) a = O(f) (e.g. log x = O(x)) - (log b |f|) a = O(f) (e.g. log x = O(x)) - g = O(fg) (e.g. x = O(x log x)) - g = O(fg) (e.g. x = O(x log x)) - fg  O(g) (e.g. x log x  O(x)) - fg  O(g) (e.g. x log x  O(x)) - a = O(f) (e.g. 3 = O(x)) - a = O(f) (e.g. 3 = O(x))

Definition:  (g), exactly order g If f  O(g) and g  O(f), then we say “g and f are of the same order” or “f is (exactly) order g” and write f  (g). If f  O(g) and g  O(f), then we say “g and f are of the same order” or “f is (exactly) order g” and write f  (g). Another, equivalent definition:  (g)  {f:R  R |  c 1 c 2 k>0  x>k: |c 1 g(x)|  |f(x)|  |c 2 g(x)| } Another, equivalent definition:  (g)  {f:R  R |  c 1 c 2 k>0  x>k: |c 1 g(x)|  |f(x)|  |c 2 g(x)| } - “Everywhere beyond some point k, f(x) lies in between two multiples of g(x).” - “Everywhere beyond some point k, f(x) lies in between two multiples of g(x).”

Rules for  Mostly like rules for O( ), except: Mostly like rules for O( ), except:  f,g>0 & constants a,b  R, with b>0, af   (f), but  Same as with O. f   (fg) unless g=  (1)  Unlike O. |f| 1-b   (f), and  Unlike with O. (log b |f|) c   (f).  Unlike with O.  f,g>0 & constants a,b  R, with b>0, af   (f), but  Same as with O. f   (fg) unless g=  (1)  Unlike O. |f| 1-b   (f), and  Unlike with O. (log b |f|) c   (f).  Unlike with O. The functions in the latter two cases we say are strictly of lower order than  (f). The functions in the latter two cases we say are strictly of lower order than  (f).

 Example Determine whether: Determine whether: Quick solution: Quick solution:

Other Order-of-Growth Relations  (g) = {f | g  O(f)} “The functions that are at least order g.”  (g) = {f | g  O(f)} “The functions that are at least order g.” o(g) = {f |  c>0  k  x>k : |f(x)| 0  k  x>k : |f(x)| < |cg(x)|} “The functions that are strictly lower order than g.” o(g)  O(g)   (g).  (g) = {f |  c>0  k  x>k : |cg(x)| 0  k  x>k : |cg(x)| < |f(x)|} “The functions that are strictly higher order than g.”  (g)   (g)   (g).

Subset relations between order-of-growth sets. Subset relations between order-of-growth sets. Relations Between the Relations RRRR  ( f ) O( f )  ( f )  ( f ) o( f ) f

Why o(f)  O(x)  (x) A function that is O(x), but neither o(x) nor  (x): A function that is O(x), but neither o(x) nor  (x):

Strict Ordering of Functions Temporarily let’s write f  g to mean f  o(g), f~g to mean f  (g) Temporarily let’s write f  g to mean f  o(g), f~g to mean f  (g) Note that: Note that: Let k>1. Then the following are true: 1  log log n  log n ~ log k n  log k n  n 1/k  n  n log n  n k  k n  n!  n n … Let k>1. Then the following are true: 1  log log n  log n ~ log k n  log k n  n 1/k  n  n log n  n k  k n  n!  n n …

Review: Orders of Growth Definitions of order-of-growth sets,  g:R  R O(g) :  {f |  c>0  k  x>k |f(x)| 0  k  x>k |f(x)| < |cg(x)|} o(g) :  {f |  c>0  k  x>k |f(x)| 0  k  x>k |f(x)| < |cg(x)|}  (g) :  {f | g  O(f)}  (g) :  {f | g  O(f)}  (g) :  {f | g  o(f)}  (g) :  {f | g  o(f)}  (g) :  O(g)   (g)  (g) :  O(g)   (g)

Addition of Binary Numbers procedure add(a n−1 …a 0, b n−1 …b 0 : binary representations of non-negative integers a, b) carry := 0 for bitIndex := 0 to n−1 {go through bits} bitSum := a bitIndex +b bitIndex +carry {2-bit sum} s bitIndex := bitSum mod 2 {low bit of sum} carry :=  bitSum / 2  {high bit of sum} s n := carry return s n …s 0 : binary representation of integer s

Two ’ s Complement In binary, negative numbers can be conveniently represented using two’s complement notation. In binary, negative numbers can be conveniently represented using two’s complement notation. In this scheme, a string of n bits can represent any integer i such that −2 n−1 ≤ i < 2 n−1. In this scheme, a string of n bits can represent any integer i such that −2 n−1 ≤ i < 2 n−1. The bit in the highest-order bit-position (#n−1) represents a coefficient multiplying −2 n−1 ; The bit in the highest-order bit-position (#n−1) represents a coefficient multiplying −2 n−1 ; the other positions i < n−1 just represent 2 i, as before. the other positions i < n−1 just represent 2 i, as before. The negation of any n-bit two’s complement number a = a n−1 …a 0 is given by a n−1 …a The negation of any n-bit two’s complement number a = a n−1 …a 0 is given by a n−1 …a The bitwise logical complement of the n-bit string a n−1 …a 0.

Correctness of Negation Algorithm Theorem: For an integer a represented in two’s complement notation, −a = a + 1. Proof: a = −a n−1 2 n−1 + a n−2 2 n−2 + … + a 0 2 0, so −a = a n−1 2 n−1 − a n−2 2 n−2 − … − a so −a = a n−1 2 n−1 − a n−2 2 n−2 − … − a Note a n−1 2 n−1 = (1−a n−1 )2 n−1 = 2 n−1 − a n−1 2 n−1. Note a n−1 2 n−1 = (1−a n−1 )2 n−1 = 2 n−1 − a n−1 2 n−1. But 2 n−1 = 2 n−2 + … But 2 n−1 = 2 n−2 + … So we have −a = − a n−1 2 n−1 + (1−a n−2 )2 n−2 + … + (1−a 0 ) = a + 1. So we have −a = − a n−1 2 n−1 + (1−a n−2 )2 n−2 + … + (1−a 0 ) = a + 1.

Subtraction of Binary Numbers procedure subtract(a n−1 …a 0, b n−1 …b 0 : binary two’s complement reps. of integers a, b) return add(a, add(b,1)) { a + (−b) } Note that this fails if either of the adds causes a carry into or out of the n−1 position, since 2 n−2 +2 n−2 ≠ −2 n−1, and −2 n−1 + (−2 n−1 ) = −2 n isn’t representable! We call this an overflow.

Multiplication of Binary Numbers procedure multiply(a n−1 …a 0, b n−1 …b 0 : binary representations of a,b  N) product := 0 for i := 0 to n−1 if b i = 1 then product := add(a n−1 …a 0 0 i, product) product := add(a n−1 …a 0 0 i, product) return product i extra 0-bits appended after the digits of a

Binary Division with Remainder procedure div-mod(a,d  Z + ) {Quotient & rem. of a/d.} n := max(length of a in bits, length of d in bits) for i := n−1 downto 0 if a ≥ d0 i then {Can we subtract at this position?} q i := 1{This bit of quotient is 1.} a := a − d0 i {Subtract to get remainder.} else q i := 0{This bit of quotient is 0.} r := a return q,r{q = quotient, r = remainder}