Download presentation
Presentation is loading. Please wait.
Published byBenedict Wilson Modified over 9 years ago
1
MSU/CSE 260 Fall 20091 1 Integer Representations & Base Conversion Section 3.6
2
MSU/CSE 260 Fall 2009 2 2 Object Representation Objects/ConceptsRepresentation A one-to-one function is required....
3
MSU/CSE 260 Fall 2009 3 3 Object Representation… Need symbols; how many? What are other issues? Physical cost of representation Representation and algorithms We’ll concentrate on “number” representation.
4
MSU/CSE 260 Fall 2009 4 ASCII code for characters CHAR CODE A 001000001 a 001100001 B 001000010 b 001100010 C 001000011 c 001100011 + 000101011. 000101110 * 000101010 % 000100101
5
MSU/CSE 260 Fall 2009 5 Encoding baseball teams A’s 000 Cardinals 001 Mariners 010 Orioles 011 Tigers 100 Twins 101 Which set of teams? 011100101000 A’s 1 Cardinals 01 Mariners 0000 Orioles 0001 Tigers 0010 Twin 0011 Which set of teams? 01100010011 Fixed size 3-bit code Variable size coding
6
MSU/CSE 260 Fall 2009 6 2^n strings of exactly n bits each in length: uniform length code n=1: {0,1} n=2: {00, 01, 10, 11} n=3: {000,001,010,011, 100,101,110,111} n=8: 256 unique strings (character set) n=10: 1024 strings n=30: over one billion strings n=33: unique string for every person n=64: new IP address size
7
MSU/CSE 260 Fall 2009 7 Physical reps for 0 and 1 Up versus Down 5 volts versus 0 volts Hot versus cold Pit versus no pit on CD surface Red checker versus black checker
8
MSU/CSE 260 Fall 2009 8 CD has many tracks of spots Shiny spot is 0 Burned spot is 1 5 billion spots total 700 MB total 44,000 x 16 spots for just 1 second of high fidelity music Laser reads shiny versus dull spots CD CD spins
9
MSU/CSE 260 Fall 2009 9 9 Example: Numbers Using only one symbol 1 2 3 12...... ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲...... As long as we have a one-to-one function, we have a correct presentation
10
MSU/CSE 260 Fall 2009 10 Example: Using two symbols 1 2 3 12...... ▲ ■ ▲ ▲ ■ ▲ ■ ▲ ▲...... As long as we have a one-to-one function, we have a correct presentation
11
MSU/CSE 260 Fall 2009 11 Example: Using two symbols 1 2 3 12...... ▲ ▲ ▲ ▲ ▲ ▲ ■ ▲ ▲...... As long as we have a one-to-one function, we have a correct presentation
12
MSU/CSE 260 Fall 2009 12 Understanding Number Representation We usually use {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} as base to write numbers, but we can use any base. Remember that : The above is a positional system representation It has a “compact” manual There are other representations
13
MSU/CSE 260 Fall 2009 13 Representation of Integers… Theorem: Let b be a positive integer > 1. We can write every positive integer uniquely in the following form: where k is a positive integer and a 0, a 1, …a k {0, 1, 2, …, b – 1} and a k ≠ 0. Example:
14
MSU/CSE 260 Fall 2009 14 Base conversion To convert a number, say 6543, in base 10 to an equivalent number in another base, say 7, we need to come up with the coefficients in the following expression: It seems that we have only one equation but many unknowns. We use the restriction that the coefficients are all integers between 0 and 6. A series of divisions would do
15
MSU/CSE 260 Fall 2009 15 Example: Base conversion
16
MSU/CSE 260 Fall 2009 16 Integer n and base b
17
MSU/CSE 260 Fall 2009 17 From decimal to another base The base b expansion of n is (a k-1 …a 1 a 0 ) b
18
MSU/CSE 260 Fall 2009 18 Example: Decimal 50 to base 2 50 = 25*2 + 0 25 = 12*2 + 1 12 = 6*2 + 0 6 = 3*2 + 0 3 = 1*2 + 1 1 = 0*2 + 1 So, the base 2 representation is 110010
19
MSU/CSE 260 Fall 2009 19 Conversion from decimal to octal
20
MSU/CSE 260 Fall 2009 20 Example: Decimal 229 to base 8 229 = 28*8 + 5 28 = 3*8 + 4 3 = 0*8 + 3 So, decimal 229 is 345 in base 8
21
MSU/CSE 260 Fall 2009 21 Bases used in Computer Science Base 2 (Binary), bits in computer, has two symbols {0,1} Base 8 (Octal), has eight symbols {0,1,2,3,4,5,6,7} Base 16 (Hexadecimal) has sixteen symbols {0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F}. Note that A=10, B=11, C=12, D=13, E=14, F=15.
22
MSU/CSE 260 Fall 2009 22 Exercise Convert 6.96 to binary, up to 5 places First we convert 6 to binary, which is 110 0.96 ×2 = 1.92, so the first digit after binary point is 1 0.92 ×2 = 1.84, so the 2nd digit after binary point is 1 0.84 ×2 = 1.68, so 3rd digit after binary point is 1 0.68 ×2 = 1.36, so the 4th digit after binary point is 1 0.36 ×2 = 0.72, so the 5th digit after binary point is 0 …. So, 6.96 is 110.11110…. in binary
23
MSU/CSE 260 Fall 2009 23 Converting Bases Related by Powers 001101 000 11010 0111 13 1 10 7 D 1 A 7 1 5 0 6 4 7 Octal (base 8) Binary (base 2) Hexadecimal (base 16) 53671 base 10 Since 8 = 2 3 then every 3 binary digits makes an Octal digit. Since 16=2 4 then every 4 binary digits makes a Hexadecimal digit
24
MSU/CSE 260 Fall 2009 24 Representation of integers in different bases Hexadecimal, Octal and Binary Representation of Integers Base Representation 100 123456789 1112131415 160 123456789ABCDEF 80 12345671011121314151617 2 01101110010111011110001001101010111100110111101111
25
MSU/CSE 260 Fall 2009 25 Algorithms for Integer Operation Computers use binary numbers to perform operations in integers. In order to handle integer operations in binary system first we should convert the decimal numbers into integers and then do the operations using binary numbers.
26
MSU/CSE 260 Fall 2009 26 Addition of Integers in Base 2 A procedure to perform addition is based on the usual method for adding numbers with pencil and paper. Example: 1 1 carry bits 1 1 1 0 (14) 10 + 1 0 1 1 (11) 10 = 1 1 0 0 1 (25) 10 An overflow may happen when adding two numbers.
27
MSU/CSE 260 Fall 2009 27 Multiplying Integers in Base 2 Example 1 1 0 (6) 10 1 0 1 (5) 10 1 1 0 + 0 0 0 1 1 0___ = 1 1 1 1 0 (30) 10 Up to 2n bits may be needed to represent the product of two n-bit numbers.
28
MSU/CSE 260 Fall 2009 28 More Examples on Base Arithmetic Compute222 + 222 in base 3. Answer: (1221) 3 Compute 222 + 222 in base 4. Answer: (1110) 4 Compute 222 + 222 in base 5. Answer: (444) 5
29
MSU/CSE 260 Fall 2009 29 A MATLAB function for dec to base function rep = decimal2base ( decimal, base ) % % Convert input decimal number to rep in base. % Output is an array of digits 0,1,... base-1. % position = 1; rep(1) = 0; while decimal>0 digitValue = mod(decimal,base); rep(position) = digitValue; % stack it in array position = position+1; decimal = floor(decimal/base); end % Flip the output array so higher order digit are at "left". rep = fliplr(rep);
30
MSU/CSE 260 Fall 2009 30 results >> decimal2base(983, 5) ans = 1 2 4 1 3 >> decimal2base(453, 5) ans = 3 3 0 3 >> decimal2base(6543, 7) ans = 2 5 0 3 5 >> decimal2base(78, 2) ans = 1 0 0 1 1 1 0
31
MSU/CSE 260 Fall 2009 31 Horner’s rule for computing polynomial Consider the compiler processing the STRING x = 983; Compute number value as characters are read val = 0; val = val*10 + 9 val = val*10 + 8 val = val*10 + 3 O(N) * ops and O(N) + ops Computing all the powers separately would require O(N 2 ) * ops (n+(n-1)+ … + 2+1)
32
MSU/CSE 260 Fall 2009 32 Factoring the polynomial p(x) P(x) = a n x n + a n-1 x n-1 + … + a 2 x 2 + a 1 x + a 0 P(x) = (a n x n-1 + a n-1 x n-2 + … + a 2 x + a 1 ) x + a 0 P(x) = ((a n x n-2 + a n-1 x n-3 + … + a 2 ) x + a 1 ) x + a 0 P(X) = ((…((a n x + a n-1 ) x + a n-2 ) x … + a 1 ) x + a 0 This shows a simple repetition of n multiplies and n adds (Horner’s rule). It could take n multiplies just to compute the term a n x n if a simple loop were used for this. (If we have a fixed x, such as a base, we could store its powers in an array and not compute them each time.)
33
MSU/CSE 260 Fall 2009 33 The following example shows a hash function using this polynomial form The key is a string of characters – any length. The characters are the coefficients of the polynomial. The value of x, or the base value, is 32, which makes multiplication very fast via shifting left 5 bits For speed, the logical and ‘∧’ is used instead of ‘*’ and also for the addition! Many tests have shown this function to be fast and uniform over the table size.
34
MSU/CSE 260 Fall 2009 34 Hash function effort O(n): n = #chars int TABLESIZE = 10000; // default size // Hash function from figure 19.2 of old Weiss text (page 611). // This function mixes the bits of the key to produce a pseudo // random integer between 0 and TABLESIZE-1 unsigned int Hash(const string& KEY, const int tableSize) { unsigned int HashValue = 0; // compute value of polynomial P(X), where KEY[i] are the coefficients // and X=32; multiplying by X is done by a left shift of 5 bits // and arithmetic is "abbreviated" using bit operations. for( int i=0; i<KEY.length(); i++ ) // use every character of key // Horner's rule. Also, use the bitwise OR instead of + op // for speed and overflow suppression { HashValue = ( HashValue << 5 ) ^ KEY[i] ^ HashValue; } return HashValue % tableSize; }
35
MSU/CSE 260 Fall 2009 35 C++ details: http://docs.hp.com/en/B3901-90013/ch05s04.html exp1 << exp2 Left shifts (logical shift) the bits in exp1 by exp2 positions. exp1 >> exp2 Right shifts (logical or arithmetic shift) the bits in exp1 by exp2 positions. exp1 & exp2 Performs a bitwise AND operation. exp1 ^ exp2 Performs a bitwise OR operation. exp1 | exp2 Performs a bitwise inclusive OR operation. ~exp1 Performs a bitwise negation (one's complement) operation.
36
MSU/CSE 260 Fall 2009 36 Program to call hash function int main() { string plainText; int digitalSignature; cout << "\n-----+----- Crypto/Hashing Demonstration -----+-----\n"; cout << "\n====== Give TABLESIZE (between 10 and 1000000): "; cin >> TABLESIZE; while ( 1 ) { cout << "\nGive plain text string = " ; cin >> plainText; if ( plainText == "quit") break; digitalSignature = Hash(plainText, TABLESIZE); cout << "\nYour digital signature is: " << digitalSignature << endl; } return 0;
37
MSU/CSE 260 Fall 2009 37 Using a polynomial hash function -----+----- Crypto/Hashing Demonstration -----+----- ====== Give TABLESIZE (between 10 and 1000000): 20 Give plain text string = harrycarrie Your digital signature is: 10 Give plain text string = spartyparty Your digital signature is: 3 Give plain text string = sparty Your digital signature is: 13 Give plain text string = party Your digital signature is: 2 Give plain text string = 122345678901234567890!@#$%^&&* Your digital signature is: 13
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.