Prepared By: Razif Razali 1 CHAPTER TWO TCS1023 NUMBERING SYSTEM

Embed Size (px)

DESCRIPTION

3 Fundamental to understand how computers work is understanding the number system that computer use to store data and communicate with each other.Fundamental to understand how computers work is understanding the number system that computer use to store data and communicate with each other. Number system been used to understand computer:Number system been used to understand computer: –Base 10 (decimal) E.g.: / 3945d –Base 2 (binary) E.g.: / b –Base 8 (octal) E.g. : –Base 16 (hexadecimal) E.g.: 0A3E 16 / 0A3Eh Number Systems

Citation preview

Prepared By: Razif Razali 1 CHAPTER TWO TCS1023 NUMBERING SYSTEM Prepared By: Razif Razali 2 CONTENTS Introduction To Numbering SystemIntroduction To Numbering System Converting between decimal and othersConverting between decimal and others Converting binary and othersConverting binary and others Converting between octal and hexadecimal to othersConverting between octal and hexadecimal to others Arithmetic operationArithmetic operation Ones complement and twos ComplementOnes complement and twos Complement 3 Fundamental to understand how computers work is understanding the number system that computer use to store data and communicate with each other.Fundamental to understand how computers work is understanding the number system that computer use to store data and communicate with each other. Number system been used to understand computer:Number system been used to understand computer: Base 10 (decimal) E.g.: / 3945d Base 2 (binary) E.g.: / b Base 8 (octal) E.g. : Base 16 (hexadecimal) E.g.: 0A3E 16 / 0A3Eh Number Systems 4 The Decimal System In everyday life we use a system based on decimal digits.In everyday life we use a system based on decimal digits. Consider the number 4728 means four thousands, seven hundreds, two tens, plus eight:Consider the number 4728 means four thousands, seven hundreds, two tens, plus eight: 4728 = (4 x 1000) + (7 x 100) + (2x10) + 8 The decimal system is said to have a base or radix of 10.The decimal system is said to have a base or radix of 10. Each digit in the number is multiplied by 10 raised to a power corresponding to that digits position:Each digit in the number is multiplied by 10 raised to a power corresponding to that digits position: 4728 = (4 x 10 3 ) + (7 x 10 2 ) + (2 x 10 1 ) + (8 x 10 0 ) 5 The Binary System In the binary system, we have only two digits, 1 and 0. Thus, number in the binary system are represented to the base 2. Each digits in a binary number also have a value depending on its position: = (1 x 2 2 ) + (0 x 2 1 ) + (0 x2 0 ) = b = (1 x 2 5 ) + (0 x 2 4 ) + (1 x 2 3 ) + (0 x 2 2 ) + (1 x 2 1 ) + (1 x 2 0 ) = 43d Binary Numbering Scale Base 2 NumberBase 10 EquivalentPowerPositional Value Binary Numbers Each binary digit (called bit) is either 1 or 0Each binary digit (called bit) is either 1 or 0 Bits have no inherent meaning, can representBits have no inherent meaning, can represent Unsigned and signed integers Characters Floating-point numbers Images, sound, etc. Bit NumberingBit Numbering Least significant bit (LSB) is rightmost (bit 0) Most significant bit (MSB) is leftmost (bit 7 in an 8-bit number) Most Significant Bit Least Significant Bit Octal Number System Also known as the Base 8 SystemAlso known as the Base 8 System Uses digits 0 - 7Uses digits Readily converts to binaryReadily converts to binary Groups of three (binary) digits can be used to represent each octal digitGroups of three (binary) digits can be used to represent each octal digit Also uses multiplication and division algorithms for conversion to and from base 10Also uses multiplication and division algorithms for conversion to and from base 10 9 The System The Hexadecimal System A computers world is a binary world and communication of instruction and data by the devices that process them is always in binary. Binary system is very difficult for human being. Human being are comfortable to decimal number system. However calculations to convert binary to decimal are relatively complex. A notation known as hexadecimal has been adopted. Binary digits are grouped into sets of four. Each possible combination of four binary digits is given a symbol (hexadecimal digits) as follows: 10 The System The Hexadecimal System 0000 = = = = = = = = = = = A 1011 = B 1100 = C 1101 = D 1110 = E 1111 = F 11 The Hexadecimal System In the hexadecimal system, we have 16 hexadecimal digits. Thus, number in the hexadecimal system are represented to the base 16. Each digits in a hexadecimal number also have a value depending on its position: Example: 2C 16 = (2 x 16 1 )+ (C x 16 0 ) = (2 x 16 1 )+ (12 x 16 0 ) = The reason for using hexadecimal notation are because it is more compact than binary notation and it is extremely easy to convert between binary and hexadecimal. 12 Number System DecimalBinaryOctalHexadecimal A B C D E F 13 Conversion Between Number Systems Converting Binary to DecimalConverting Binary to Decimal Converting Binary to OctalConverting Binary to Octal Converting Binary to HexConverting Binary to Hex Converting Decimal to BinaryConverting Decimal to Binary Converting Decimal to OctalConverting Decimal to Octal Converting Decimal to HexConverting Decimal to Hex Converting Octal to BinaryConverting Octal to Binary Converting Octal to DecimalConverting Octal to Decimal Converting Octal to HexConverting Octal to Hex Converting Hex to BinaryConverting Hex to Binary Converting Hex to DecimalConverting Hex to Decimal Converting Hex to OctalConverting Hex to Octal b to decimal101001b to decimal b = (1 x 2 5 ) + (0 x 2 4 ) + (1 x 2 3 ) + (0 x 2 2 ) + (0 x 2 1 ) + (1 x 2 0 ) = = 41d Converting Binary to Decimal Binary to Decimal Conversion The easiest method for converting a binary number to its decimal equivalent is to use the Multiplication AlgorithmThe easiest method for converting a binary number to its decimal equivalent is to use the Multiplication Algorithm Multiply the binary digits by increasing powers of two, starting from the rightMultiply the binary digits by increasing powers of two, starting from the right Then, to find the decimal number equivalent, sum those productsThen, to find the decimal number equivalent, sum those products Multiplication Algorithm Convert ( ) 2 to its decimal equivalent: Binary Positional Values x xxxxxxx Products Binary to Decimal (Real number) Convert the following binary to decimal:Convert the following binary to decimal: a) b) c) Prepared By: Razif Razali 17 Binary to Hexadecimal Conversion The easiest method for converting binary to hexadecimal is to use a substitution codeThe easiest method for converting binary to hexadecimal is to use a substitution code Each hex number converts to 4 binary digitsEach hex number converts to 4 binary digits Convert to hex using the 4-bit substitution code : Substitution Code 5 6 A E 6 A 56AE6A 16 Converting Binary to Hexadecimal Each hexadecimal digit corresponds to 4 binary bits Example: Convert the 32-bit binary number to hexadecimal Solution: A B 1110 E Converting from binary to hexadecimal To convert from binary to hexadecimalTo convert from binary to hexadecimal 1. Organize the stream of binary digits into groups of four. 2. Find the hexadecimal value for each group of 4 bits E 1 A Example: 22 Decimal can be converted in to a binary systems with the Remainder MethodDecimal can be converted in to a binary systems with the Remainder Method Example: Convert 26d to base 2Example: Convert 26d to base 2 => 26d = 11010b 26/2 = /2 = 6 1 6/2 = 3 0 3/2 = 1 1 1/2 = 0 1 Most significant bit Least significant bit Converting Decimal to Binary 23 How about floating point number?How about floating point number? E.g.: Convert 0.875d into base 2 number.E.g.: Convert 0.875d into base 2 number. => 0.875d = b x 2 = x 2 = x 2 = x 2 = 0 0 Converting Decimal to Binary (Floating point number) 24 How about floating point number?How about floating point number? E.g 2.: Convert 0.442d into base 2 number.E.g 2.: Convert 0.442d into base 2 number x 2 = x 2 = => 0.875d = b x 2 = x 2 = x 2 = x 2 = Converting Decimal to Binary (Floating point number) 25 Decimal can be converted into a hex with the Remainder MethodDecimal can be converted into a hex with the Remainder Method Example: Convert 425d to base 16Example: Convert 425d to base 16 425 / 16 = > 9 26 / 16 = > A 1 / 16 = 0 1 -> 1 => 425d = 1A9h Example: Convert 374d to base 16Example: Convert 374d to base 16 374 / 16 = > 6 23 / 16 = 1 7 -> 7 1 / 16 = 0 1 -> 1 => 374d = 176h Converting Decimal to Hex Decimal to Octal Conversion Convert to its octal equivalent: 427 / 8 = 53 R 3 Divide by 8; R is LSD 53 / 8 = 6 R 5 Divide Q by 8; R is next digit 6 / 8 = 0 R 6 Repeat until Q = Octal to Decimal Conversion Convert to its decimal equivalent: xx x Positional Values Products Octal Digits Octal to Binary Conversion Each octal number converts to 3 binary digits To convert to binary, just substitute code: Substitution code can also be used to convert binary to octal by using 3-bit groupings: Substitution Code 30 To convert a hex number to binary, we need only express each hex digit in binaryTo convert a hex number to binary, we need only express each hex digit in binary E.g.: Convert DE1 16 to binaryE.g.: Convert DE1 16 to binary D E 1 D E 1 = = = b = b To go from binary to hex, just reverse this processTo go from binary to hex, just reverse this process = = Converting Between Hex and Binary 31 Converting Hex to Decimal A3F 16 to decimal A3F 16 = (A x 16 2 ) + (3 x 16 1 ) + (F x 16 0 ) = (10 x 256) + (3 x 16) + (15 x 1) = EXERCISE Convert the following numbers: a) to Decimal b)3BA4 16 to Decimal c) to Decimal d)422d to Hexadecimal Prepared By: Razif Razali 32 1.Show the hexadecimal equivalent of the bit pattern Show the hexadecimal equivalent of the bit pattern What is the bit pattern for 2675 base 16? 4.What is the bit pattern for B51E base 16? Exercises 1 Exercise 2 a)Convert to decimal. b)Convert 3B2 16 to decimal. c)Convert to decimal. Exercise 3 Convert the following to decimal: _______ 2.6A 16 _______ _______ Table 2.1 shows a summary of the four positional number systems discussed in this chapter. Summary of the four positional systems 37 Integer Representation For the purpose of computer storage and processing, only binary digits (0 and 1) may be used to represent numbers (negative or positive).For the purpose of computer storage and processing, only binary digits (0 and 1) may be used to represent numbers (negative or positive). For a 8-bit number, there are 2 8 =256 possible bit patterns.For a 8-bit number, there are 2 8 =256 possible bit patterns. For unsigned number, we can represent 0 to 255 using 8-bit number.For unsigned number, we can represent 0 to 255 using 8-bit number. For signed number, the most significant (leftmost) bit usually used as a sign bit.For signed number, the most significant (leftmost) bit usually used as a sign bit. 0 for positive number. 1 for negative number Sign bit Negative Positive 38 Integer Representation There are several alternative conventions used to represent negative integers. Some of them are: Signed magnitude Ones complement Twos complement 39 Signed Magnitude Also know as sign and magnitude, the leftmost bit is the sign and the rest are magnitudeAlso know as sign and magnitude, the leftmost bit is the sign and the rest are magnitude 0 = positive 1 = negative SignMagnitude Integer Representation 40 Example (for 8-bit number)Example (for 8-bit number) +25d = b -25d= b Largest number is +127 and smallest number is 127Largest number is +127 and smallest number is 127 Problems: Two representations for zero:Problems: Two representations for zero: +0= b -0= b Integer Representation Signed Magnitude (cont.) 41 Ones Complement The leftmost bits is the sign ( 0 = +ve, 1 = -ve)The leftmost bits is the sign ( 0 = +ve, 1 = -ve) Negative number is obtained by complementing each bit from 0 to 1 or from 1 to 0Negative number is obtained by complementing each bit from 0 to 1 or from 1 to 0 Example (8-bit number):Example (8-bit number): +25d = b -25d = b Two representation of zero:Two representation of zero: +0d = b and -0d = Largest number is +127 and smallest number is -127Largest number is +127 and smallest number is -127 Integer Representation Ones complement Switch all 0s to 1s and 1s to 0s Binary # s complement 43 The leftmost bit is the sign bit (0= +ve, 1 = -ve)The leftmost bit is the sign bit (0= +ve, 1 = -ve) Negative of the number is obtained by adding 1 to the ones complement negative.Negative of the number is obtained by adding 1 to the ones complement negative. Example(8-bit number):Example(8-bit number): +25d= b -25d= b One representation for zero: bOne representation for zero: b Largest number is +127 and smallest number is -128Largest number is +127 and smallest number is -128 Twos complement Integer Representation Twos Complement Step 1: Find 1s complement of the number Binary # s complement Step 2: Add 1 to the 1s complement Example Convert +5 into twos complement representation and give the answer in 8 bits.Convert +5 into twos complement representation and give the answer in 8 bits. Solution:Solution: Obtain +5 representation = Obtain ones complement= Add 1 to ones complement= = -5 in twos complement is Prepared By: Razif Razali 45 Exercise Get the representation of twos complement for the following numbers (by using 6 bit)Get the representation of twos complement for the following numbers (by using 6 bit) a.+7 b.-20 Prepared By: Razif Razali 46 Example Interpret in decimal if the number was stored as a sign-and-magnitude integerInterpret in decimal if the number was stored as a sign-and-magnitude integer Solution:Solution: Ignoring the leftmost bit, the remaining bits are This number in decimal is 59. The leftmost bit is 1, so the number is 59. Binary Arithmetic Addition (+)Addition (+) 0 + 0 = 0 0 + 1 = 1 1 + 0 = 1 1 + 1 = 10 Example:Example: = = Prepared By: Razif Razali 48 Binary Addition Start with the least significant bit (rightmost bit)Start with the least significant bit (rightmost bit) Add each pair of bitsAdd each pair of bits Include the carry in the addition, if presentInclude the carry in the addition, if present (54) (29) (83) 1 carry 01234bit position: Binary Arithmetic SubtractionSubtraction 0 - 0 = 0 0 - 1 = 1(borrow 1) 1 - 0 = 1 1 1 = 0 ExampleExample = = Prepared By: Razif Razali 50 Binary Arithmetic MultiplicationMultiplication 0 x 0 = 0 0 x 1 = 0 1 x 0 = 0 1 x 1 = 1 Prepared By: Razif Razali 51 Binary Arithmetic DivisionDivision 0 / 1 = 0 1 / 1 = 0 Prepared By: Razif Razali 52 Arithmetic Operation Arithmetic's operation can be done by usingArithmetic's operation can be done by using Ones complement Twos complement Sign and Magnitude Prepared By: Razif Razali 53 Arithmetic Operation Sign And Magnitude All subtraction operation will be changed into addition operation.All subtraction operation will be changed into addition operation. ExampleExample 8 5 = 8 + (-5) = (-10) (-3) = Prepared By: Razif Razali 55 Example If sign of both number are negativeIf sign of both number are negative Assume we have -3 4Assume we have -3 4 Solution:Solution: Step 1 -3 4 = (-3) + (-4) = Step 2 Only add the magnitude = Step 3 Set the sign bit of the result to ve (add 1 to MSB) = Prepared By: Razif Razali 56 Example If sign of both number differIf sign of both number differ Assume (-2) + (+4)Assume (-2) + (+4) SolutionSolution Step 1 (get the binary pattern) Step 2 Change the sign bit of the ve number to +ve Step 3 (get the result) = 2 10 Prepared By: Razif Razali 57 Example (+3) + (-5) = = = = -2 Prepared By: Razif Razali 58 Ones Complement In Ones complement, it is easier than sign-magnitude.In Ones complement, it is easier than sign-magnitude. However a situation called overflow might occur when addition is performed on the following categories:However a situation called overflow might occur when addition is performed on the following categories: If both are negative numbers If both are in difference sign Prepared By: Razif Razali 59 Example Assume we have find the value by using 8 bit. (-3) + (-4) Step 1: Convert -3 and -4 into 1 complement +3 (8 bit) Implement 1 complement Prepared By: Razif Razali 60 Example Step 2: Perform the addition operation -3 = = = = (9 bit) Prepared By: Razif Razali 61 Overflow occur and needs to be add to rightmost bit Example Step = -7 Prepared By: Razif Razali 62 Answer 63 For n bit number, highest integer value can be represent is 2 n-1 -1.For n bit number, highest integer value can be represent is 2 n Highest valueLowest Value Signed Magnitude2 n-1 -1-(2 n-1 -1) Ones Complement2 n-1 -1-(2 n-1 -1) Twos Complement2 n-1 -1-(2 n-1 ) Range in Integer Representation