Instructor: Rob Nash Readings: Berger, Chapter 1; Rosen, Chapter 3.

24
Number Bases & Conversions Instructor: Rob Nash Readings: Berger, Chapter 1; Rosen, Chapter 3
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    214
  • download

    1

Transcript of Instructor: Rob Nash Readings: Berger, Chapter 1; Rosen, Chapter 3.

Number Bases & Conversions

Instructor: Rob Nash Readings: Berger, Chapter 1; Rosen,

Chapter 3

Algorithm

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.

Procedures & Algorithms

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

Integers with Other Bases

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

Why Binary?

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

Integer Representation

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)

Intuition for Binary Expansion

(101) base 2 is also a compact form of:

101 = (1 * 2^2) + (0 * 2^1) + (1 * 2^0 ) = 5

What about 1011?

Intro to Theorem 1 (Rosen)

Let b be a positive integer greater than 1. If n is a positive integer, it can be uniquely expressed in the form:

n = akbk + ak-1bk-1 + … + a1b1 + a0b0

Theorem 1: Base Expansions

Let b be a positive integer greater than 1. If n is a positive integer, it can be uniquely expressed in the form:

n = akbk + ak-1bk-1 + … + a1b1 + a0b0

Where k is a nonnegative integer And a0 - ak

are nonnegative integers < b This simply means our coefficients can’t exceed our base

And ak is not equal to 0 i.e. eliminate leading zeroes

The base b expansion of n is then (ak, ak-1,…, a0)b

Base is indicated by subscript b

n = akbk + ak-1bk-1 + … + a1b1 +

a0b0

The idea here: we can denote base b expansion of an integer n as (ak, ak-1,…, a0)b

From this we can calculate the decimal expansion as n = akbk

+ ak-1bk-1 + … + a1b1 + a0b0

So, (245)8 represents… (2 * 8^2) + (4 * 8^1) + (5 * 8^0) (2 * 64) + (4 * 8) + (5 * 1) 128+32+5 = 165

n = akbk + ak-1bk-1 + … + a1b1 +

a0b0

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^? )

n = akbk + ak-1bk-1 + … + a1b1 +

a0b0

(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 = ?

Binary Expansions of Integers

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

0xCAFEBABE - Hexadecimal 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

Hex-to-Binary

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.

Binary Values

0000 – 0 1000 - 8 0001 – 1 1001 - 9 0010 – 2 1010 - 10 0011 – 3 1011

- 11 0100 – 4 1100 - 12 0101 – 5 1101 - 13 0110 – 6 1110 - 14 0111 – 7 1111 - 15

Theorem 1 Revisited

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 …

Base Conversion - AYB

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

“Somebody Set Us Up the Bomb!”

Algorithm 1: The “Other” Direction

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

In Action

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

Example #2

Given n=165, convert to hexadecimal, b=16

165 / 16 = 10 r 5 10/ 16 = 0 r A

Answer: 0xA5

Example #3

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”

Pseudocode for Algorithm 1 //invariant: b, n are positive ints Procedure baseBExpansion(int n, int b) {

q = n k = 0 while( q != 0 ) {▪ ak = q mod b

▪ q = floor( q / b ) //if q & b are ints, no need for floor▪ k = k + 1

} return (ak-1…a1a0)b

}