Download presentation
Presentation is loading. Please wait.
1
Instructor: Rob Nash Readings: Berger, Chapter 1; Rosen, Chapter 3
2
A set of steps in a specific order, or A procedure for performing some (usually arithmetic) operation These algorithms, adapted for use with binary, form the basis for computer arithmetic.
3
In addition to arithmetic operations… We’ll take a look at Euclid's Algorithm An algorithm for finding the base b expansion of a positive integer for any base b And, we’ll take some time to consider an algorithm’s performance
4
We can represent numbers in other bases, however, often for convenience of manipulation or translation. We’ll need to know base 2, since those are our machine’s symbols We’ll also see that base 16 (hex) is useful for representing bit strings in a compact fashion
5
Early tube & transistor technology functions much like a lightswitch Today’s integrated circuits also offer “microswitches” as the basis for feedback loops (memory) We could have had to manipulate numbers in base 3 if our early technology functioned accordingly In fact, we’ll try out base 3 for fun
6
965 = (9 * 10^2) + (6 * 10^1) + (5 * 10^0) Just an expanded form that highlights the sum of products that compose this number ▪ Also highlights the base here, which is 10 In fact, we can use any int > 1 as a base when expressing integers, and we can convert between these bases We’ll often use base 10 as the intermediary base when converting ▪ But, we can also convert from one base to another directly (16 -> 2)
7
(101) base 2 is also a compact form of: 101 = (1 * 2^2) + (0 * 2^1) + (1 * 2^0 ) = 5 What about 1011?
8
Let b be a positive integer greater than 1. If n is a positive integer, it can be uniquely expressed in the form: n = a k b k + a k-1 b k-1 + … + a 1 b 1 + a 0 b 0
9
Let b be a positive integer greater than 1. If n is a positive integer, it can be uniquely expressed in the form: n = a k b k + a k-1 b k-1 + … + a 1 b 1 + a 0 b 0 Where k is a nonnegative integer And a 0 - a k are nonnegative integers < b This simply means our coefficients can’t exceed our base And a k is not equal to 0 i.e. eliminate leading zeroes The base b expansion of n is then (a k, a k-1,…, a 0 ) b Base is indicated by subscript b
10
The idea here: we can denote base b expansion of an integer n as (a k, a k-1,…, a 0 ) b From this we can calculate the decimal expansion as n = a k b k + a k-1 b k-1 + … + a 1 b 1 + a 0 b 0 So, (245) 8 represents… (2 * 8^2) + (4 * 8^1) + (5 * 8^0) (2 * 64) + (4 * 8) + (5 * 1) 128+32+5 = 165
11
Try to convert the following from the various bases to base 10 498 = (4 * 10^2) + (9 * 10^1) + (8 * 10^0) (110) 2 = (? * 2^2) + (? * 2^1) + (? * 2^0 ) (201) 3 = (? * 3^?) + (0 * 3^?) + (1 * 3^? )
12
(110) 2 = (1 * 2^2) + (1 * 2^1) + (0 * 2^0 ) (201) 3 = (2 * 3^2) + (0 * 3^1) + (1 * 3^0 ) (1111) 2 = ? (0xCAFE) 16 = ?
13
Lets use 2 as the base The binary expansion of an integer, then, is just a string of bits Lets consider first the decimal expansion of the integer that has “0101 1111” as its binary expansion? First, use the full form Then, consider a quick counting trick
14
Hex is quite useful in computer science, and we’ll soon see why Hex symbols: {0,1,…,9,A,B,C,D,E,F} Where A == 10, B == 11, …, F == 15 (0x01F) 16 = (0*16^2) + (1*16^1) + (15*16^0) = 31 (0xCAFE1) 16 = (12 * 16^4) + (10 * 16^3) + (15 * 16^2) + (14 * 16^1) + 1 = big base 10 int
15
Consider (0xCAFE1) 16 Each hex symbol represents (2^4) unique symbols Thus, a hex symbol neatly encodes 4 bits Two hex symbols comprise a byte So, if we can memorize all the bit patterns from 0000-1111 (only 16 of them, to represent the integers 0-15), we can quickly convert from base 16 to base 2.
16
0000 – 01000 - 8 0001 – 1 1001 - 9 0010 – 21010 - 10 0011 – 3 1011 - 11 0100 – 41100 - 12 0101 – 51101 - 13 0110 – 61110 - 14 0111 – 71111 - 15
17
So far, this is our tool for converting some integer n from an arbitrary base b to base 10. We know now how to translate from any base to decimal, but… How do we translate from decimal to an arbitrary base b? We can derive a second algorithm from Theorem 1, which accomplishes expansions in arbitrary bases Binary expansions Hexadecimal expansions …
18
Now lets consider this algorithm for constructing a base b expansion from a decimal integer. With the two algorithms combined, we can go from base x to base 10, and then from base 10 to some other base y And we’ll learn shortcuts to cut out the “middleman” along the way
20
Lets see a Base b expansion of an integer n Approach: First, divide n by b to obtain a quotient q0 and a remainder r0. ▪ r0 is your least significant digit (rightmost digit) Next, divide q0 by b to produce another quotient q1 and a remainder r1 ▪ r1 is your next least significant digit (second to rightmost) Repeat until qx == 0
21
Convert to n=31 to binary, b=2 31/2 = 15 r 1 //the rightmost bit 15/2 = 7 r 1 7/2 = 3 r 1 3/2 = 1 r 1 1/2 = 0 r 1 //the most significant bit
22
Given n=165, convert to hexadecimal, b=16 165 / 16 = 10 r 5 10/ 16 = 0 r A Answer: 0xA5
23
Given n=241, find the binary expansion, b=2 241 / 2 = 120 r 1 120 / 2 = 60 r 0 60 / 2 = 30 r 0 30 / 2 = 15 r 0 15 / 2 = 7 r 1 7 / 2 = 3 r 1 3 / 2 = 1 r 1 1 / 2 = 0 r 1 Answer: 1111 0001 Now try converting this bit string to hex using our “shortcut”
24
//invariant: b, n are positive ints Procedure baseBExpansion(int n, int b) { q = n k = 0 while( q != 0 ) { ▪ a k = q mod b ▪ q = floor( q / b ) //if q & b are ints, no need for floor ▪ k = k + 1 } return (a k-1 …a 1 a 0 ) b }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.