Chapter 2 Number Systems + Codes
Overview Objective: To use positional number systems To convert decimals to binary integers To convert binary integers to decimals To represent binary integers in 3 forms: –Sign magnitude –1’s complement –2’s complement To reprensent number in octal (base 8) and hexadecimal numbers and convert them to decimal and binary numbers To understand various computer codes
Overview Two types of representations of information: Numeric information -Positional number system -Conversion between number systems -Operation on number systems (add, substract …) Non numeric information -Codes - Properties of codes
Overview Real life Data Numeric data (125.5 : a price Nonnumeric data (John : a name ) Representation of data in Computer or digital circuits : string of 0’s and 1’s Correspondance or Representation
Positional Number Systems (PNS) PNS – a number is represented by a string of digits. Each digit position is weighted by a power of the base or radix. Example: Decimal PNS, in base 10 digit position i is weighted by 10 i =9 .0001 General Form: Base or Radix = 10 D : d p-1 d p-2 d p-3 … d 2 d 1 d 0. d -1 d -2 …d -n D = d p-1.10 p-1 + d p-2.10 p-2 +…+ d d d d … + d -n.10 -n p -1 D = d i.10 i i=-n I. Representation of numerical information d i is a decimal digit
Positional Number Systems (PNS) General Form: Base or Radix = 2 D : b p-1 b p-2 b p-3 … b 2 b 1 b 0. b -1 b -2 …b -n D = b p-1.2 p-1 + b p-2.2 p-2 +…+ b b b b … + b -n.2 -n p -1 D = b i.2 i i=-n b i is a binary digit I. Representation of numerical information
Summary of PNS Decimal is natural (we count in base 10) - (Radix or Base 10 ) digits {0,1,…,8,9} Binary is used in digital system - (Radix or Base 2 ) digits {0,1} Octal is used for representing multibits (group of 3 bits) numbers in digital systems. - (Radix or Base 8=2 3 ) digits {0,1,…,6,7} Hexadecimal is used for representing multibits (group of 4 bits) numbers in digital systems - (Radix or Base 16=2 4 ) digits {0,1,…9,A,B,…,F} I. Representation of numerical information
Summary of PNS (…) Dec Binary Oct Hex 000 (000)0 (0000) (001)1 (0001) 2102 (010)2 (0010) 3113 (011)3 (0011) (100)4 (0100) ( 101)5 (0101) (110)6 (0110) (111)7 (0111) ( )8 (1000) ( )9 (1001) ( )A (1010) ( )B (1011) ( )C (1100) ( )D (1101) ( )E (1110) ( )F (1111) ( )10 ( ) I. Representation of numerical information
Conversions From any base r ---> Decimal Use base 10 arythmetic to expand : p -1 D = d i.r i (r is the base and di are the digits) i=0 Two solutions can be used to do this : Polynomial expansion D = d p-1.r p-1 + d p-2.r p-2 +…+ d 1.r 1 + d 0.r 0 Examples : = = = CE8 16 = = = = I. Representation of numerical information
Conversions From any base r ---> Decimal Iterative multiplication (Expansion of D) D can be written as : ((….((d p-1).r + d p-2).r +…+d 2 ).r + d 1 ).r + d 0 Examples : = ((3).16+ 1) ) = (49) = Good for programming I. Representation of numerical information
Conversions From Decimal ---> Any base r Successive divisions of D by r yield the digits from the least to the most significant bit. Remember that D can be written in base r as : D = d p-1.r p-1 + d p-2.r p-2 +…+ d 1.r + d 0. = ((….((d p-1).r + d p-2).r +…+d 2 ).r + d 1 ).r + d 0 D/r -----> quotient D1 = (….((dp-1).r + dp-2).r +…+d 2 ).r + d 1 remainder d0 D1/r -----> quotient D2 = (….((dp-1).r + dp-2).r +… d3).r+d 2 ) remainder d1 D2/r -----> quotient D3 = (….((dp-1).r + dp-2).r +…+d 3 ) remainder d2 And so on ….. I. Representation of numerical information
Conversions From Decimal ---> Any base r Example : conversion of 179 from Base 10 -> Base LSB MSB weight Successive divisions by = I. Representation of numerical information
Conversions From Decimal ---> Any base r Example : conversion of 467 from Base 10 -> Base LSB MSB 467 weight successive divisions = I. Representation of numerical information
Other Conversions From Binary ---> Octal/Hex Conversion by substitution –Binary to Oct : Starting from rightmost bit, make groups of 3 bits and convert each group to base = 100/011/001/110 = / 3 / 1 / 6 –Binary to Hex : Starting from rightmost bit, make groups of 4 bits and convert each group to base = 1000/1100/1110 = 8CE 16 8 / C / E I. Representation of numerical information
Other Conversions From Octal/hex ---> Binary Conversion by substitution – Octal to Binary : Convert each digit to a 3 bits binary number = –Hex to Binary : Convert each digit to a 4 bits binary number = I. Representation of numerical information
Addition of binary numbers Example 1 (Addition) Carry X Y Result has more bits than binary addends Carry equals 1 in the last bit position Example 2 (Addition) Carry X Y Carry equal to 0 in the last bit position I. Representation of numerical information
Addition of binary numbers In general, given two binary number X = ( x n-1 … x i … x 1 x 0 ) 2 and Y = ( y n-1 … y i … y 1 y 0 ) 2 the sum (X+Y) bit by bit at position i is given by (c i is the ith position’s carry) c i-1 Addition table + x i ci-1 xi yi si ci + y i c i s i I. Representation of numerical information
Substraction of binary numbers Substraction B X Y B X Y I. Representation of numerical information
Addition of hexadecimal numbers C 1 1 X 1 9 B Y C 7 E 6 16 E 1 9 F 16 I. Representation of numerical information Addition of two hexadecimal numbers
Representation of negative numbers Signed-magnitude systems +99, -57, Binary uses a (sign bit, the most significant bit (MSB)) MSB = 0 (Positive) MSB = 1 (Negative) = = = = = = Problem: Too much logic needed to - detect and compare sign bit - add or abstract magnitudes - determine the sign of the result I. Representation of numerical information
Representation of negative numbers Complement number system Difficult to change to complement But adding or substracting two numbers are easier once they are represented in complement number systems I. Representation of numerical information
Representation of negative numbers Radix – Complement representation n digit number is substracted from r n Example: if r = 10 and n = 4, r n = 10 4 =10,000 The 10’s complement of D= is: (r n – D) = 8151 Computing 10’s complement: r n – D = (r n - 1 – D) +1 (r n - 1) – D => complement of each digit with rspect to base (r-1) In base 10 complement each digit with respect to 9 and 1 to the result Example: The 10’s complement of is ( )= (9999 – 1849) + 1 = = 8151 I. Representation of numerical information
Representation of negative numbers Radix – Complement representation Complement of each digit : r –1 – d i In base 10 : 10 –1 – d i = 9 – d i Example : The complement of each digit of 345 is : For 5 : (10 – 1) – 5 = 9 – 5 = 4 For 4 : = 9 – 4 = 5 For 3 : = 9 – 3 = 6 In base 3 the radix complement of 121 is 101 In base 2 the radix complement of is I. Representation of numerical information
Representation of negative numbers Two’s Complement The MSB is the sign bit Example: = = = = The range is -(2 n-1 ) -> (2 n-1 -1), For n= = = I. Representation of numerical information Note: The weight of the MSB is –2 n-1 The range of positive numbers is 0 to 127 The range of negative numbers is –1 to -128
Representation of negative numbers Two’s Complement Two’s complement of 0 10 and = = = = I. Representation of numerical information Note: Ignore carry out of the MSB position Note: -128 does not have a positive counterpart, that is –128 is its own Complement : this can creat problems ……
2’s Complement Addition + Substraction > > > > > => = 3 I. Representation of numerical information Overflow in the last position Rule : Ignore any carry beyond the MSB. The result is correct if the range of the number system Is not exceeded.
2’s Complement Addition + Substraction I. Representation of numerical information Addition/Substraction rule : Assume that the signed integers are represented in 2s complement : To compute A – B, compute the 2’s complement B’ of B and add B’ to A Examples : ( = 2’) (ignore the leftmost carry
Overflow Addition of 2 numbers with different signs can never overflow Addition of 2 numbers with same sign = = -5wrong If signs of (addends) are same and sign of sum is different I. Representation of numerical information
Substraction rules (2’s complement) Same overflow rules apply I. Representation of numerical information
Binary multiplication uses a shift register and adder, and logic control Signed multiplication + + = + + - = - - - = + I. Representation of numerical information
Binary codes for decimal numbers Goal: Use binary strings (sequence of 0’s and 1’s) to represent decimal information Definition: A code is a set of n-bit strings. Each n-bit string represrnts a different number. A code word is a particular member of a code II. Representation of non numerical information
Binary codes for decimal numbers A code is a set of n-bit strings. Each n-bit string represrnts a different number. A code word is a particular member of a code If n bit is used to represent each code words, the total number of possible code words is 2 n Examples: To encode a set consisting of 3 elements {A,B,C} we need 2 bits to represent each code word. The possible code words are {00, 01, 10, 11} II. Representation of non numerical information
Binary codes for decimal numbers We need to assign a code word to each element of the original set. Assignment 1 : > A > B > C Code 11 is unused Another assignment : > A > B > C Code 00 is unused II. Representation of non numerical information
Binary codes for decimal numbers To encode the 10 decimal digits {0,1,….,8,9} We need 4 bit binary code words. There are 16=2 4 possible code words. So 6 codes are unused. Assignment 1 : > > > > > > > > > > 1001 The following codes are unused: 1010, 1011, 1100, 1101, 1110, 1111 II. Representation of non numerical information