Number System (Different Ways To Say How Many) Fall 2016 Introduction to Information and Communication Technologies CSD 102 Email: mehwish.fatima@ciitlahore.edu.pk Website: https://sites.google.com/a/ciitlahore.edu.pk/ict/
Outline Numeral System Decimal System Binary System Octal System Hexadecimal System Digit and Positions Bits, Bytes & Words Conversions Negative Numbers 2
Numeral System A Numeral System is a writing system for expressing numbers, a mathematical notation for representing numbers of a given set, using symbols in a consistent manner. Ideally, a numeral system will: Represent a useful set of numbers (integers, or rational numbers) Give every number represented a unique representation (or a standard representation) Reflect the algebraic and arithmetic structure of the numbers. 3
Base/Radix of Numeral System The number of distinct symbols that can be used to represent numbers in that system. For example, the base for the decimal numerical symbol is 10, as we can use the ten symbols 0,1,2,,9 to represent numbers in this system. This means that the same number will have different representations in different systems. So, 101 means a hundred and one in decimal, and five, in binary; while 5 means five in decimal but is invalid in binary as the symbol 5 is not allowed in binary. 4
Decimal Number System The decimal numeral system (base ten or denary) has ten as its base. It is the numerical base most widely used by modern civilizations. 5
Binary Number System The binary numeral system (base two) has two as its base. It is the numerical base used by the modern day computers where numbers need to be stored using the on/off logic of electronic and/or magnetic media. The ON and OFF conveniently translate into 1 and 0. 6
Octal Number System The octal numeral system (base eight) has eight as its base. 7
Hexadecimal Number System The hexadecimal numeral system (base 16) has sixteen as its base. This system uses the 16 symbols: {0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F} The main advantage of using this numeral system is that it keeps the representations short, and hence manageable. For example: the decimal number 199834, is represented in binary as 110000110010011010; and in hexadecimal as 30C9A. 8
Why we need all these systems The length of representations can grow very rapidly in binary, and even decimal, while slowly in hexadecimal, as extra symbols are allowed. So we need hexadecimal to keep things manageable on the paper. Computers need binary numbers, but humans can work better with decimal, octal and hexadecimal. Hence we should know all these systems and how to convert numbers between these different representations. 9
Digit & Position The length of a representation grows, from right to left, like: 0,1, 2, 3, so on. Let s take the example of the decimal system. The number nine, in decimal, consists of one digit 9 at position 0. Similarly, all the numbers 0, 1, 2,9 all consist of a single digit at position 0. But once we reach 9, we have run out of new symbols (in decimal system) and hence we must increase the length. Therefore we add another digit to the left, at position 1. Hence we get 10, 11,12,19,20,21,,99. when again, we run out of all length two combinations of symbols and must proceed to length three representations, starting from 100, so on. 10
Digit & Position In general, a number x may be represented with a representation of length n in the following manner (here d p means the digit at position p). d n-1 d 3 d 2 d 1 d 0 So for the number 199834, in decimal, d 0 4, d 1 3, d 2 8, d 3 9, d 4 9 and d 5 1. In general, We call the rightmost digit, d 0, the least significant digit (LSB) and the leftmost digit d n 1, the most significant digit (MSB). 11
BITS, BYTES and WORDS A digit in the binary system is more commonly called a bit. Therefore, a bit is a single digit in the binary representation of a number and is either 0 or 1. When a binary number is represented using 8 bits, the resulting representation, composed of d 0, d 1, d 2 d 7, is called a byte. Similarly a binary representation composed of 16 bits is called a word, a binary representation composed of 32 bits is called a double word, and a binary representation composed of 64 bits is called a quadruple word. 12
BITS, BYTES and WORDS A less common, 4 bit representation of numbers is called nibble. The concept of a byte is fundamental in computer science because a byte is the smallest addressable unit of memory in a modern computer; furthermore, data is quantified in terms of byte! When you say that your hard disk can store 20 Giga Bytes, you really mean that it can store 20x2 30 bytes of data. A byte is therefore a unit for measuring data in computers. 13
BITS, BYTES and WORDS The following quantifiers are important: KILO 1K 2 10 So how many bytes in 37KB? MEGA 1M 2 20 So how many Kilo bytes in 137MB? GIGA 1G 2 30 So how many Tera bytes in 562MB? TERA 1T 2 40 So how Giga bytes in 307TB? 14
Representation of a Number What if I know the digits and want to get the actual representation for the number from them? Assuming I cannot simply paste them together, but actually have to do a computation. I will do something like the following: 199834 4x1+ 3x10 + 8x100 + 9x1000 + 9x10000 + 1x100000 4x10 0 +3x10 1 +8x10 2 +9x10 3 +9x10 4 +1x10 5 In general, for the decimal system: D ; where p represents the positions in an n digit number in the decimal system. 15
Hold on! Can we use this method to convert a number from any other base to decimal? Yes, of course we can! 16
Method-I We can rewrite the above formula for any general (Base) radix r, which may be 10, 2, 8, 16 or anything we wish. Hence, we get: r Let s call this Method I By this method we can compute any representation of any base (10, 2, 8, 16 ) and find its equivalent value in base 10. 17
Converting numbers from other bases to their decimal equivalents Binary 1001 (so r2) : D 1x2 0 +0x2 1 +0x2 2 +1x2 3 1+0+0+8 9 Octal 1767 (so r8) : D 7x8 0 +6x8 1 +7x8 2 +1x8 3 7+48+448+512 1015 Hexadecimal 12AC (so r16): D Cx16 0 +Ax16 1 +2x16 2 +1x16 3 12+160+512+4096 4780 (A represents a decimal 10 and C represents a decimal 12) 18
This is kool! But how do we solve the converse problem? How do we convert decimal numbers into binary, octal and hexadecimal? 19
Decimal to Binary Conversion We want to convert the decimal number 1233 into its binary equivalent. The binary digits we are looking for are: d 0, d 1, d 2, d 3 so on. How do we find d 0? It does not seem obvious. What will be get if we divide 1233 by 2? Something like the following: ; where rem is the remainder we get when we divide 1233 by 2, and q is the quotient. The rem here could only be either 1 or 0 for now I will simply take this as d0 (you will soon see why I am doing this). In general, for a decimal number, 9/26/2016 Mehwish Fatima- CIIT Lahore 20
Decimal to Binary Conversion Now let me repeat this process on the number q Can you see what is happening? Continuing this process we will reach a point where the quotient cannot be further divided. 21
Decimal to Binary Conversion And at this point we will have derived the following formula: Which is exactly the same as Method I!!! So it turns out that by dividing we were only doing the opposite operation of what we had done before. Hence our assumption of taking the successive remainders as d 0, d 1 and so on was in fact correct! 22
Converting Decimal Numbers into their Equivalents in other Bases Generally, if we want to convert a decimal number D into the equivalent number in base r we will simply divide the number D by the base r. And then the successive quotients, by r, and pick the successive remainders as digits d 0, d 1 and so on, of the equivalent number in base r. We call this Method II. 23
Conversion from Base r to Base s Now we posses enough knowledge to covert a number from any given base to a desired base. Simply, we first convert the number into decimal using Method I, and then, from decimal to the desired base, using Method II. So if you want to convert a number in base r to its equivalent number in base s, you simply follow the following path: 24
Direct Conversions Sometimes it is much easier to convert numbers between bases directly (without first converting them into decimal). You should know these methods for speeding up your calculations. Here are some sources for learning these: http://www.mindspring.com/~jimvb/binary.htm http://atrevida.comprenica.com/atrtut01.html 25
We have seen positive numbers representation till now, how would negative numbers will represent in binary system 26
Negative Number How to represent negative number in binary system Signed Magnitude 1 s Complement 2 s Complement 27
Signed Magnitude Use the leftmost digit as a sign indication, and treat the remaining bits as if they represented an unsigned integer. The convention is that if the leftmost digit (most significant bit) is 0 the number is positive, if it s 1 the number is negative. 00001010 decimal 10 10001010 decimal 10 28
Signed Magnitude The main problem with this system is that it doesn t support binary arithmetic (which is what the computer would naturally do). That is, if you add 10 and 10 binary you won t get 0 as a result. 00001010 (decimal 10) + 10001010 (decimal 10) 10010100 (decimal 20) 29
1 s Complement To obtain one s complement, you simply need to flip all the bits. Suppose we are working with unsigned integers. Decimal 10 is represented as: 00001010 It s one complement would be: 11110101 Notice that the complement is 245, which is 255 10. That is no co incidence. The complement of a number is the largest number represented with the number of bits available minus the number itself. Since we are using 8 bits here the maximum number represented is 255 (2^32 1). So the complement of 10 will be 245. 30
1 s Complement using the one s complement system to represent negative numbers we would have two zeroes: 00000000 (could be seen as +0) and 11111111 (could be seen as 0). 00001100 (decimal 12) +11110011 (decimal 12) 11111111 (decimal 0) 31
1 s Complement this system partially solves the binary arithmetic problem because there are some special cases left. 00000011 (decimal 3) +11111101 (decimal 2) 100000000 (decimal 256) But since we have only 8 bits to represent the numbers, the leftmost 1 will be discarded, and the result would be 00000000 (decimal +0). This is not the answer we expected. 32
2 s Complement Two s Complement of a binary number is basically another number which, when added to the original, will make all bits become zeroes. You find a two s complement by first finding the one s complement, and then by adding 1 to it. 11110011 (one's complement of 12) +00000001 (decimal 1) 11110100 (two's complement of 12) 33
2 s Complement Now let s add 12 with 5 to see if we ll have the same problem that we had when using the one s complement system: 00001100 (decimal 12) +11111011 (decimal 5) 00000111 (decimal 7) 34
35
Number Overflow Overflow occurs when the value that we compute cannot fit into the number of bits we have allocated for the result. For example, if each value is stored using eight bits, adding 127 to 3 would overflow: 01111111 + 00000011 10000010 36
Code for Letters and Symbols Representing text strings, such as Hello, world, in a computer Computers use a standard binary code to represents letters of the alphabet, numerals, punctuation marks and other special characters. Each character is coded as a byte. The code is called ASCII (pronounced askey ) which stands for American Standard Code for Information Interchange. There are 256 code combinations. 37
ASCII Features 7 bit code (Use 1 Byte) 2 7 128 codes 000 001 010 011 100 101 110 111 0000 NULL DLE 0 @ P ` p 0001 SOH DC1! 1 A Q a q 0010 STX DC2 " 2 B R b r 0011 ETX DC3 # 3 C S c s 0100 EDT DC4 $ 4 D T d t 0101 ENQ NAK % 5 E U e u 0110 ACK SYN & 6 F V f v 0111 BEL ETB ' 7 G W g w 1000 BS CAN ( 8 H X h x 1001 HT EM ) 9 I Y i y 1010 LF SUB * : J Z j z 1011 VT ESC + ; K [ k { 1100 FF FS, < L \ l 1101 CR GS - M ] m } 1110 SO RS. > N ^ n ~ 1111 SI US /? O _ o DEL Two general types of codes: 95 are Graphic codes (displayable on a console) 33 are Control codes (control features of the console or communications channel) CIIT Lahore-Mehwish Fatima 26/09/2016 38
ASCII 000 001 010 011 100 101 110 111 0000 NULL DLE 0 @ P ` p 0001 SOH DC1! 1 A Q a q 0010 STX DC2 " 2 B R b r 0011 ETX DC3 # 3 C S c s 0100 EDT DC4 $ 4 D T d t Most significant bit 0101 ENQ NAK % 5 E U e u 0110 ACK SYN & 6 F V f v 0111 BEL ETB ' 7 G W g w 1000 BS CAN ( 8 H X h x 1001 HT EM ) 9 I Y i y 1010 LF SUB * : J Z j z 1011 VT ESC + ; K [ k { 1100 FF FS, < L \ l 1101Least CR significant GS bit - M ] m } 1110 SO RS. > N ^ n ~ 1111 SI US /? O _ o DEL CIIT Lahore-Mehwish Fatima 26/09/2016 39
e.g., a 1100001 000 001 010 011 100 101 110 111 0000 NULL DLE 0 @ P ` p 0001 SOH DC1! 1 A Q a q 0010 STX DC2 " 2 B R b r 0011 ETX DC3 # 3 C S c s 0100 EDT DC4 $ 4 D T d t 0101 ENQ NAK % 5 E U e u 0110 ACK SYN & 6 F V f v 0111 BEL ETB ' 7 G W g w 1000 BS CAN ( 8 H X h x 1001 HT EM ) 9 I Y i y 1010 LF SUB * : J Z j z 1011 VT ESC + ; K [ k { 1100 FF FS, < L \ l 1101 CR GS - M ] m } 1110 SO RS. > N ^ n ~ 1111 SI US /? O _ o DEL CIIT Lahore-Mehwish Fatima 26/09/2016 40
95 Graphic codes 000 001 010 011 100 101 110 111 0000 NULL DLE 0 @ P ` p 0001 SOH DC1! 1 A Q a q 0010 STX DC2 " 2 B R b r 0011 ETX DC3 # 3 C S c s 0100 EDT DC4 $ 4 D T d t 0101 ENQ NAK % 5 E U e u 0110 ACK SYN & 6 F V f v 0111 BEL ETB ' 7 G W g w 1000 BS CAN ( 8 H X h x 1001 HT EM ) 9 I Y i y 1010 LF SUB * : J Z j z 1011 VT ESC + ; K [ k { 1100 FF FS, < L \ l 1101 CR GS - M ] m } 1110 SO RS. > N ^ n ~ 1111 SI US /? O _ o DEL CIIT Lahore-Mehwish Fatima 26/09/2016 41
33 Control codes 000 001 010 011 100 101 110 111 0000 NULL DLE 0 @ P ` p 0001 SOH DC1! 1 A Q a q 0010 STX DC2 " 2 B R b r 0011 ETX DC3 # 3 C S c s 0100 EDT DC4 $ 4 D T d t 0101 ENQ NAK % 5 E U e u 0110 ACK SYN & 6 F V f v 0111 BEL ETB ' 7 G W g w 1000 BS CAN ( 8 H X h x 1001 HT EM ) 9 I Y i y 1010 LF SUB * : J Z j z 1011 VT ESC + ; K [ k { 1100 FF FS, < L \ l 1101 CR GS - M ] m } 1110 SO RS. > N ^ n ~ 1111 SI US /? O _ o DEL CIIT Lahore-Mehwish Fatima 26/09/2016 42
Alphabetic codes 000 001 010 011 100 101 110 111 0000 NULL DLE 0 @ P ` p 0001 SOH DC1! 1 A Q a q 0010 STX DC2 " 2 B R b r 0011 ETX DC3 # 3 C S c s 0100 EDT DC4 $ 4 D T d t 0101 ENQ NAK % 5 E U e u 0110 ACK SYN & 6 F V f v 0111 BEL ETB ' 7 G W g w 1000 BS CAN ( 8 H X h x 1001 HT EM ) 9 I Y i y 1010 LF SUB * : J Z j z 1011 VT ESC + ; K [ k { 1100 FF FS, < L \ l 1101 CR GS - M ] m } 1110 SO RS. > N ^ n ~ 1111 SI US /? O _ o DEL CIIT Lahore-Mehwish Fatima 26/09/2016 43
Punctuation, etc. 000 001 010 011 100 101 110 111 0000 NULL DLE 0 @ P ` p 0001 SOH DC1! 1 A Q a q 0010 STX DC2 " 2 B R b r 0011 ETX DC3 # 3 C S c s 0100 EDT DC4 $ 4 D T d t 0101 ENQ NAK % 5 E U e u 0110 ACK SYN & 6 F V f v 0111 BEL ETB ' 7 G W g w 1000 BS CAN ( 8 H X h x 1001 HT EM ) 9 I Y i y 1010 LF SUB * : J Z j z 1011 VT ESC + ; K [ k { 1100 FF FS, < L \ l 1101 CR GS - M ] m } 1110 SO RS. > N ^ n ~ 1111 SI US /? O _ o DEL CIIT Lahore-Mehwish Fatima 26/09/2016 44
Numeric codes 000 001 010 011 100 101 110 111 0000 NULL DLE 0 @ P ` p 0001 SOH DC1! 1 A Q a q 0010 STX DC2 " 2 B R b r 0011 ETX DC3 # 3 C S c s 0100 EDT DC4 $ 4 D T d t 0101 ENQ NAK % 5 E U e u 0110 ACK SYN & 6 F V f v 0111 BEL ETB ' 7 G W g w 1000 BS CAN ( 8 H X h x 1001 HT EM ) 9 I Y i y 1010 LF SUB * : J Z j z 1011 VT ESC + ; K [ k { 1100 FF FS, < L \ l 1101 CR GS - M ] m } 1110 SO RS. > N ^ n ~ 1111 SI US /? O _ o DEL CIIT Lahore-Mehwish Fatima 26/09/2016 45
Common Control Codes CR 0D carriage return LF 0A line feed HT 09 horizontal tab DEL 7F delete NULL 00 null Hexadecimal code CIIT Lahore-Mehwish Fatima 26/09/2016 46
Hello, world Example 26/09/2016 CIIT Lahore-Mehwish Fatima 47 Binary 01001000 01100101 01101100 01101100 01101111 00101100 00100000 01110111 01100111 01110010 01101100 01100100 Hexadecimal 48 65 6C 6C 6F 2C 20 77 67 72 6C 64 Decimal 72 101 108 108 111 44 32 119 103 114 108 100 H el l o, w or l d
Sources [1] http://en.wikipedia.org/wiki/numeral_systems [2] http://www.mathsisfun.com/definitions/base numbers.html [3] http://atrevida.comprenica.com/atrtut01.html [4] http://www.mindspring.com/~jimvb/binary.htm 48