Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 1104 Help Session III Number Systems

Similar presentations


Presentation on theme: "CS 1104 Help Session III Number Systems"— Presentation transcript:

1 CS 1104 Help Session III Number Systems
Colin Tan

2 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.

3 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:

4 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: Binary:

5 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 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.

6 The Octal Number System
As an example, let’s regroup bits into groups of 3, and assign them symbols according to this table: So for the previous examples, after re-grouping (from right to left) we get: Assigning the symbols, we get:

7 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:

8 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: A B C D E F If we re-grouped the previous example into 4 bits (from right to left), we get:

9 The Hexadecimal Number System
Using the table and assigning symbols, we get: 2BA57FAB This is the hexadecimal number system, or base-16 number system. Counting in hexadecimal: A B C D E F A 1B 1C 1D 1E 1F

10 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 Pad a ‘0’ to the leftmost bit, giving us , which is (25)8

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

12 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: ( )2 We then regroup to groups of 3, from right to left: ( )2 We now use the octal table to convert these back to digits: ( )8

13 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.

14 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

15 Example Convert (101001)2 to decimal: Convert (1FEA)16 to decimal:
Starting from lsb, we have: 1 x x x x x 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 x x x 163 = 8170. Convert (741)8 to decimal: Starting from least significant digit: 1 x x x 83 = 481

16 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.

17 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

18 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 = ( )2.

19 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.

20 Example Find (334)10 in hexadecimal: 334/16 = 20 remainder 14
14 in hexadecimal is the symbol E. So we have (334)10 = (14E)16

21 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 ( )2 to octal and hexadecimal: Octal: Regroup integer portion: This gives us (1545)8. Regroup fraction: This gives us (644)8. Combine the two parts with an octal-point: ( )8. Hexadecimal: Regroup integer portion: This gives us (365)16. Regroup fraction: This gives us (D2)16.. Recombine using a hexadecimal-point: (365.D2)16

22 Dealing with Fractions
Converting from octal or hexadecimal to binary is simple: simply replace each symbol with their binary pattern. E.g.: Convert ( )8 to binary: Replace each digit with its binary pattern: ( )2 Convert to hexadecimal: Start with binary pattern: ( )2 Regroup into groups of 4 bits: ( )2 This gives us (365.D2)16

23 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 x x x x 16-2 = ( )10

24 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 to binary, to 4 significant figures. 235 using repeated divide converts to: ( )2.

25 Dealing with Fractions
Use repeated-multiply for fraction part: 0.21 x 2 = Integer portion: 0 0.42 x 2 = Integer portion: 0 0.84 x 2 = Integer portion: 1 0.68 x 2 = Integer portion: 1 0.36 x 2 = 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: ( )2 From here, can convert to octal or hexadecimal. Can also convert directly by repeated multiply by 8 or 16.

26 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

27 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 will be written as , while –(01001) will be written as

28 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 = ( )sm = 1110 = -6?? Need to introduce other negative-number systems Radix Complement Diminished Radix Complement

29 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.

30 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 – = – = (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

31 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: = 9999 = (6787)9cns Find the 1s complement of (101101)2 = = (010010)1cns

32 2’s Complement The 2’s complement number system is the radix-complement number system for binary numbers: 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: ( )2s

33 1’s Complement The 1’s complement number system is the diminished-radix complement of the binary system. 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’: and Useful for computing functions that tend towards 0.

34 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 in 2s is simply ( )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 : Invert every bit: Add 1: ( )2s

35 Representation vs. Operation
The same may be said for the 1’s complement number system. E.g. the 1’s complement representation of is simply ( )1s. 1’s complement operation is achieved by inverting all the bits: E.g. Find the 1’s complement of : Invert all bits: ( )1s

36 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

37 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 and Likewise with fraction portion. Floating Point number systems remove this limitation.

38 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 = x 10^3 ad infinitum. This gives us very large ranges for our numbers.

39 Floating Point Numbers Example
Example 1: Represent in floating point, assuming 16 bits for significand, 8 bits for the exponent. Assume that 2’s complements is used for the exponent. = 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: 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.

40 Floating Point Numbers Example
7 in 8-bit 2’s complement is The number is positive, so our sign bit is 0. Our final representation is now:

41 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:

42 Floating Point Numbers
Example 2: What is the floating point representation of ? 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). = To normalize, shift the binary point 2 places to the right. We get For each position we shift the binary point to the right, we subtract 1 from the exponent. So our exponent is -2.

43 Floating Point Numbers
Our significand is thus , where the italicised 1 is the hidden bit (note that we have 17 significand bits!). Our exponent is -2, or in 2’s complement. Our representation is thus: 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.

44 Floating Point Numbers
Example: Given a floating point value , and using the floating point format of our previous 2 examples, find the decimal equivalent.

45 Floating Point Numbers
With our hidden bit, the actual significand is This is 1/2+1/4+1/8+1/16+1/256+1/512+1/1024 = Exponent (assuming 2’s complement) is or 39. Our value is thus x 2^39 = x 10^11 Since the sign bit is 1, the final value is x 10^11

46 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 x 2^3 is: 0.111 x 2^-3 is:

47 Floating Point Numbers
If we compared the bit patterns for 0.111x2^3 and x 2^-3, we will be comparing vs respectively In binary form, it looks like 0.111x2^-3 is bigger than 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.

48 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 = 131, while 0.11x2^-3 will have an exponent of = 125.

49 Floating Point Numbers
This gives us the following representations: For 0.11 x 2^3: For 0.11 x 2^-3: Now it is obvious from the binary representation which number is bigger. Can now use integer comparisons to compare 2 floating point numbers!

50 Floating Point Numbers
Find the decimal value of given that we have 1 sign bit, 16 significand bits, and 8 exponent bits in biased representation. The actual significand, including hidden bit, is = 1/2+1/4+1/8+1/16+1/256+1/512+1/1024 = 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.

51 Floating Point Numbers
Our value is thus x 2 ^ -89 or x 10^-12. Since the sign bit is 1, this gives us a value of x 10^-12.

52 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

53 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.

54 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 Add extra bit: Extra bit retains value ‘0’ as the number of ‘1’ bits is already odd: Even parity: Set this bit in such a way as to make the number of ‘1’ bits even. Extract bit is set to ‘1’ to make the number of ‘1’ bits even:

55 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.

56 Parity Bits Limitations:
Parity can only detect an odd number of errorneous bits: E.g. original binary pattern: After odd-parity, we get Supposed the bit pattern transmitted is instead of 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 (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).

57 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.

58 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.

59 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.


Download ppt "CS 1104 Help Session III Number Systems"

Similar presentations


Ads by Google