Chapter 1 addtion - exp Mod N Fall 2017 PDF.pdf

# Chapter 1 addtion - exp Mod N Fall 2017 PDF.pdf - Chapter 1...

• 36

This preview shows pages 1–10. Sign up to view the full content.

Chapter 1 Numerical Algorithms

This preview has intentionally blurred sections. Sign up to view the full version.

Addition on a computer is done by circuits Modern computers 64 bits at a time. (Aside, C int is still 32 bits for compatibility) One bit adder demonstration is by Marble machine (Own work) [CC BY-SA 4.0 ( )], via Wikimedia Commons Because we are working with asymptotic notation. A 64-bit adder is a constant 64 times as e cient as a 1-bit adder In the bit model we assume we are working with a 1-bit adder because we can simply extract out the factor of 64 and treat everything as 1-bit adder. Note we only need the bit model when working with numbers that have no fixed upper bound (as that upper bound can be treated as another constant value). 1 1 0 1 0 + 1 1 1 0 1 16’s 8’s 4’s 2’s 1’s 26 +29
Binary Addition Using the Bit Model In the bit-model, we consider adding three binary digits to be one step (i.e. takes a constant amount of time) Adding three single digit numbers results in a number at most two digits long! (This is true for addition in any base 2) As a function of the input size, how long does it take to add two n-bit binary numbers? (e.g. n 2048)? x 1 x 2 x 3 x 4 ! x n 1 x n + y 1 y 2 y 3 y 4 ! y n 1 y n z 0 z 1 z 2 z 3 z 4 ! z n 1 z n 1 1 + 1 0 1 + 1 0 0 + 1 1 1 + 0 1 0 + 1 1 0 + 0 0 0 + 0 n-bit # + n-bit # (n+1)-bit # O(n) bit operations thus O(n) time

This preview has intentionally blurred sections. Sign up to view the full version.

Binary Addition* (cont) To add two n-bit number we needed to be able to read them and write down the answer. Each bit in the sum took a constant amount of time to compute So, our addition algorithm is optimal up to multiplicative contents! We will usually use N to denote the number and n to denote the number of bits in the binary representation of N N = ( 1 0 0 1 1 ) 2 16’s 8’s 4’s 2’s 1’s n = 5 10 = 19 10 *Of course, assumptions on the number and the data structure could change the analysis
We now can use addition as a subroutine in other algorithms For n = log 2 (N+1) , adding two n-bit binary numbers takes O(n) time

This preview has intentionally blurred sections. Sign up to view the full version.

X*2, X/2 We are going to do multiplication and division next. In order to do this, we need two lower level operations. The ability to double X, and the ability to halve X (With flooring) We accomplish this by the use of bit shift operations. In C this is represented by << and >> These shift a binary number “over”.
Bit shift example Doubling: 0010 ShiftLeft 1 = 0100 (I.e. 2 ShiftLeft 1 = 4) 0010 ShiftRight 1 = 0001 (I.e. 2 shiftRight 1 = 1) Note that 0011 ShiftRight 1 = 0001 (I.e. 3 ShiftRight 1 = 1) This means that if you use shift to quickly compute division by two you will get the floor

This preview has intentionally blurred sections. Sign up to view the full version.

Bit Shift Complexity As you might have guessed, bit shifting takes each bit and moves it over by one. This is O(n) in the bit model
How can you multiply two very large binary numbers?

This preview has intentionally blurred sections. Sign up to view the full version.

This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern