59
CS 1104 Help Session III Number Systems Colin Tan [email protected] http:// www.comp.nus.edu.sg/ ~ctank

CS 1104 Help Session III Number Systems Colin Tan [email protected] ctank

Embed Size (px)

Citation preview

Page 1: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

CS 1104 Help Session IIINumber Systems

Colin Tan

[email protected]

http://www.comp.nus.edu.sg/~ctank

Page 2: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Representing Numbers

• Computers are built to manipulate numbers.• Question is: How do we represent these numbers in a way

that an automated machine can manipulate?– Could represent data by range of voltage levels.

• E.g. Min voltage of 0 volts can be used to represent 0, maximum voltage of 10 volts can be used to represent some maximum number 10,000.

• A number n can be represented by 10n/10000 volts.

– This presents some problems:• Small dynamic range

– Not practical to represent numbers like 9.99 x 10^999– Sensitive to noise. A slight voltage shift will give you a completely different

number.

Page 3: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Representing Numbers

• Clearly this is an unsatisfactory system. Alternatively, instead of using a range of voltages, we make use of two voltages:– 0 volts, representing a ‘low’ or ‘0’

– 5 volts, representing a ‘high’ or ‘1’.

• We can then form numbers by stringing ‘0’s and ‘1’s together:0 0000

1 0001

2 0010

3 0011

Page 4: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Counting in Binary

• From this was born the binary number system.– It is a base-2 system: i.e. its digits are in the set [0, 1].

• Counting in binary is exactly the same as counting in decimal, except that we increment the next digital after ‘1’ instead of ‘9’.– Decimal: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21...

– Binary: 0 1 10 11 100 101 110 111 100....

Page 5: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Alternative Computer Bases

• Binary numbers are easy to manipulate in computers, but are hard for ordinary humans to understand and to use:– Imagine keying hundreds of numbers like

00101011101001010111 111110101011

• To make it easier for people, computer scientists have re-grouped bits together, and gave groups of bits new symbols.

• Each binary digit is called a “bit” for short.

Page 6: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

The Octal Number System

• As an example, let’s regroup bits into groups of 3, and assign them symbols according to this table:

000 - 0 001 - 1 010 - 2 011 - 3

100 - 4 101 - 5 110 - 6 111 - 7

So for the previous examples, after re-grouping (from right to left) we get:

101 011 101 001 010 111 111 110 101 011

Assigning the symbols, we get:

5351277653

Page 7: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

The Octal Number System

• By grouping bits together and assigning groups of 3 bits symbols from ‘0’ to ‘7’, we have created a new number system called ‘octal’.

• Octal is a base-8 number systems, meaning that the digits belong to the set [0, 7].

• Counting in octal is similar to counting in decimal, except that the largest digit is 7:– 0 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17 20 21 22 23 24 25 26 27

30....

Page 8: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

The Hexadecimal Number System

• Instead of grouping into groups of 3 bits, we can also group into 4 bits. Each pattern of 4 bits will be assigned 1 of 16 symbols according to this table:0000 - 0 0001 - 1 0010 - 2 0011 - 3

0100 - 4 0101 - 5 0110 - 6 0111 - 7

1000 - 8 1001 - 9 1010 - A 1011 - B

1100 - C 1101 - D 1110 - E 1111 - F

• If we re-grouped the previous example into 4 bits (from right to left), we get:0010 1011 1010 0101 0111 1111 1010 1011

Page 9: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

The Hexadecimal Number System

• Using the table and assigning symbols, we get:0010 1011 1010 0101 0111 1111 1010 1011

2BA57FAB

• This is the hexadecimal number system, or base-16 number system.

• Counting in hexadecimal:0 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15 16 17 18 19 1A

1B 1C 1D 1E 1F 20 21 22 ...

Page 10: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Dealing with Insufficient Bits

• If there are insufficient left-most bits to complete the groupings, pad zeroes to the left:– E.g. (10101)2 to octal:

• This regroups 10 101

• Pad a ‘0’ to the leftmost bit, giving us 010 101, which is (25)8

Page 11: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Notations

• For clarity, we normally place numbers within brackets and indicate their base (2 for binary, 8 for octal, 16 for hexadecimal):

(00101011101001010111 111110101011)2

(5361277543)8

(2BA57FAB)16

Page 12: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Conversion Between Bases

• Since base-8 and base-16 are all basically different ways of grouping bits, it is possible to convert between base-8 and base-16 (vice versa) simply by using the tables to convert each digit into base-2, and then re-grouping the bits as appropriate. E.g.(2BA57FAB)2

We convert each digit to base-2 using the hexadecimal table:

(0010 1011 1010 0101 0111 1111 1010 1011)2

We then regroup to groups of 3, from right to left:

(101 011 101 001 010 111 111 110 101 011)2

We now use the octal table to convert these back to digits:

(5351277653)8

Page 13: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Decimal Numbers

• Binary, octal and hexadecimal are well and good, but people count in decimals! We have two options:– Re-educate everyone to count in base-2

– Find some way to represent decimal numbers using binary digits.

• For various reasons, computer scientists have chosen the second option: represent decimal numbers using binary digits.– This turns out to be easier than expected, as it turns out that there

is a direct relationship between binary, hexadecimal and octal numbers, and decimals.

Page 14: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

The Weighted Number System

• This relationship comes in the fact that the binary, octal and hexadecimal numbers systems are weighted.– This means that each digit has a weight (multiplier) attached to it,

and the value of the multiplier is dependent on the position of the digit.

– For digits in the least-significant (rightmost) position, it has a weight of B0, where B is the base for that number. For the digit in the next position, it has a base of B1 etc.

– It is easy to find the decimal equivalent of a number, simply by summing over all the products of each digit with its respective base.

– So a number (a4a3a2a1a0)B has decimal value:

a9b0 + a1b1 + a2b2 + a3b3 + a4b4

Page 15: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Example

• Convert (101001)2 to decimal:– Starting from lsb, we have:

• 1 x 20 + 0 x 21 + 0 x 22 + 1 x 23 + 0 x 24 + 1 x 25 = (41)10

• Convert (1FEA)16 to decimal:– Starting from least-significant, and treating A = 10, F = 15 and E =

14, we have:• 10 x 160 + 14 x 161 + 15 x 162 + 1 x 163 = 8170.

• Convert (741)8 to decimal:– Starting from least significant digit:

• 1 x 80 + 4 x 82 + 7 x 83 = 481

Page 16: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Converting from Decimal

• Converting from decimal is trickier. We take the decimal number D and repeatedly divide by the desired base B (i.e.the base we want to convert to) until we have a remainder of 0. At each step, the remainder gives you the digit that you want in the target base.

Page 17: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Converting from Decimal

• Why repeated divide method works:– Suppose we have a decimal number D, which can be represented by

(b3b2b1b0)B. We now have to determine what each digit bx is.• We then have D = b3 x B3 + b2 x B2 + b1

x B1 + b0 • Compute D1 = D/B

– We will have D1 = b3 x B2 + b2 x B1 + b1, remainder b0.

• Compute D2 = D1/B. – We will have D2 = b3 x B1 + b2, remainder b1.

• Compute D3 = D2/B– We will have D3 = b3, remainder b2

• Compute D4 = D3/B– We will now have D4 = 0, remainder b3

– If we take all the remainders from bottom up and string them together, we will get our converted number (b3b2b1b0)B

Page 18: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Example

• Convert (321)10 to binary:– 321/2 = 160 remainder 1

– 160/2 = 80 remainder 0

– 80/2 = 40 remainder 0

– 40/2 = 20 remainder 0

– 20/2 = 10 remainder 0

– 10/2 = 5 remainder 0

– 5/2 = 2 remainder 1

– 2/2 = 1 remainder 0

– 1/2 = 0 remainder 1

• Reading from bottom up, we have (321)10 = (101000001)2.

Page 19: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Example

• Conversion to octal or hexadecimal can be achieved by using the re-group/table-lookup method shown earlier.

• Note that the mathematics behind the repeated divide does not stop you from converting to base-8 or base-16 directly without first converting to binary. Just repeatedly divide by 8 or 16 instead of 2.

Page 20: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Example

• Find (334)10 in hexadecimal:– 334/16 = 20 remainder 14

– 20/16 = 1 remainder 4

– 1/16 = 0 remainder 1

– 14 in hexadecimal is the symbol E. So we have (334)10 = (14E)16

Page 21: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Dealing with Fractions

• Converting from binary to octal or hexadecimal is done using the usual re-group/lookup-table/translate method, except that regrouping is done from left to right instead of right to left.– E.g. Convert (1101100101.1101001)2 to octal and hexadecimal:

• Octal:– Regroup integer portion: 001 101 100 101. This gives us (1545)8.– Regroup fraction: 110 100 100. This gives us (644)8.– Combine the two parts with an octal-point: (1545.644)8.

• Hexadecimal:– Regroup integer portion: 0011 0110 0101. This gives us (365)16.– Regroup fraction: 1101 0010. This gives us (D2)16..

– Recombine using a hexadecimal-point: (365.D2)16

Page 22: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Dealing with Fractions

• Converting from octal or hexadecimal to binary is simple: simply replace each symbol with their binary pattern. E.g.:– Convert (1545.644)8 to binary:

• Replace each digit with its binary pattern: (001 101 100 101.110 100 100)2

– Convert to hexadecimal:• Start with binary pattern: (001101100101.110100100)2

• Regroup into groups of 4 bits: (0011 0110 0101.1101 0010)2

• This gives us (365.D2)16

Page 23: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Dealing with Fractions

• Converting from binary, hexadecimal and octal to decimal uses the same weighted scheme method, but with negative weights:– (b3b2b1b0.b-1b-2b-3)B

= b3 x B3 + b2 x B2 + b1 x B1 + b0 x B0 + b-1 x B-1 + b-2 + b-3 x B-3

• Example:– Convert (135.2F)16 to decimal:

• 1 x 162 + 3 x 161 + 5 x 160 + 2 x 16-1 + 15 x 16-2 = (309.1836)10

Page 24: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Dealing with Fractions

• Converting a fractional decimal number is achieved by repeated multiplication:– Keep multiplying the fraction by the target-base (the base you want

to convert TO), until you obtain zero or the correct number of significant figures.

• Take the integer portion of the result as your answer.• Remove the integer portion and continue multiplying.• If you need to truncate the number to n significant figures, always

compute till n+1 significant figures, then use the (n+1)th digit for rounding.

– In binary, if n+1 digit is 1, round up. In hexadecimal, round up with n+1 digit is > 7. In octal, round up if n+1 digit is >3

– E.g. convert 235.21 to binary, to 4 significant figures.– 235 using repeated divide converts to: (11101011)2.

Page 25: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Dealing with Fractions

– Use repeated-multiply for fraction part:• 0.21 x 2 = 0.42 Integer portion: 0• 0.42 x 2 = 0.84 Integer portion: 0• 0.84 x 2 = 1.68 Integer portion: 1• 0.68 x 2 = 1.36 Integer portion: 1• 0.36 x 2 = 0.72 Integer portion: 0

– We can stop here, since we have converted to 5 SF.– The 5th digit is 0, so no rounding. We simply truncate to 4 SF:

0011.– Combine both parts using a binary point: (11101011.0011)2

– From here, can convert to octal or hexadecimal. Can also convert directly by repeated multiply by 8 or 16.

Page 26: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Why Repeated Multiply Works

• Suppose we had a fractional decimal number D. Since we are working with weighted number systems, D can be expressed in the target base B as:– D = 0.b-1b-2b-3, or D = b-1 x B-1 + b-2

x B-2 + b-3 x B-3

– Take D1 = D x B. We have D1 = b-1 x B0 + b-2 x B-1 + b-3 x B-2

• We take the integer portion b-1 x B0 (this is integer as the power of the weight is >=0). So we have b-1. Subtract away the integer, and now D1 = b-2

x B-1 + b-3 x B-2

– Take D2 = D1 x B. We have D2 = b-2 x B0 + b-3 x B-1

.

• Again we take the integer portion b-2 x B0 = b-2. Subtract it away from D2 to get D2 = b-3 x B-1

– Take D3 = D2 x B. We have D3 = b-3 x B0

• Take integer portion b-3 x B0 = b-3. Subtract it away from D3 and we get D3 = 0. Operation terminates here.

– Now we string together all the integer parts we gathered to get our fraction in the new base: (0.b-1b-2b-3)B

Page 27: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Signed Numbers

• The number systems shown in the previous slides allow us to perform lots of useful stuff on computers, but they have a fundamental problem:– There is no way to represent negative numbers!!

• We can solve this by augmenting an extra digit to the front of the number to represent the sign. The processor can then look at this extra digit and decide on the sign-ness of the number. We call this “sign and magnitude”:– E.g. if we used ‘0’ to represent +ve, and 9 to represent –ve, then 123

can be written as 0123, while –123 can be written as 9123– If we used ‘0’ to represent +ve, and ‘1’ to represent –ve, then the

binary number 01001 will be written as 001001, while –(01001) will be written as 11001.

Page 28: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Signed Numbers

• The signed-magnitude system is simple, but has some fundamental problems:– Cannot directly add numbers together:

• E.g. (1011)sm + (0001)sm = (1100)sm

– In S+Mag, this means (-3) + 1 = (-4)!!

– It is not complementary:• E.g. –3 + 3 = (1011 + 0011)sm = 1110 = -6??

• Need to introduce other negative-number systems– Radix Complement

– Diminished Radix Complement

Page 29: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Radix Complements

• The radix complement C of a number N with M digits in a base B is defined to be:– C = (BM – N)Bcns

– We can prove that C is the complement of N by finding C + N:

• C + N = (BM – N) + N = BM

• This BM is what we call an end-round-carry, and is discarded, giving us a final answer of 0.

Page 30: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Radix Complements

• E.g. Find the radix-complement of (235)10:

– -(235)10 = 103 – 235 = (765)10cns

• Find the radix-complement of (101101)2:

– 26 – 101101 = 1000000 – 101101 = (010011)2cns

– The radix-complement of a binary number is particularly useful in computers, and is called the Two’s Complement Number System.

• Find the radix-complement of (327)8:

– 83 – 327 = 1000 – 327 = (455)8

Page 31: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Diminished Radix Complement

• There is another complementary number system called the Diminished Radix Complement or (B-1) complement. The diminished radix complement C of an M digit number N in base B is defined to be:– C = (BM –1 – N)B-1cns

• Find the 9s Complement of (3212)10:

– 104 - 1 = 9999

– 9999 - 3212 = (6787)9cns

• Find the 1s complement of (101101)2

– 26 - 1 = 111111

– 111111 - 101101 = (010010)1cns

Page 32: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

2’s Complement

• The 2’s complement number system is the radix-complement number system for binary numbers:– 101011 in 2’s complement is written as (101011)2cns or (101011)2s

for short.

• Range of n-bit 2’s complement:– Smallest possible number represented: -2n-1

– Largest possible number represented: 2n-1 - 1

– So for 8-bit 2’s complement, the range is [-128, 127].• It is an asymmetric number system: there are more negative numbers

than positive

• 2’s Complement has 1 representation for 0: (00000000)2s

Page 33: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

1’s Complement

• The 1’s complement number system is the diminished-radix complement of the binary system.– 101011 in 1’s complement is written as (101011)1cns or (101011)1s

for short.

• Range of an n bit 1’s complement number– Smallest possible number represented: -2n-1 -1

– Largest possible number represented: 2n-1 -1

– So for an 8-bit 1’s complement number, the range is [-127, 127].

• 1’s complement has two representations for ‘0’: 00000000 and 11111111.– Useful for computing functions that tend towards 0.

Page 34: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Representation vs. Operation

• When we say that a number is in 2’s complement, we are speaking of its representation:– i.e. We are saying that the number is a signed number.

– We do not attempt to find the complement of the number.

– E.g. 01001100 in 2s is simply (01001100)2s

• When we are asked to find the 2’s complement of a number, or are required to negate the number, then we actually find the complement of the number:– Fastest way is to invert all bits and add 1.

– E.g. Find the 2’s complement of 01001100:• Invert every bit: 10110011

• Add 1: (10110100)2s

Page 35: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Representation vs. Operation

• The same may be said for the 1’s complement number system.– E.g. the 1’s complement representation of 01100111 is simply

(01100111)1s.

• 1’s complement operation is achieved by inverting all the bits:– E.g. Find the 1’s complement of 01100111:

• Invert all bits: (10011000)1s

Page 36: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Complements Arithmetic

• Will not be covering this topic for this set of notes, as it is relatively mechanical. Please see the AMIGOS notes set on this, available at http://www.comp.nus.edu.sg/~ctank.

Page 37: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Floating Point Numbers• Assuming a 32-bit data word size, a fixed point number

system assumes that x bits are reserved for the integer portion of a number, while 32-x bits are reserved for the fraction.

• This limits the range of number representation.– Limited number of bits in integer => limits size of integer (e.g. 16-

bit integer portion allows us to represent integers between -32768 and 32767.

– Likewise with fraction portion.

• Floating Point number systems remove this limitation.

Page 38: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Floating Point Numbers

• In the floating point number system, the binary point separating the integer and fraction portions is allowed to “float” along the number, by adjusting the exponent value:– e.g. 1 x 10^0 = 0.1 x 10^1 = 0.01 x 10^2 = 0.001 x

10^3 ad infinitum.

• This gives us very large ranges for our numbers.

Sign Significand Exponent

Page 39: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Floating Point Numbers Example

• Example 1: Represent 123.75 in floating point, assuming 16 bits for significand, 8 bits for the exponent. Assume that 2’s complements is used for the exponent.123.275 = 1111011.11

We will normalize this number such that all bits before the binary point are 0, and the first bit after is 1. To do this, we bounce the binary point 7 positions to the left:

0.111101111

For each bit we bounce to the left, we will add 1 to the exponent. Since we have bounced 7 positions, our exponent is now 7.

Page 40: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Floating Point Numbers Example

7 in 8-bit 2’s complement is 0000 0111. The number 123.75 is positive, so our sign bit is 0. Our final representation is now:

0 1111 0110 0000 0000 0000 0111

Page 41: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Floating Point Numbers

• Because of normalization, the bit immediately to the right of the binary point is always 1.

• We can always safely assume that this bit is 1, and there is no need to explicitly represent it. In our example above, using this technique we effectively have 17 significand bits: this 1 hidden bit, and the 16 bits that we can see. The hidden bit is not shown (it is hidden!), so under this new scheme our previous answer becomes:

0 1110 1100 0000 0000 0000 0111

Page 42: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Floating Point Numbers

• Example 2: What is the floating point representation of 0.2421875? Assume that the exponent is 8 bit 2’s complement, the significand is 16+1 bits (i.e. 16 bits that we can see, and 1 hidden bit).0.1875 = 0.00111112

To normalize, shift the binary point 2 places to the right. We get 0.11111. For each position we shift the binary point to the right, we subtract 1 from the exponent. So our exponent is -2.

Page 43: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Floating Point Numbers

Our significand is thus 11111 0000 0000 0000, where the italicised 1 is the hidden bit (note that we have 17 significand bits!).

Our exponent is -2, or 1111 1110 in 2’s complement. Our representation is thus:

0 1111 0000 0000 0000 1111 1110

Note that the first 1 has disappeared from the representation. The hardware assumes that it is there, and we don’t represent it in memory.

Page 44: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Floating Point Numbers

• Example: Given a floating point value 1 1100 0111 0000 0000 0010 0111, and using the floating point format of our previous 2 examples, find the decimal equivalent.

1 1100 0111 0000 0000 0010 0111

Page 45: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Floating Point Numbers

With our hidden bit, the actual significand is 0.11100 0111 0000 0000

This is 1/2+1/4+1/8+1/16+1/256+1/512+1/1024 = 0.9443359375

Exponent (assuming 2’s complement) is 0010 0111 or 39. Our value is thus 0.9443359375 x 2^39 = 5.192 x 10^11

Since the sign bit is 1, the final value is -5.192 x 10^11

Page 46: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Floating Point Numbers

• Using 2’s complement (or even 1’s complement or sign+magnitude) exponents leads to a complication:– Assuming 4 bit significand, 8 bit 2’s complement exponent, then

0.111 x 2^3 is:

0 1100 0000 0011

– 0.111 x 2^-3 is:

0 1100 1111 1101

Page 47: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Floating Point Numbers

• If we compared the bit patterns for 0.111x2^3 and 0.111 x 2^-3, we will be comparing 0 1100 0000 0011 vs. 0 1100 1111 1101respectively

• In binary form, it looks like 0.111x2^-3 is bigger than 0.111 x 2^3!!– This complicates matters when we want to use integer compare

operations (fast, cheap to use) to compare floating point numbers!

• We introduce the bias concept to fix this problem.

Page 48: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Floating Point Numbers

• Bias: For an n bit exponent, we add a bias of 2n-1 to the exponent to make it into a positive number:– Old range for 8-bit 2’s complement exponent is -128 to 127.

– If we add a bias of 28-1 (i.e. a bias of 128) to the exponent, we can remap the exponent to the range of 0 to 255.

• For our example, the exponents for 0.11x2^3 will be 128+3 = 131, while 0.11x2^-3 will have an exponent of -3+128 = 125.

Page 49: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Floating Point Numbers

• This gives us the following representations:

• For 0.11 x 2^3:

0 1100 1000 0011

• For 0.11 x 2^-3:

0 1100 0111 1101

• Now it is obvious from the binary representation which number is bigger. Can now use integer comparisons to compare 2 floating point numbers!

Page 50: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Floating Point Numbers• Find the decimal value of 1 1100 0111 0000 0000 0010

0111 given that we have 1 sign bit, 16 significand bits, and 8 exponent bits in biased representation.

1 1100 0111 0000 0000 0010 0111

• The actual significand, including hidden bit, is 11100 0111 0000 0000 = 1/2+1/4+1/8+1/16+1/256+1/512+1/1024 = 0.9443359375

• The exponent here is 39. However we had added in 128 to get the biased representation, and now we must minus 128, giving us an actual exponent of -89.

Page 51: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Floating Point Numbers

• Our value is thus 0.9443359375 x 2 ^ -89 or 1.717 x 10^-12.

• Since the sign bit is 1, this gives us a value of -1.717 x 10^-12.

Page 52: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Floating Point Numbers

• Some problems:– Certain numbers cannot be represented accurately. For

example the decimal value 0.11 (not binary!).

– There is a compromise between number of significand bits and number of exponent bits, since word size of a computer is fixed:

• More significand bits => more accurate representations, smaller range

• More exponent bits => less accurate representations, larger range

Page 53: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Did I Err?Parity Bits

• For various reasons data in the form of binary bits needs to be transferred around:– E.g. how to transport data from memory to place in the processor’s

registers?– Data must be transferred from a website to your computer, or you

cannot surf the web.

• The problem is that errors can be introduced in the transmission:– Noise can be introduced by line capacitance, electro-magnetic

interference etc.– This noise can cause insertion or deletion of bits, or cause bits to

change from ‘0’ to ‘1’ and vice-versa.

Page 54: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Parity Bits

• The idea behind parity bits is very simple:– Add an extra bit to the leftmost of the data you want to transfer.– Odd parity:

• Set this bit in such a way as to make the number of ‘1’ bits odd.– E.g. 010110

» Add extra bit: 0010110» Extra bit retains value ‘0’ as the number of ‘1’ bits is already odd:

0010110

– Even parity:• Set this bit in such a way as to make the number of ‘1’ bits even.

– E.g. 010110» Add extra bit: 0010110» Extract bit is set to ‘1’ to make the number of ‘1’ bits even:

1010110.

Page 55: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Parity Bits

• Both sending and receiving side must agree on:– i) Number of data bits to be sent and received (6 in our example)

– ii) Type of parity to use (odd or even)

• When the receiving side gets the data, it counts the number of ‘1’ bits:– If the agreed parity is odd, and yet the receiver finds an even

number of ‘1’ bits, then an error has occurred. Otherwise the data is fine.

– Likewise if the agreed parity is even, but the receiver finds an odd number of ‘1’ bits, an error has occurred. Otherwise the data is fine.

Page 56: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Parity Bits

• Limitations:– Parity can only detect an odd number of errorneous bits:

• E.g. original binary pattern: 10110. After odd-parity, we get 010110.

• Supposed the bit pattern transmitted is 011010 instead of 010110. The receiver side will still see an odd-number of ‘1’ bits, and assume that the data is fine.

• Suppose this same piece of data is received as 011000 (3 bits incorrect). Here the receiver sees and even number of bits. Knowing that odd-parity is in effect, the receiver knows that this is an error.

– Parity cannot correct errors• For this you need specialized error-correcting codes (usually

expensive).

Page 57: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Summary

• A binary number system is the only practical number system for computers:– Allows us to use 0volts as ‘0’ and 5 volts as ‘1’, giving better

noise properties and dynamic range.

• It is inconvenient, so we use larger groups of binary digits (bits), giving us the octal and hexadecimal number systems.

• Conversion between binary and hexadecimal is simply a case of translating to binary, re-grouping the bits, and translating back to octal or hexadecimal.

Page 58: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Summary

• Conversion from decimal to binary, octal or hexadecimal can only be achieved using repeated divide for integers, repeated multiply for fractions.

• Conversion from binary, octal or hexadecimal to decimal takes advantage of the weighted number system.

• Floating points give us a large range of numbers (at the expense of accuracy), or very accurate numbers (at the expense of range).

• Floating point numbers cannot represent some numbers accurately.

Page 59: CS 1104 Help Session III Number Systems Colin Tan ctank@comp.nus.edu.sg ctank

Summary

• Parity bits are added to data to determine if data was transmitted and received correctly.

• Parity bit can only detect odd number of errors.