Introduction to Numbering System What is a numbering
Introduction to Numbering System • What is a numbering system ? – A method to count; know quantity • Many ways to represent numbers – For humans we use decimal numbering system • 0, 1, 2, …. 9, pattern keeps repeating, till infinity • Why 10 digits? Not sure…. . Take a guess – Computers use binary numbering system Computers only understand zeros and ones • Computers are made of electronic circuit which has current flowing through it. Voltage makes the current flow • Only two levels of voltage (there always exceptions) Zero volt = ZERO = ground = OFF +5 volt (or 3. 5 V) = ONE = ON 2000 Prentice Hall, Inc. All rights reserved. 2001 NHTI CP 107 M. Saleem Yusuf. 1
Why? • Why do we have numbering systems? – So we can count • Why so many numbering systems? – Computers can only understand zeros and ones – Decimal numbering systems started long before the dawn of computes and is easy for humans to use – Not easy to use zeros and ones for daily use, takes to may bits to represent a small amount • Example • 65536 in decimal takes five digits • 65536 in binary takes 16 bits 2000 Prentice Hall, Inc. All rights reserved. 2001 NHTI CP 107 M. Saleem Yusuf. 2
Introduction to Numbering System • Decimal • Hexadecimal • Octal • Binary 2000 Prentice Hall, Inc. All rights reserved. 2001 NHTI CP 107 M. Saleem Yusuf. 3
Decimal Most commonly used numbering system Base 10, there are ten unique units (digits) 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Pattern starts at 0 and ends with base – 1 then keeps repeating, tens, hundreds, thousands, … 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29. . . . . 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 2000 Prentice Hall, Inc. All rights reserved. 2001 NHTI CP 107 M. Saleem Yusuf. 4
Hexadecimal • Used extensively in computer programming Base 16, 16 digits, From 0 to f (0 to base – 1) 0 1 10 11 20 21 30 31 2 12 22 32 3 4 5 6 7 13 14 15 16 17 23 24 25 26 27 33 34 35 36 37 0 to F 10 to FF 100 to FFF 1000 to FFFF 8 18 28 38 9 19 29 39 a 1 a 2 a 3 a b 1 b 2 b 3 b c 1 c 2 c 3 c d 1 d 2 d 3 d e 1 e 2 e 3 e f 1 f 2 f 3 f int x = 0 x. FF; // 255 in decimal int x = 0 x. FFFF; // 65536 in decimal int x = 0 x 7 FFFFFFF; //2, 147, 483, 647 in decimal 2000 Prentice Hall, Inc. All rights reserved. 2001 NHTI CP 107 M. Saleem Yusuf. 5
Octal • WAS used extensively in computer programming a while ago, replaced by Hex • Still used in debuggers and assembly language programs – Base 8, 8 digits from 0 to 7 ( 0 to base-1) • 0 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17 20 21 22 23 24 25 26 27 int y = 011; // 9 in decimal 2000 Prentice Hall, Inc. All rights reserved. 2001 NHTI CP 107 M. Saleem Yusuf. 6
Binary • Language of computers • Computer only understand 0 s and 1 s Base 2, 2 variations of 1 bit 0 and 1 (0 to base – 1) 0 1 one bit = 2 unique values 00 01 10 11 two bits = 4 unique values 100 101 110 111 three bits = 8 unique values 1000 1001 1010 1011 1100 1101 1110 1111 2000 Prentice Hall, Inc. All rights reserved. 2001 NHTI CP 107 M. Saleem Yusuf. 7
Bits, Bytes and Words 1 byte has 8 bits 1 word has 16 bits 1 double word has 32 bits • Byte is the smallest unit of memory allocated • An int is 32 bits, one double word • 000000010 00000011 00000100 00000101 00000110 00000111 00001000 00001001 00001100 00001011 00001100 example: 00000001 in binary = 1 in decimal 00000011 in binary = 3 in decimal 2000 Prentice Hall, Inc. All rights reserved. 2001 NHTI CP 107 M. Saleem Yusuf. 8
Binary 2 bits can represent 4 (22) unique combinations From 00 01 10 11 (0, 1, 2, 3) 4 bits can represent 16 (24) unique combinations From 0000 (0) to 1111 (0. . 15) 8 bits can represent 256 (28) unique combinations From 0000 (0) to 1111 (0. . 255) ) 8 bits = 1 byte = 28 = 256 unique combinations 16 bits = 2 bytes = 216 = 65536 unique combinations 32 bits = 4 bytes = 232 = 4, 294, 967, 296 (~ 4. 2 billion) 2000 Prentice Hall, Inc. All rights reserved. 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 2001 NHTI CP 107 M. Saleem Yusuf. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 9
Binary MSB <<<LSB 128 64 32 16 8 4 2 1 27 26 25 24 23 22 21 20 ----------------------0 0 0 0 = 0 0 0 0 1 = 1 0 0 0 1 0 = 2 0 0 0 1 1 = 3 (2+1) 0 0 1 1 = 15 (8+4+2+1) 0 1 1 1 0 0 = 112 (64+32+16) 1 0 0 0 0 = 128 or -1 1 1 1 1 = 255 or -128 8 bits = 1 byte in binary system, can represent 256 numbers 2 8 bits can represent 256 numbers 2 16 bits can represent 65536 numbers 2000 Prentice Hall, Inc. All rights reserved. 2001 NHTI CP 107 M. Saleem Yusuf. 10
Conversion 8 bits = 1 byte = 256 unique combinations Binary Octal Decimal Hex 00000001 00000010 00000011 00000100 00000101 00000110 00000111 00001000 00001001 00001010 00001011 00001100 00001101 00001110 00001111 -------1111 0 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17 -377 2000 Prentice Hall, Inc. All rights reserved. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 1 2 3 4 5 6 7 8 9 A B C D E F --255 -FF 2001 NHTI CP 107 M. Saleem Yusuf. 11
Conversion – bin to hex, octal Binary Number 100011010001 = Octal 4321 = Hexadecimal 8 D 1 • To convert a binary number to Octal, split binary number into pairs of three (need three binary digits to represent an octal) 100 011 010 001 4 3 2 1 (bin 001 = octal 1, bin 100 = octal 4) • To convert a binary number to hex, split binary number into pairs of four (need four binary digits to represent a hex num) 1000 1101 0001 8 D 1 (binary 1000 = hex 8 binary 1101 = decimal 13 = hex D) 2000 Prentice Hall, Inc. All rights reserved. 2001 NHTI CP 107 M. Saleem Yusuf. 12
Conversion – hex, octal to bin • Hexadecimal to Binary 8 D 1 8 1000 D 1101 1 0001 = 100011010001 • Octal to Binary 4321 4 3 100 011 2 010 2000 Prentice Hall, Inc. All rights reserved. 1 001 = 100011010001 2001 NHTI CP 107 M. Saleem Yusuf. 13
Conversion to Decimal • Binary to Decimal Pos Val Bin Val 27 128 1 26 64 1 25 32 0 24 16 1 23 8 0 22 4 0 21 2 1 20 1 0 (1*128) + (1*64) + (0*32) + (1*16) + (0*8) + (0*4) + (1*2) + (0*1) 128 + 64 + 0 + 16 + 0 + 2 + 0 Dec Val 210 2000 Prentice Hall, Inc. All rights reserved. 2001 NHTI CP 107 M. Saleem Yusuf. 14
Conversion – Octal to Decimal • Octal to Decimal 83 512 Octal Value 7 Positional Val 82 64 6 81 8 1 80 1 4 (7 * 512) + (6 * 64) + (1* 8) + (4*1) 3584 + 384 + 8 + 4 Octal Value was 7614 Decimal Value 3980 2000 Prentice Hall, Inc. All rights reserved. 2001 NHTI CP 107 M. Saleem Yusuf. 15
Conversion – Hex to Decimal • Hex to Decimal Positional Val Hex Value 163 4096 A 162 256 D 161 16 3 160 1 B (A * 4096) + (D *256) + (3 * 16) + (B*1) 40960 + 3328 + 48 + 11 Hex Value was AD 3 B Decimal Value is 44347 2000 Prentice Hall, Inc. All rights reserved. 2001 NHTI CP 107 M. Saleem Yusuf. 16
Conversion – Decimal to Bin • Decimal to Binary – Convert decimal 13 to Binary Positional Val 25 24 23 22 21 20 32 0 16 0 8 1 4 1 2 0 1 1 first find (divisor and Modulus) operator. Is 8 > 13, no, it is OK, is 16 > 13, yes, NOT OK. Therefore first operator is 8 (13 / 8) = 1, (13 Mod 8) = 5 (5 / 4) = 1, (5 mod 4) =1 (1 / 2) = 0, (1 mod 2) =1 (1 / 1) = 1, (1 mod 1) =0 Result = 1 1 0 1 in binary is decimal 13 2000 Prentice Hall, Inc. All rights reserved. 2001 NHTI CP 107 M. Saleem Yusuf. 17
Conversion – Decimal to Hex • Decimal to Hex – Convert decimal 44347 to Hex Positional Val 163 4096 162 256 161 16 160 1 (44347 / 4096) = 10, (44347 Mod 4096) = 3387 (3387 / 256) = 13, (3387 mod 256) = 59 (59 / 16) = 3, (59 /16) = 11 (11 / 1) = 11 Result = 10, 13, 3, 11 = Hex AD 3 B 2000 Prentice Hall, Inc. All rights reserved. 2001 NHTI CP 107 M. Saleem Yusuf. 18
Conversion – compliment • Ones compliment (~) or NOT – Consider a 32 bit value “ 36” 00000000 00100100 ~ 11111111 11011011 Simply invert the bits • Twos compliment (~ + 1) or NOT + 1 – Consider a 32 bit value “ 36” 00000000 00100100 ~ 11111111 11011011 + 1 00000000 00000001 ---------------------= -36 11111111 11011100 2000 Prentice Hall, Inc. All rights reserved. 2001 NHTI CP 107 M. Saleem Yusuf. 19
Conversion – compliment -36 11111111 11011100 +36 00000000 00100100 --------------------0 00000000 -36 + 36 = 0 2000 Prentice Hall, Inc. All rights reserved. 2001 NHTI CP 107 M. Saleem Yusuf. 20
End – Back to Lecture 2 http: //www. geocities. com/msaleemyusuf/lecture_2. htm 2000 Prentice Hall, Inc. All rights reserved. 2001 NHTI CP 107 M. Saleem Yusuf. 21
- Slides: 21