Presentation on theme: "NUMBER SYSTEM Compiled by : S. Agarwal, Lecturer & Systems Incharge"— Presentation transcript:
1NUMBER SYSTEM Compiled by : S. Agarwal, Lecturer & Systems Incharge St. Xavier’s Computer Centre,St. Xavier’s CollegeKolkata.March-2003
2IntroductionA number system defines a set of values used to represent quantity. We talk about the number of people attending class, the number of modules taken per student, and also use numbers to represent grades achieved by students in tests.Quantifying values and items in relation to each other is helpful for us to make sense of our environment. We do this at an early age; figuring out if we have more toys to play with, more presents, more lollies and so on.The study of number systems is not just limited to computers. We apply numbers every day, and knowing how numbers work will give us an insight into how a computer manipulates and stores numbers.
3The Romans devised a number system which could represent all the numbers from 1 to 1,000,000 using only seven symbolsI = 1V = 5X = 10L = 50C = 100D = 500M = 1000A small bar placed above a symbol indicates the number is multiplied by 1000.
4The Decimal Number System The primary number system used is a base ten number system. Base ten number systems are called decimal number systems. In decimal we have ten numerals, 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.
5Counting in decimal:When we count, we start at 1 and count up to 9. The next number is 10. That is, we create a place for the tens digit and put zero in the ones position. We continue to count by adding one to the ones place until we run out of digits again at 19. Then we reset the ones place to zero and increment the tens place to get 20. We continue in this way until we reach 99. At that point we create a third digit to hold hundreds and reset the tens and ones place to zero to get 100. And so on. Each time we change a digit, all the digits to its right are set to zero. So the number after 299 is 300, with zeros to the right of the 3.
7The Octal Number System We can use a number system with only eight numerals,0 through 7.A system with only eight numerals is called octal.
8Counting in OctalIn octal, instead of ten digits, we only have eight. So we resort to changing the digits to the left more frequently.Counting in base 8, we get:
9Since the octal number 10 follows 7, 10 in octal is 8 in decimal Since the octal number 10 follows 7, 10 in octal is 8 in decimal. 11 in octal is 9 in decimal. And so on.octal-decimal0 - 010 - 8...1 - 111 - 92 - 23 - 34 - 45 - 56 - 67 - 7
10Octal Digit Positions and Values In base 8, each digit occupies a position worth eight times the position to its right, instead of ten times as in base 10. So if 5732 is an octal number, it can be read as:5732 = 5000 (octal) =5 * 1000 (octal) =5 * 83 =5 * 512 (decimal) =2560 (decimal)+ 700 (octal) =7 * 100 (octal) =7 * 82 =7 * 64 (decimal) =448 (decimal)+ 30 (octal) =3 * 10 (octal) =3 * 81 =3 * 8 (decimal) =24 (decimal)+ 2 (octal) =2 * 1 (octal) =2 * 80 =2 * 1 (decimal) =2 (decimal)Total = 3034 (decimal)We total the decimal values of each octal digit to get the decimal equivalent. So 5732 (octal) is 3034 (decimal).
11Step Divide Equals Remainder Digits (1) 3034 / 8 = 379 2 (3) 379 / 8 = To convert a decimal number to octal, we simply repeat dividing by 8 and saving the remainder.To convert 3034 in decimal to octal, start by dividing 3034 by 8 to get 379 with a remainder of 2.The first remainder will be the last digit in the octal number; the second remainder will be the second digit from the right; and so on. That is, all we need to do is write the remainders right-to-left as we divide by 8:StepDivideEqualsRemainderDigits(1)3034 / 8 =3792(3)379 / 8 =47332(5)47 / 8 =57732(6)5 / 8 =5732
12The Hexadecimal Number System We can use a number system with sixteen numerals. A base 16 number system is call hexadecimal or just hex.We can use our usual 0 through 9 for the first ten digits, then use the letters A for 10, B for 11, C for 12, D for 13, E for 14 and F for 15.
13Counting in Hexadecimal In hexadecimal, we count the same same way we did in decimal and octal. But instead of ten digits or eight digits, we have sixteen, 0-9, A-F. So we resort to changing the digits to the left less frequently than in decimal or octal.Counting in base 16, we get:A B C D E FA 1B 1C 1D 1E 1FA 2B 2C 2D 2E 2F
14Since the hex number A follows 9, A in hex is 10 in decimal Since the hex number A follows 9, A in hex is 10 in decimal. B in hex is 11 in decimal. After F in hex (15 in decimal) we have 10 (16 in decimal). And so on.hex-dec0 - 0...F1 - 1F2 - 2F3 - 3F4 - 4F5 - 5F6 - 6F7 - 7F8 - 8F9 - 9FA - 101A - 262A - 42FA - 25010A - 266B - 111B - 272B - 43FB - 25110B - 267C - 121C - 282C - 44FC - 25210C - 268D - 131D - 292D - 45FD - 25310D - 269E - 141E - 302E - 46F - 151F - 312F - 48FF - 25510F - 271
15Hex Digit Positions and Values In base 16, each digit occupies a position worth sixteen times the position to its right, instead of ten times as in base 10 or eight times as in octal. So if 3F72 is an hex number, it can be read as:3F72 = 3000 (hex) =3 * 1000 (hex) =3 * 163 =3 * 4096 (decimal) =12288 (decimal)+ F00 (hex) =F * 100 (hex) =15 * 162 =15 * 256 (decimal) =3840 (decimal)+ 70 (hex) =7 * 10 (hex) =7 * 161 =7 * 16 (decimal) =112 (decimal)+ 2 (hex) =2 * 1 (hex) =2 * 160 =2 * 1 (decimal) =2 (decimal)Total = (decimal)We total the decimal values of each hex digit to get the decimal equivalent.So 3F72 (hex) is (decimal).
16So 16242 in decimal is written as 3F72 in hex. Converting Decimal to HexWe can convert a decimal to hex using the same procedure we used to convert decimal to octal. The only difference is that we divide by 16 each time since we are working in base 16.In the following steps we convert from decimal to hex:StepDivideEqualsRemainderDigits(1)16242 / 16 =10152 = 2 (hex)2(2)1015 / 16 =637 = 7 (hex)72(3)63 / 16 =315 = F (hex)F72(4)3 / 16 =3 = 3 (hex)3F72So in decimal is written as 3F72 in hex.
17The Binary Number System The base 2 number system, called binary is based on powers of 2 and contains only two digits, 0 and 1.
18Counting in BinaryWith only two numerals, 1 (one) and 0 (zero), counting in binary is pretty simple. Just keep in mind the following:0 + 0 = 00 + 1 = 11 + 0 = 11 + 1 = 10= 11
19We we would count in binary as follows: bin-dec0 - 01 - 110 - 211 - 3
20 Binary Digit Positions and Values In base 2, each digit occupies a position worth two times the position to its right, instead of ten times as in base 10, eight times as in octal, or 16 as in hex. So if is a binary number, it can be read as:
21= (bin) =1 * 26 =1 * 64 (decimal) =64 (decimal)(bin) =1 * 25 =1 * 32 (decimal) =32 (decimal)(bin) =0 * 24 =0 * 16 (decimal) =0 (decimal)(bin) =1 * 23 =1 * 8 (decimal) =8 (decimal)+ 000 (bin) =0 * 22 =0 * 4 (decimal) =+ 00 (bin) =0 * 21 =0 * 2 (decimal) =+ 1 (bin) =1 * 20 =1 * 1 (decimal) =1 (decimal)TOTAL = 105 (decimal)We total the decimal values of each binary digit to get the decimal equivalent. So (binary) is 105 (decimal).
22So 105 in decimal is written as 1101001 in binary. Converting Decimal to BinaryWe can convert a decimal to binary by dividing by 2 each time since we are working in base 2. In the following steps we convert 105 from decimal to binary:StepDivideEqualsRemainderDigits(1)105 / 2 =521(2)52 / 2 =2601(3)26 / 2 =13001(4)13 / 2 =61001(5)6 / 2 =301001(6)3 / 2 =101001(7)1 / 2 =So 105 in decimal is written as in binary.
23Converting Between Hex, Octal and Binary Converting between binary, octal and hex is simple.Binary is base 2. Octal is base 8, and 8 is 23. That is, it takes exactly three binary digits to make one octal digit. If we line up the binary numbers and octal numbers, the connection is even more obvious:bin-octal-dec
24What this means is that we can convert from binary to octal simply by taking the binary digits in groups of three and converting. Consider the binary number If we take the digits in groups of three from right to left and convert, we get:That is, (binary) is (octal).
25Converting from octal to binary is just as easy Converting from octal to binary is just as easy. Since each octal digit can be expressed in exactly three binary digits, all we have to do is convert each octal digit to three binary digits. Converting in octal to binary goes as follows:So (octal) is (binary.)
26Since (almost!) all computers have a binary architecture, octal is very useful to programmers. For humans, octal is more concise, smaller, easier to work with, and less prone to errors than binary. And since it is so easy to convert between binary and octal, octal is a favored number system for programmers.
27In the same way, hex is base 16 and 16 is 24 In the same way, hex is base 16 and 16 is 24. That is, it takes exactly four binary digits to make a hex digit. By taking binary digits in groups of four (right to left) we can convert binary to hex. Consider once more the binary number By grouping in fours and converting, we get:D C B BSo (binary) is the same number as 2D3CB8B (hex), and the same number as (octal).
28Since we can drop the leading zero, Converting from hex to binary, simply write each hex digit as four binary digits. In this way we can convert 6F037C2:F CSince we can drop the leading zero,6F037C2 (hex) is (binary).
29These days, octal tends to be used by programmers who come from a mini-computer background or who work with unicode. Hex tends to be preferred by programmers with a mainframe background or who work with colors. Many programmers are at home with either.
30nibble In computers and digital technology, a nibble is four binary digits or half of an eight-bit byte.Maximum number in a nibble :1111 = 15A nibble can be conveniently represented by one hexadecimal digit.
31BCDBinary-coded decimal, or BCD, is a method of using binary digits to represent the decimal digits 0 through 9. A decimal digit is represented by four binary digits, as shown below:The binary combinations 1010 to 1111 are invalid and are not used.
32UNPACKED & PACKED BCDIn unpacked BCD, a decimal digit is stored in one byte and in packed BCD, four bits are used to keep one digit, so two digits are stored in one byte, one in the lower half and one in the upper half of the byte.BCD and binary are not the same.For example, 4910 in binary is ,but 4910 in BCD is BCD.= BCD (PACKED BCD)Each decimal digit is converted to its binary equivalent.
33BCD Conversion The BCD equivalent of 26410 is 001001100100BCD Conversion of decimal to BCD or BCD to decimal is similar to the conversion of hexadecimal to binary and vice versa.For example, let's go through the conversion of to BCD. We'll use the block format that you used in earlier conversions. First, write out the decimal number to be converted; then, below each digit write the BCD equivalent of that digit:The BCD equivalent of is BCD
34The DECIMAL equivalent of 100110000011BCD is 98310 To convert from BCD to decimal, simply reverse the process as shown:The DECIMAL equivalent of BCD is
35ASCII CodeASCII, pronounced "ask-key", is the common code for microcomputer equipment. The standard ASCII character set consists of 128 decimal numbers ranging from zero through 127 assigned to letters, numbers, punctuation marks, and the most common special characters.The Extended ASCII Character Set also consists of 128 decimal numbers and ranges from 128 through 255 representing additional special, mathematical, graphic, and foreign characters.
36The Standard ASCII character set is divided into four groups of 32 characters. The first 32 characters, form a special set of non-printing characters called the control characters. We call them control characters because they perform various printer/display control operations rather than displaying symbols.
37Examples of common control characters include: carriage return which positions the cursor to the left side of the current line of characters,line feed which moves the cursor down one line on the output deviceback space which moves the cursor back one position to the leftUnfortunately, different control characters perform different operations on different output devices. There is very little standardization among output devices. To find out exactly how a control character affects a particular device, you will need to consult its manual.
38The second group of 32 ASCII character codes comprise various punctuation symbols, special characters, and the numeric digits. The most notable characters in this group include the:space characternumeric digits 0 through 9Note that the numeric digits differ from their numeric values only in the high order nibble. By subtracting 30h from the ASCII code for any particular digit you can obtain the numeric equivalent of that digit.
39The third group of 32 ASCII characters is reserved for the upper case alphabetic characters. The ASCII codes for the characters "A" through "Z" lie in the range 41h through 5Ah. Since there are only 26 different alphabetic characters, the remaining six codes hold various special symbols.The fourth, and final, group of 32 ASCII character codes are reserved for the lower case alphabetic symbols, five additional special symbols, and another control character (delete).
40ISCII Indian Script Code for Information Interchange The ISCII code table is a super-set of all the characters required in the ten Brahmi-based Indian scripts. An optimal keyboard overlay for these scripts is made possible by the phonetic nature of the alphabet. The differences between scripts primarily are in their written forms, where different combination rules get used.
41The 8-bit ISCII code retains the standard ASCII code, while the Indian script keyboard overlay is designed for the standard English can co-exist with Indian scripts. This approach also makes it feasible to use Indian scripts along with existing English computers and software, so long as 8-bit character codes are allowed.
42The common INSCRIPT keyboard overlay allows typing of all the ten Indian scripts. This overlays fits on any existing English keyboard. Alternating between the English and Inscript overlay is achieved through the CAPSLOCK key.The INSCRIPT keyboard, provides a logical and intuitive arrangement of vowels and consonants. It is based both on the phonetic properties and the relative usage frequencies of the letters. Not only does this made the keyboard much easier to learn, but also enables a person to type subsequently in all the Indian scripts.
44EBCDIC Extended Binary Coded Decimal Interchange Code It is an 8 bit character encoding used on IBM mainframes and AS/400s. It is descended from punched cards and the corresponding six bit Binary Coded Decimal Code that most of IBM's computer peripherals of the late 1950s and early 1960s used. Outside of such IBM systems, ASCII are normally used instead; EBCDIC is generally considered an anachronism.EBCDIC takes up eight bits, which are divided in two pieces. The first four bits are called the zone and represent the category of the character, whereas the last four bits are the called the digit and identify the specific character. There are a number of different versions of EBCDIC, customised for different countries.
46The Binary Addition Algorithm To add two 1-bit (representations of) integers: Count the number of ones in a column and write the result in binary. The right bit of the result is placed under the column of bits. The left bit is called the "carry out of the column".1---01101100
50Signed Binary Integers We will not be using a minus sign (-) to represent negative numbers. We would like to represent our binary numbers with only two symbols, 0 and 1. There are a few ways to represent negative binary numbers. The simplest of these methods is called ones complement, where the sign of a binary number is changed by simply toggling each bit (0's become 1's and vice-versa). This has some difficulties, among them the fact that zero can be represented in two different ways (for an eight bit number these would be and )., we will use a method called two's complement notation which avoids the pitfalls of one's complement.
51The value of bits in signed and unsigned binary numbers To represent an n bit signed binary number the leftmost bit, has a special significance. The difference between a signed and an unsigned number is given in the table below for an 8 bit number.The value of bits in signed and unsigned binary numbersBit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0Unsigned27 = 12826 = 6425 = 3224= 1623= 822 = 421 = 220 = 1Signed-(27) = -12824 = 1623 = 821= 2
52Let's look at how this changes the value of some binary numbers If Bit 7 is not set (as in the first example) the representation of signed and unsigned numbers is the same. However, when Bit 7 is set, the number is always negative. For this reason Bit 7 is sometimes called the sign bit.BinaryUnsignedSigned35163-93255-1128-128
53NEGATIVE BINARY NUMBER Usually we represent a negative decimal number by placing a minus sign directly to the left of the most significant digit, just as in the example above, with -5. Since binary uses only 0 & 1, we don't have a third symbol such as a "minus" sign, since these circuits can only be on or off (two possible states). One solution is to reserve a bit (circuit) that does nothing but represent the mathematical sign:= 510 (positive).. Extra bit, representing sign (0=positive, 1=negative)|= 510 (positive)= -510 (negative)
54Extra bit, place weight = negative eight . | . 10112 = 510 (negative) There's another method for representing negative numbers called complementation. With this strategy, we assign the leftmost bit to serve a special purpose, just as we did with the sign-magnitude approach, defining our number limits just as before. However, this time, the leftmost bit is more than just a sign bit; rather, it possesses a negative place-weight value. For example, a value of negative five would be represented as such:Extra bit, place weight = negative eight|= 510 (negative).. (1 x -810) + (0 x 410) + (1 x 210) + (1 x 110) = -510With the right three bits being able to represent a magnitude from zero through seven, and the leftmost bit representing either zero or negative eight, we can successfully represent any integer number from negative seven (10012 = = -110) to positive seven (01112 = = 710).
55Examples …. zero 0000 positive one 0001 negative one 1111 positive two negative twopositive three 0011 negative three 1101positive four negative four 1100positive five negative five 1011positive six negative sixpositive seven 0111 negative sevenNote that the negative binary numbers in the right column, being the sum of the right three bits' total plus the negative eight of the leftmost bit, don't "count" in the same progression as the positive binary numbers in the left column. Rather, the right three bits have to be set at the proper value to equal the desired (negative) total when summed with the negative eight place value of the leftmost bit.
56The two’s complement :The two's complement for any positive binary number will be whatever value is needed to add to the binary number to make that positive value's negative equivalent.To get the two’s complement, simply invert all the bits of that number, changing all 1's to 0's and visa-versa (to arrive at what is called the one's complement) and then add one!For example, to obtain the two's complement of five (1012), we would first invert all the bits to obtain 0102 (the "one's complement"), then add one to obtain 0112, or -510 in three-bit, two's complement form.
57Interestingly enough, generating the two's complement of a binary number works the same if you manipulate all the bits, including the leftmost (sign) bit at the same time as the magnitude bits.Let's try this with the former example, converting a positive five to a negative five, but performing the complementation process on all four bits. We must be sure to include the 0 (positive) sign bit on the original number, five (01012). First, inverting all bits to obtain the one's complement: Then, adding one, we obtain the final answer: 10112, or -510 expressed in four-bit, two's complement form.
58It is critically important to remember that the place of the negative-weight bit must be already determined before any two's complement conversions can be done. If our binary numeration field were such that the eighth bit was designated as the negative-weight bit ( ), we'd have to determine the two's complement based on all seven of the other bits. Here, the two's complement of five ( ) would be A positive five in this system would be represented as , and a negative five as
59SubtractionWe can subtract one binary number from another by using the standard techniques adapted for decimal numbers (subtraction of each bit pair, right to left, "borrowing" as needed from bits to the left). However, if we can leverage the already familiar (and easier) technique of binary addition to subtract, that would be better. As we just learned, we can represent negative binary numbers by using the "two's complement" method and a negative place-weight bit. Here, we'll use those negative binary numbers to subtract through addition.
60Addition equivalent: 710 + (-510) Subtraction:Addition equivalent: (-510)Represent seven and negative five in binary (two's complemented) form, all we need is three bits plus the negative-weight bit: positive seven = negative five = 10112Now, let's add them together:<--- Carry bits|Discard extra bit.. Answer = 00102Since we've already defined our number bit field as three bits plus the negative-weight bit, the fifth bit in the answer (1) will be discarded to give us a result of 00102, or positive two, which is the correct answer.
61Another example : add -2510 to 1810 If we want to add to 1810, we must first decide how large our binary bit field must be. To represent the largest (absolute value) number in our problem, which is twenty-five, we need at least five bits, plus a sixth bit for the negative-weight bit.+2510 = (showing all six bits)One's complement of =One's complement + 1 = two's complement =-2510 =Essentially, we're representing negative twenty-five by using the negative-weight (sixth) bit with a value of negative thirty-two, plus positive seven (binary 1112).Now, let's represent positive eighteen in binary form, showing all six bits:=.. Now, let's add them together<--- Carry bitsSince there were no "extra" bits on the left, there are no bits to discard. The leftmost bit on the answer is a 1, which means that the answer is negative, in two's complement form, as it should be. Converting the answer to decimal form by summing all the bits times their respective weight values, we get:(1 x -3210) + (1 x 1610) + (1 x 810) + (1 x 110)= -710
62OverflowOne problrm with signed binary numbers is that of overflow, where the answer to an addition or subtraction problem exceeds the magnitude which can be represented with the alloted number of bits. Remember that the place of the sign bit is fixed from the beginning of the problem. With the last example problem, we used five binary bits to represent the magnitude of the number, and the left-most (sixth) bit as the negative-weight, or sign, bit. With five bits to represent magnitude, we have a representation range of 25, or thirty-two integer steps from 0 to maximum. This means that we can represent a number as high as ( ), or as low as ( ). If we set up an addition problem with two binary numbers, the sixth bit used for sign, and the result either exceeds or is less than -3210, our answer will be incorrect.
63. . 1 11 <--- Carry bits . (Showing sign bits) 010001 . + 010011 Let's try adding 1710 and 1910 to see how this overflow condition works for excessive positive numbers:= =.<--- Carry bits. (Showing sign bits)The answer ( ), interpreted with the sixth bit as the place, is actually equal to -2810, not as we should get with and added together!
64The answer ( ), interpreted with the sixth bit as the place, is actually equal to -2810, not as we should get with and added together! Obviously, this is not correct. What went wrong? The answer lies in the restrictions of the six-bit number field within which we're working Since the magnitude of the true and proper sum (3610) exceeds the allowable limit for our designated bit field, we have an overflow error. Simply put, six places doesn't give enough bits to represent the correct sum, so whatever figure we obtain using the strategy of discarding the left-most "carry" bit will be incorrect.
65. . 1 1111 <--- Carry bits . (Showing sign bits) 101111 . + 101101 A similar error will occur if we add two negative numbers together to produce a sum that is too low for our six-bit binary field. Let's try adding and together to see how this works (or doesn't work, as the case may be!):= =.<--- Carry bits. (Showing sign bits)|Discard extra bitFINAL ANSWER: = +2810The (incorrect) answer is a positive twenty-eight. The fact that the real sum of negative seventeen and negative nineteen was too low to be properly represented with a five bit magnitude field and a sixth sign bit is the root cause of this difficulty.
66Let's try these two problems again, except this time using the seventh bit for a sign bit, and allowing the use of 6 bits for representing the magnitude:(-1710) + (-1910).|Discard extra bit. ANSWERS: = = -3610By using bit fields sufficiently large to handle the magnitude of the sums, we arrive at the correct answers.
67In these sample problems we've been able to detect overflow errors by performing the addition problems in decimal form and comparing the results with the binary answers. For example, when adding and together, we knew that the answer was supposed to be +3610, so when the binary sum checked out to be -2810, we knew that something had to be wrong. Although this is a valid way of detecting overflow, it is not very efficient. After all, the whole idea of complementation is to be able to reliably add binary numbers together and not have to double-check the result by adding the same numbers together in decimal form!What we need is a simple error-detection method that doesn't require any additional arithmetic. Perhaps the most elegant solution is to check for the sign of the sum and compare it against the signs of the numbers added. Obviously, two positive numbers added together should give a positive result, and two negative numbers added together should give a negative result. Notice that whenever we had a condition of overflow in the example problems, the sign of the sum was always opposite of the two added numbers: plus giving -2810, or plus givingBy checking the signs alone we are able to tell that something is wrong.