Numbering systems
Understand and identify a few of the coding systems . Learning Objectives Understand and identify a few of the coding systems .
Data and Program Representation Digital computers: Can only understand two states, off and on (0 and 1) Digital data representation: The process of representing data in digital form so it can be understood by a computer
Digital Data Representation Bit: The smallest unit of data that a binary computer can recognize (a single 1 or 0) Byte = 8 bits , Nibble =4 bits Byte terminology used to express the size of documents and other files, programs, etc. Prefixes are often used to express larger quantities of bytes: kilobyte (KB), megabyte (MB), gigabyte (GB), terabyte (TB), etc.
Quiz I have a 2 Gb Flash memory. How many 512Kb files can I store on the Flash memory? Answer Convert 2Gb to Kb 2x1024=2048Mb Convert from Mb to Kb 2048x1024= 2097152kb Number of files= 2097152/ 512 = 4096 file Note Mb is in common use for Megabit e.g. 200Megabit = 200Mb MB is in common use for Megabyte , e.g. 200MegaByte=200MB 5
Numbering Systems A numbering system is a way of representing numbers. A number is represented as a string of digits, e.g., a number N with n digits represented by sequence dn-1, …, d1,d0
Numbering Systems Positional notation A system of expressing numbers in which the digits are arranged in succession, the position of each digit has a place value, and the number is equal to the sum of the products of each digit by its place value . Determination of a number require the following : The digits or symbols; The positional of the digit in the number; The base of the number system.
Numbering Systems Base ( radix ) The base of a number system is simply the number of different digits, including zero that exist in the number system. A number system of radix r, typically has a set of r allowed digits belongs to {0,1,…, r-1} We write a number N in the number system of radix r by expressing it in the form (N)r or Nr (12)10 1 and 2 are digits in base 10, 325 2 and 3 are digits in base 5 i.e., belongs to {0,1,..,4}
Numbering Systems a radix point is the symbol used in numerical representations to separate the integer part of a number (to the left of the radix point) from its fractional part (to the right of the radix point). Ex. 13.62510, 13 is the integer to the left of the radix point, and 625 (i.e. 625/1000) is the fractional part to the right.
Understanding Decimal Numbers The numbering system we commonly use called the decimal numbering system because it uses 10 digits - symbols - (0,1,2,…,9) to represent all possible numbers. Why do we use 10 digits, anyway? Numbers greater than nine such as 7216 represented using combination of these 10 digits.
Understanding Decimal Numbers Decimal numbers are made of decimal digits: (0,1,2,3,4,5,6,7,8,9) But how many items does a decimal number represent? 8653 = 8x103 + 6x102 + 5x101 + 3x100 What about fractions? 97654.35 = 9x104 + 7x103 + 6x102 + 5x101 + 4x100 + 3x10-1 + 5x10-2 In formal notation -> (97654.35)10
Understanding Octal Numbers Octal numbers are made of octal digits: (0,1,2,3,4,5,6,7) How many items does an octal number represent? (4536)8 = 4x83 + 5x82 + 3x81 + 6x80 = (1362)10 What about fractions? (465.27)8 = 4x82 + 6x81 + 5x80 + 2x8-1 + 7x8-2 Octal numbers don’t use digits 8 or 9
Understanding Binary Numbers Binary numbers are made of binary digits (bits): 0 and 1 How many items does an binary number represent? (1011)2 = 1x23 + 0x22 + 1x21 + 1x20 = (11)10 What about fractions? (110.10)2 = 1x22 + 1x21 + 0x20 + 1x2-1 + 0x2-2
Understanding Hexadecimal Numbers Hexadecimal numbers are made of 16 digits: (0,1,2,3,4,5,6,7,8,9,A, B, C, D, E, F) How many items does an hex number represent? (3A9F)16 = 3x163 + 10x162 + 9x161 + 15x160 = 1499910 What about fractions? (2D3.5)16 = 2x162 + 13x161 + 3x160 + 5x16-1 = 723.312510 Note that each hexadecimal digit can be represented with four bits. (1110) 2 = (E)16 Groups of four bits are called a nibble. (1110) 2
Representing Numerical Data Least Significant Digit (LSD) is the right most digit has the lowest value Most Significant Digit (MSD) is the left most digit has the highest value The most significant symbol can not be zero Example: Show how the value of the number (9375)10 is estimated 1 2 3 position 100 101 102 103 Weight 5 7 9 Digit 9375 5x1 7x10 3x100 9x1000 Value 5 is the least significant digit (LSD) 9 is the most significant digit (MSD)
Representing Numerical Data Commonly used numbering systems Decimal number system base =10, Ten allowed digits{0,1,…,9} Binary number system base= 2, Two allowed digits {0,1} A Binary digit is referred to as Bit Most significant Bit (MSB) is the leftmost bit has the highest value Least significant Bit (LSB) is the rightmost bit has the lowest value C. Octal number system base =8, Eight allowed digits {0,1,2,…,7} D. Hexadecimal number system base = 16, Sixteen allowed digits {0,1,…,9, A,B,C,D,E,F} Where, A= ten , B= eleven, C=Twelve, D= thirteen, E=fourteen, F= fifteen Q. Why is the digit following 9 assigned the character A not 10 ? Ans. What we need is a single digit whose value is ten but “10” is actually two digits. Thus, in hexadecimal system the two digits number (10)16 actually represents a value of sixteen not ten
digit in the number system) Check point Q. What is the largest value can be represented in three integral digits? ------------------------------------------------------------- For the decimal system, it is (- - - )10 For the octal system, it is (- - - )8 For the hex system, it is (- - - )16 For the binary system, it is (- - - )2 Hint (The largest value results when all 3 positions are filled with the largest digit in the number system) Answer For the decimal system, it is ( 999 )10 For the octal system, it is ( 777 )8 For the hex system, it is ( fff )16 For the binary system, it is ( 111 )2
Conversion Between Number Bases Hexadecimal (base16) Decimal(base 10) Octal(base 8) Binary(base 2) Learn to convert between bases.
Representing Numerical Data Converting between numbering systems Converting from decimal to any base Converting from any base to decimal Converting from any base to any base A special conversion case number bases that are related (Shortcut method to/from binary )
Convert an Integer from Decimal to Another Base For each digit position: Divide decimal number by the base (e.g. 2) The remainder is the lowest-order digit Repeat first two steps until no divisor remains. Example for (13)10: Integer Quotient Remainder Coefficient 13/2 = 6 + ½ a0 = 1 6/2 = 3 + 0 a1 = 0 3/2 = 1 + ½ a2 = 1 1/2 = 0 + ½ a3 = 1 Repeated Division-by-r (for whole numbers) Repeated Multiplication-by- r (for fractions) r is the radix of the number system which we need LSB MSB Answer (13)10 = (a3 a2 a1 a0)2 = (1101)2
Repeated Division-by-r To convert a whole number to the new base r, use successive division by r until the quotient is 0. The remainders form the answer, with the first remainder as LSB or LSD and the last as MSB or MSD. Example: (43)10 = (?)2 (43)10 = (101011)2
Convert an Fraction from Decimal to Another Base For each digit position: Multiply decimal number by the base (e.g. 2) The integer is the highest-order digit Repeat first two steps until fraction becomes zero. Example for (0.625)10: Integer Fraction Coefficient 0.625 x 2 = 1 + 0.25 a-1 = 1 0.250 x 2 = 0 + 0.50 a-2 = 0 0.500 x 2 = 1 + 0 a-3 = 1 MSB LSB Answer (0.625)10 = (0.a-1 a-2 a-3 )2 = (0.101)2
Repeated Multiplication-by-r To convert decimal fractions to binary, repeated multiplication by r is used, until the fractional product is 0 (or until the desired number of digits of accuracy obtained). The carried digits, or carries, produce the answer, with the first carry as the MSB or MSD, and the last as the LSB or MSD. Example: (0.3125)10 = (.0101)2
Note For a number that has both integral and fractional parts, conversion is done separately for both parts, and then the result is put together with a system point in between both parts.
2-Any base r-to-Decimal Conversion Base-r to decimal: multiply digits with their corresponding weights. Examples (1101.101)2 = 123 + 122 + 120 + 12-1 + 12-3 = 8 + 4 + 1 + 0.5 + 0.125 = (13.625)10 (572.6)8 = 582 + 781 + 280 + 68-1 = 320 + 56 + 2 + 0.75 = (378.75)10 Convert 3b216 to decimal. N = 3*162 + 11*161 + 2*160 = 94610 Convert 3708 to decimal. N = 3*82 + 7*81 + 0*80 = 192 + 56 + 0 = 24810
3-Conversion between Bases In general, conversion between bases can be done via decimal: First convert given number to decimal then convert decimal number to the new base. Base-2 Base-2 Base-3 Base-3 Base-4 Decimal Base-4 … …. Base-R Base-R
Converting Between Base 16 and Base 2 3A9F16 = 0011 1010 1001 11112 3 A 9 F Conversion is easy! Determine 4-bit value for each hex digit Note that there are 24 = 16 different values of four bits Easier to read and write in hexadecimal. Representations are equivalent! Similarly, it takes one hexadecimal digit to exactly represent 4 bits (i.e., 4bit ~ (d)16).
Converting Between Base 16 and Base 8 3A9F16 = 0011 1010 1001 11112 3 A 9 F 352378 = 011 101 010 011 1112 3 5 2 3 7 Convert from Base 16 to Base 2 Regroup bits into groups of three starting from right Ignore leading zeros Each group of three bits forms an octal digit. Since 8=23 , we can represent binary numbers directly in base 8 using one octal digit to correspond to each three binary digits (i.e., 3bit ~ (d)8).
A special conversion case number bases that are related (Shortcut method to/from binary ) Grouping of several digits in the smaller number base corresponds exactly to single digit in the larger number base Examples Binary Octal: Partition in groups of 3 (10 111 011 001 . 101 110)2 = (2731.56)8 Octal Binary: reverse (2731.56)8 = (10 111 011 001 . 101 110)2 Binary Hexadecimal: Partition in groups of 4 (101 1101 1001 . 1011 1000)2 = (5D9.B8)16 Hexadecimal Binary: reverse (5D9.B8)16 = (101 1101 1001 . 1011 1000)2
Arithmetic operation on numbering systems Addition Subtraction Multiplication division
Decimal Addition Example Write down 5, carry 1 Add 3758 to 4657: 2) Add 5 + 5 + 1 = 11 Write down 1, carry 1 1 1 1 3 7 5 8 + 4 6 5 7 3) Add 7 + 6 + 1 = 14 Write down 4, carry 1 8 4 1 5 4) Add 3 + 4 + 1 = 8 Write down 8 33
Decimal Addition Explanation What just happened? 1 1 1 (carry) 3 7 5 8 + 4 6 5 7 8 14 11 15 (sum) 10 10 10 (subtract the base) 8 4 1 5 1 1 1 3 7 5 8 + 4 6 5 7 8 4 1 5 So when the sum of a column is equal to or greater than the base, we subtract the base from the sum, record the difference, and carry one to the next column to the left. 34
Binary Addition Rules 1 + 1 = 210 = 102 = 0 with 1 to carry 0 + 0 = 0 0 + 1 = 1 1 + 0 = 1 (just like in decimal) 1 + 1 = 210 = 102 = 0 with 1 to carry 1 + 1 + 1 = 310 = 112 = 1 with 1 to carry 35
Binary Addition Example 1 Col 1) Add 1 + 0 = 1 Write 1 Example 1: Add binary 110111 to 11100 Col 2) Add 1 + 0 = 1 Write 1 1 1 1 1 1 1 0 1 1 1 + 0 1 1 1 0 0 Col 3) Add 1 + 1 = 2 (10 in binary) Write 0, carry 1 Col 4) Add 1+ 0 + 1 = 2 Write 0, carry 1 1 1 1 1 Col 5) Add 1 + 1 + 1 = 3 (11 in binary) Write 1, carry 1 Col 6) Add 1 + 1 + 0 = 2 Write 0, carry 1 Col 7) Bring down the carried 1 Write 1 36
Binary Addition Explanation What is actually happened when we carried in binary? In the first two columns, there were no carries. In column 3, we add 1 + 1 = 2 Since 2 is equal to the base, subtract the base from the sum and carry 1. 1 1 0 1 1 1 + 0 1 1 1 0 0 - . 1 1 1 1 In column 4, we also subtract the base from the sum and carry 1. 2 3 2 2 In column 5, we also subtract the base from the sum and carry 1. 2 2 2 2 In column 6, we also subtract the base from the sum and carry 1. 1 1 1 1 In column 7, we just bring down the carried 1 37
Binary Addition Verification You can always check your answer by converting the figures to decimal, doing the addition, and comparing the answers. Verification 1101112 5510 +0111002 + 2810 8310 64 32 16 8 4 2 1 1 0 1 0 0 1 1 = 64 + 16 + 2 +1 = 8310 1 1 0 1 1 1 + 0 1 1 1 0 0 1 1 1 1 38
Binary Addition Example 2 Verification 1110102 5810 +0011112 + 1510 7310 64 32 16 8 4 2 1 1 0 0 1 0 0 1 = 64 + 8 +1 = 7310 Example 2: Add 1111 to 111010. 1 1 1 1 1 1 1 1 0 1 0 + 0 0 1 1 1 1 1 1 1 39
Decimal Subtraction Example (Borrow method) Try to subtract 5 – 7 can’t. Must borrow 10 from next column. Subtract 4657 from 8025: Add the borrowed 10 to the original 5. Then subtract 15 – 7 = 8. Try to subtract 1 – 5 can’t. Must borrow 10 from next column. But next column is 0, so must go to column after next to borrow. 7 9 1 1 8 0 2 5 - 4 6 5 7 1 1 Add the borrowed 10 to the original 0. Now you can borrow 10 from this column. 3 3 6 8 Add the borrowed 10 to the original 1.. Then subract 11 – 5 = 6 3) Subtract 9 – 6 = 3 4) Subtract 7 – 4 = 3 40
Decimal Subtraction Explanation 8 0 2 5 - 4 6 5 7 3 3 6 8 So when you cannot subtract, you borrow from the column to the left. The amount borrowed is 1 base unit, which in decimal is 10. The 10 is added to the original column value, so you will be able to subtract. 41
Binary Subtraction Example 1 Col 1) Subtract 1 – 0 = 1 Example 1: Subtract binary 11100 from 110011 Col 2) Subtract 1 – 0 = 1 Col 3) Try to subtract 0 – 1 can’t. Must borrow 2 from next column. But next column is 0, so must go to column after next to borrow. 2 1 Add the borrowed 2 to the 0 on the right. Now you can borrow from this column (leaving 1 remaining). 2 2 1 1 0 0 1 1 - 1 1 1 0 0 Add the borrowed 2 to the original 0. Then subtract 2 – 1 = 1 Col 4) Subtract 1 – 1 = 0 1 1 1 1 Col 5) Try to subtract 0 – 1 can’t. Must borrow from next column. Add the borrowed 2 to the remaining 0. Then subtract 2 – 1 = 1 Col 6) Remaining leading 0 can be ignored. 42
Binary Subtraction Verification 1100112 5110 - 111002 - 2810 2310 64 32 16 8 4 2 1 1 0 1 1 1 = 16 + 4 + 2 + 1 = 2310 Subtract binary 11100 from 110011: 2 1 2 2 1 1 0 0 1 1 - 1 1 1 0 0 1 1 1 1 43
X = (1151)10 , y = (1102)10 which one is greater than the other ? Note X = (1151)10 , y = (1102)10 which one is greater than the other ? Ans. Compare each digit from left to right digit 1 in x = digit 1 in y goto the next digit digit 2 in x = digit 2 in y goto the next digit digit 3 in x > digit 3 in y stop x greater than y Quiz: (110101)2 , (11001)2 which one is greater than the other ? 44
Binary multiplication Binary multiplication uses the same technique as decimal multiplication. Example: multiplying 1102 by 102. 110 x 10 --------- 000 + 110 -------- 1100 6 x 2 ------ 12 45
Division Division rule Set quotient to zero Repeat while dividend is greater than or equal to divisor Subtract divisor from dividend Add 1 to quotient End of repeat block quotient is correct, dividend is remainder STOP 46
Binary division Division as repeated subtraction Example in decimal 84 ÷ 21 = ?? I subtracted 21 four times, so 84 ÷ 21 = 4 84 − 21 ------ 63 − 21 ------- 42 − 21 ------ 21 − 21 ------- 0 1 ---- 4 47
Hence result is 2 and with remainder 2 ( (10)2 ) Example: Find 1100 ÷ 0101 Ans. 1100 - 0101 = 111 111-101= 010 Hence result is 2 and with remainder 2 ( (10)2 ) quotient 1 ----- 2 48
One’s Complement and Two’s Complement Representations
One’s Complement Representation The one’s complement of a binary number involves inverting all bits. 1’s comp of 00110011 is 11001100 1’s comp of 10101010 is 01010101
Two’s Complement Algorithm 1 – Simply complement each bit and then add 1 to the result. Finding the 2’s complement of (01100101)2 and of its 2’s complement… N = 01100101 [N] = 10011011 10011010 01100100 + 1 + 1 --------------- --------------- 10011011 01100101
Two’s Complement Representation Algorithm 2 – Starting with the least significant bit, copy all of the bits up to and including the first 1 bit and then complementing the remaining bits. N = 0 1 1 0 0 1 0 1 [N] = 1 0 0 1 1 0 1 1
Logical Operation On binary
Logical OR Rules OR Operations OR Results in 1 if either or both of the operands are 1. OR Table 0 OR 0 = 0 0 OR 1 = 1 1 OR 0 = 1 1 OR 1 = 1
Logical OR Operation To perform the OR operation, take one column at a time and perform the OR operation using the OR table. Ex 1: 1 0 0 1 0 0 1 1 OR 0 0 0 0 1 1 1 1 1 0 0 1 1 1 1 1
Logical XOR Rules XOR Operations The exclusive OR. Similar to OR except that it now gives 0 when both its operands are 1. Rules. 0 XOR 0 = 0 0 XOR 1 = 1 1 XOR 0 = 1 1 XOR 1 = 0
Logical XOR Examples Ex 1: 1 0 0 1 1 0 0 1 XOR 0 0 0 0 1 1 1 1 1 0 0 1 0 1 1 0 Ex 2: 0 1 1 1 XOR 0 0 1 0 0 1 0 1
Logical AND Rules AND Operations AND yields 1 only if both its operands are 1. Rules. 0 AND 0 = 0 0 AND 1 = 0 1 AND 0 = 0 1 AND 1 = 1
Logical AND Examples Ex 2: 0 1 1 1 AND 1 0 0 1 0 0 0 1
Logical NOT NOT Operations NOT is a separate operator for flipping the bits. Rules. NOT 0 = 1 NOT 1 = 0 Example. NOT 1 0 1 0 = 0 1 0 1
Thank you