Number Systems - Part II CS 215 Lecture # 6
Conversion from Decimal Let N be the number to be converted. We can write N as N = q*b + r where q = quotient b = the base into which N is to be converted r = remainder
Example 6.1 Convert 8010 to binary (base 2) 80 = 40*2 + 0 40 = 20*2 + 0 20 = 10*2 + 0 10 = 5*2 + 0 5 = 2*2 + 1 2 = 1*2 + 0 1 = 0*2 + 1 Answer: 10100002
Example 6.2 Convert 8010 to octal(base 8) 80 = 10*8 + 0 10 = 1*8 + 2 10 = 1*8 + 2 1 = 0*8 + 1 Answer: 8010 = 1208 Also, from binary: 8010 = 10100002 = 1 010 000 = 120
Example 6.3 Convert 8010 to hexadecimal(base 16) 80 = 5*16 + 0 5 = 0*16 + 5 Answer: 8010 = 5016 Also, from binary: 8010 = 10100002 = 101 0000 = 50
Conversion from Decimal Decimal Binary or Hexadecimal Repeated division by 2 or 16 Example: Convert 292 to hexadecimal. 292/16 = 18 R 4 18/16 = 1 R 2 1/16 = 0 R 1 292 = 12416 When the quotient is less than 16, the process ends
Conversion from Decimal Convert 42 to hexadecimal 42/16 = 2 R 10 (but 10 = a16) 2/16 = 0 R 2 42 = 2a16 Convert 109 to binary 109/2 = 54 R 1 54/2 = 27 R 0 27/2 = 13 R 1 13/2 = 6 R 1 6/2 = 3 R 0 3/2 = 1 R 1 1/2 = 0 R 1 109 = 11011012
Shortcut: convert to hexadecimal It is sometimes easier to convert from binary to hexadecimal, then to decimal, instead of converting directly to decimal 11101010110110 = 0011 1010 1011 0110 = 3 a b 6 = 3*16^3 + 10*16^2 + 11*16 + 6 = 3*4096 + 10*256 + 176 + 6 = 12198 + 2560 + 182 = 12198 + 2742 = 14940
Shortcut: convert to hexadecimal The converse is also true: converting from decimal to hexadecimal, then to binary generally requires far fewer steps than converting directly to binary 278/16 = 17 R 6 17/16 = 1 R 1 1/16 = 0 R 1 278 = 11616 = 0001 0001 01102 = 1000101102
Example 6.4 Convert the following: 5010 = ____16 5010 = ____8 5010 = ____2 5010 = ____5
Conversion of Fractions A decimal improper fraction a/b is converted into a different base b by converting it into a mixed fraction a/b = d e/f e.g. 13/8 = 1 5/8 convert d into the required base using the techniques we have already discussed convert e/f (now a proper fraction) into the required base by following the steps below the answer is in the format ...f1f0.f-1f-2...
Example 6.5 Convert 2 5/8 to binary 2*(5/8) = 1 + 1/4 2*(1/4) = 0 + 1/2 2*(1/2) = 1 + 0 Answer: 10.101
Example 6.6 Convert 3.6 to binary 2*.6 = 1 + .2 2*.2 = 0 + .4 2*.4 = 0 + .8 2*.8 = 1 + .6 Answer: 11.10011001...
Converting back into decimal 10.101 fraction part = 1*(1/2)1 + 0*(1/2)2 + 1*(1/2)3 = .625 or 5/8 integer part = 1*(21) + 0*(20) = 2 Answer: 2 5/8
Scientific Notation Given a number N, we can express N as N = s*rx where s = mantissa or significand r = radix x = exponent
The mantissa can be made an integer by multiplying it with a fixed power of the radix and subtracting the appropriate integer constant from the exponent For example, 2.358 * 101 = 2358 * 10-2
Normalization Standard scientific notation defines the normal form to meet the following rule. 1 £ s < r where s = significand r = radix A representation is in normal form if the radix point occurs just to the right of the first significant symbol
Example 6.7 2358 * 10-2 = 2.358 * 101 1525 * 10-1 = 1.525 * 102
Representing Numbers Choosing an appropriate representation is a critical decision a computer designer has to make The chosen representation must allow for efficient execution of primitive operations For general-purpose computers, the representation must allow efficient algorithms for addition of two integers determination of additive inverse
Representing Numbers With a sequence of N bits, there are 2N unique representations Each memory cell can hold N bits The size of the memory cell determines the number of unique values that can be represented The cost of performing operations also increases as the size of the memory cell increases It is reasonable to select a memory cell size such that numbers that are frequently used are represented
Binary Representation The binary, weighted positional notation is the natural way to represent non-negative numbers. MAL numbers the bits from right to left, beginning with 0 as the least significant digit. 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Modulo Arithmetic Consider the set of number {0, … ,7} Suppose all arithmetic operations were finished by taking the result modulo 8 3 + 6 = 9, 9 mod 8 = 1 3 + 6 = 1 3*5 = 15, 15 mod 8 = 7 3 * 5 = 7 1 2 3 4 5 6 7
Modulo Arithmetic: Additive Inverse What is the additive inverse of 7? 7 + x = 0 7 + 1 = 0 0 and 4 are their own additive inverses 1 2 3 4 5 6 7