Digital Logic & Design Dr. Waseem Ikram Lecture 02
Recap Last lecture discussion Decimal Number Systems Caveman Base 5 Number System Binary Number System Number System Conversion Today’s lecture discussion Decimal-Binary Conversion In the second part of the last lecture we discussed three Number systems. The decimal number system, a hypothetical base 5 caveman number system and the binary number system used by digital systems. All three number systems are positional number systems. Roman number system is not a positional number system. Number System Conversion. I hope you had the chance to practice with caveman and binary numbers and calculating their decimal equivalents. Today we would be starting our discussion on number systems by looking at binary to decimal and decimal to binary conversion. Learning to convert numbers from binary to decimal and decimal to binary is important as all real world values are represented in terms of decimal numbers and all digital systems represent values in terms of binary. Thus you would like to know how to represent 63.35 in binary and how the binary output 11101 of a digital system can be represented in decimal.
Binary to Decimal Conversion Sum-of-Weights Expression base number & weights Sum terms Paper and pencil method Sum of non-zero terms Mental Arithmetic, quick method Sum of weights of non-zero terms Lets first look at the conversion from binary number system to decimal number system. In the last lecture I had mentioned that there are two methods to convert a binary number to decimal. The Sum-of-Weights method and the sum of non-zero terms. Lets consider both methods. We have already been converting binary numbers to decimal using the Sum-of-Weights. Converting the binary number 100112 using the Sum-of-Weights method requires writing an expression in terms of the base number and weights. (1 x 24) + (0 x 23) + (0 x 22) + (1 x 21) + (1 x 20) Solving the expression results in the terms 16, 0, 0, 2 and 1 which add up to 19.
Binary to Decimal Conversion Sum-of-Weights 100112 (1 x 24) + (0 x 23) + (0 x 22) + (1 x 21) + (1 x 20) Terms 16, 0, 0, 2 and 1 19 Converting the binary number 100112 using the Sum-of-Weights method requires writing an expression in terms of the base number and weights. (1 x 24) + (0 x 23) + (0 x 22) + (1 x 21) + (1 x 20) Solving the expression results in the terms 16, 0, 0, 2 and 1 which add up to 19.
Binary to Decimal Conversion Add weights of non-zero terms Weights increase/decrease by power of 2 100112 = 16 + 2 + 1 = 19 1011.1012 = 8 + 2 + 1 + 1/2 + 1/8 = 11 + 5/8 = 11.625 A quicker method is to add the weights of non-zero terms. What are the weights of the non-zero terms for the binary number 100112? If you remember the weight of the binary bits increase by a factor of two starting from the least significant bit. Thus the weights of non-zero terms are 16, 2 and 1. Adding the weights of non-zero terms results in 19. Adding the weights of non-zero terms involves mental arithmetic and is quick way of converting binary numbers into decimal. Consider another example of converting 1011.1012 to decimal using sum of non-zero terms. The weights of the non-zero terms are 8, 2, 1, ½ and 1/8 which add up to 11.625. You should practice the adding of non-zero terms to find decimal equivalents of binary numbers as it is a convenient and quick way to convert binary into decimal.
Decimal to Binary Conversion Sum-of-Weights method used in reverse Highest binary weight less than the decimal number Subsequent smaller weights that add up to decimal number Repeated division by 2 Paper and pencil method Number repeatedly divided by 2 Conversion from decimal to binary can be done using two different methods. Sum of weights method is used in reverse involves mental arithmetic and allow for quick conversion of small decimal numbers. The repeated division by 2 is a paper and pencil method used for converting large decimal numbers to binary. In the Sum-of-weights method the conversion starts by noting the highest weight that is smaller than the decimal number to be converted.
Decimal to Binary Conversion using Sum-of-Weights number Weight Result after subtraction Binary Bit 392 256 392-256 = 136 1 b8 136 128 136-128 = 8 1 b7 8 64 0 b6 32 0 b5 16 0 b4 8-8 =0 1 b3 4 0 b2 2 0 b1 1 0 b0 Consider the decimal number 392 which is to be converted into binary. The highest weight that is smaller than 392 is 256. The next highest weight is 512 which is not smaller than 392. As the next step in the conversion process 1 is written at bit position 8 to represent the weight 256. Why bit position 8? Because bit position 8 represents the weight 256. The weight 256 is subtracted from 392 to result in 136. The process of determining the next highest weight which is smaller than 136 is determined, which in this particular case is 128. Thus another 1 is written at bit position 7 to represent the weight 128. The weight 128 is subtracted from the previous result of subtraction 136, which results in 8. The process of comparing the result with the next highest weight is repeated. The weight 64 is greater than the result 8, thus a zero is added to the binary bits ‘11’ Successive lower weights 32 and 16 both are larger than 8. Thus two more binary bits are set to ’00’. The next lower weight is exactly equal to 8. The corresponding binary bit is set to ‘1’. The result 8 is subtracted from the weight 8 resulting in 0. The next three weights are all greater than 0, therefore three 0 bits are added to signify 0 weights. The binary number is 1100010002
Decimal to Binary Conversion number Quotient after division Remainder after division 392 196 0 (b0) 98 0 (b1) 49 0 (b2) 24 1 (b3) 12 0 (b4) 6 0 (b5) 3 0 (b6) 1 1 (b7) 1 (b8) Repeated Division-by-2 method allows decimal numbers of any magnitude to be converted into binary. The original number is divided by 2, its quotient and the remainder is noted. The quotient obtained in the first step is again divided and the new quotient and remainder value noted. The process of repeatedly dividing the previous quotient value and noting the remainder continues until the quotient value becomes 0. The remainders noted in the repeated division are written out in reverse to represent the binary equivalent of the decimal number. 110001000
Binary-Decimal fraction conversion Binary to Decimal Conversion Sum-of-Weights method Weights decrease by a factor of 2 0.11012 weights ½, ¼, 1/16 Sum up to 0.8125 Decimal to Binary Conversion Repeated Multiplication by 2 example Converting fractions in decimal number system to Binary and vice versa is also important. Binary to Decimal conversion uses the Sum-of Weight method used earlier. The weight of binary bits constituting the fraction part decreases by a factor of 2 starting from the bit to the immediate left of the decimal point. Thus 0.11012 has the weights ½, ¼ and 1/16 for the non-zero terms. Adding the non-zero term weights results in 13/16 or 0.8125
Decimal-Binary fraction conversion Decimal to Binary Conversion Repeated multiplication by 2 Number Mult. By 2 Integer 0.8125 1.625 1 (b-1) 0.625 1.250 1 (b-2) 0.250 0.500 0 (b-3) 1.000 1 (b-4) Converting fractions from decimal to binary requires repeated multiplication by 2. The decimal number 0.8125 to be converted into binary is multiplied by 2 which results in 1.625. The integer part ‘1’ is noted and the fraction part is multiplied by 2. The result is 1.250. The integer part ‘1’ is noted and the fraction part is multiplied by 2. The result is 0.5. The integer part ‘0’ is noted and the fraction part is multiplied by 2. The result is 1. Since the fraction part is 0, therefore there is no further need for multiplying with 2. The integer part is noted and the noted integer values are written out preceded by a decimal point to give 0.1101
Binary Arithmetic Binary Addition Binary Subtraction Binary Multiplication Binary Division We have looked at binary to decimal conversions and decimal to binary conversions. Now we know how to convert integers as well as fractions from one number system to the other. Now lets look at simple arithmetic operations such as add, subtract, multiply and divide. We should be able to perform simple arithmetic operations such as add, subtract, multiply and divide on binary numbers, as we normally do with decimal numbers. Arithmetic operations performed on binary numbers are in fact exactly like the operations performed on decimal numbers. Let us start by looking at the binary addition operation
Binary Addition 1st digit 2nd digit Sum Carry 1 Four Basic rules for binary addition 1st digit 2nd digit Sum Carry 1 Looking at the results the first three additions give a result 0, 1 and 1 respectively which can be represented by a single binary digit (bit). The fourth addition results in the number 2, which is represented in binary as 102. Thus two digits (bits) are required. The sum bit is 0 and the carry bit is 1. This is similar to the addition of 9 + 3 in decimal. The answer is 12 which can not be represented by a single digit, thus two digits are required. The number 2 is the sum part and 1 is the carry part. Addition of multiple binary numbers
Binary Addition Carry 1 10 1st Number 2nd Number 3rd Number 4th Number 2nd Number 3rd Number 4th Number Result Similar to Decimal addition any number of binary numbers having any number of digits can be added together. Thus the number 1011, 110, 1000 and 11 can be added together. Most significant digits (bits) of second and fourth numbers are assumed to be zero. At Bit 2 the carry is 10, is it possible to have a 2-bit carry? Consider adding the number 19, fifteen times. Adding the first digit 9, fifteen times results in 135. 5 is the Sum and 13 is the carry. The carry 13 added with fifteen 1s of the second digit result in 28. The answer is 285.
Binary Subtraction 1st digit 2nd digit Difference Borrow 1 Four Basic rules for binary subtraction 1st digit 2nd digit Difference Borrow 1 The 2nd digit is subtracted from the first. The result of the first, third and fourth subtraction operations result in a difference value where no borrow is required. The second subtraction subtracts 1 from 0 for which a borrow is required to make the first digit equal to 2. The answer is 1. This is similar to what is done in decimal 21-17 where 7 can not be subtracted from 1 therefore a 1 is borrowed from the next most significant digit to allow 7 to be subtracted from 11. Thus if only the least significant digit of the decimal numbers is considered subtracting 7 from 1 results in a borrow of 1 and a difference of 4.
Binary Subtraction Borrow 1 1st Number 2nd Number Result
Binary Multiplication Four Basic rules for binary multiplication 1st digit 2nd digit Product 1 In binary multiplication anything multiplied by 0 results in 0 just like in decimal multiplication. Example of Binary Multiplication
Binary Multiplication 1101 (13) x 101 (5) 1st product term 1101 2nd product term 0000 3rd product term 1101 Product 1000001 (65) Binary multiplication is identical to the decimal multiplication. The first product term results in 1101 The second product term results in 0000 and is written shifted left by 1 bit The third product term results in 1101 and is written shifted left by 2 bits The product result is sum of all the partial product terms
Multiplication by shifting left Decimal 29 shifted left by one digit 290 Shift left 1 digit is multiply by 10 Binary 111012 (29) shifted left by one bit 1110102 (58) Shift left 1 bit is multiply by 2 Multiplication operations can be performed by shifting a number towards left. For example decimal 29 shifted left by 1 digit is like multiplying by 10 Shifting 29 by two digits towards left is multiplying by 102 or 100 Each shift left operation in decimal is equivalent to multiplying a number by 10. In a binary number system, each left shift is equivalent to multiplying by 2. Shifting 111012 twice towards left is 11101002 which is multiply by 4.
Binary Division 10 101 | 1101 101 011 000 11 Binary division follows the same rules as in decimal division. The binary division divides 11012 (13) by 1012 (5) which results in a quotient value of 102 (2) and a remainder 112 (3) which is in confirmation with equivalent decimal division.
Division by shifting right Decimal 29 shifted right by one digit 2.9 Shift left 1 digit is divide by 10 Binary 111012 (29) shifted left by one bit 1110.12 (14.5) Shift left 1 bit is divide by 2 Division operations can be performed by shifting a number towards right. For example decimal 29 shifted right by 1 digit is like dividing by 10 Shifting 29 by two digits towards right is dividing by 102 or 100 Each shift right operation in decimal is equivalent to dividing a number by 10. In a binary number system, each right shift is equivalent to dividing by 2. Shifting 111012 twice towards right is 111.012 which is divide by 4.
Signed and Unsigned Numbers Unsigned Binary Numbers Signed Binary Numbers Most significant bit represents sign 0 represents a positive number 1 represents a negative number Digital systems should be able to handle positive and negative numbers. So far we have only considered positive binary numbers. How does the digital system represent the + or – symbol? To handle positive and negative binary numbers, the digital system sets aside the most significant digit (bit) to represent the sign MSB set to 1 indicates a negative number MSB set to 0 indicates a positive number Thus +13 and -13 are represented as 01101 and 11101 respectively. Digital systems handle two different types of binary numbers, signed and unsigned. Thus 111012 represents -13 in signed binary and 29 in unsigned binary. The digital system has to be told beforehand to deal with a binary number as signed or unsigned.
2’s Complement form 1’s complement form 2’s complement form Binary number 01101 (13) 1’s complement 10010 + 1 2’s complement 10011 (-13) Informing the digital system beforehand to deal with a number as signed or unsigned is inconvenient Signed binary numbers are represented in their 2’s complement form. A 2’s complement of a binary number is achieved by first taking the 1’s complement of a number followed by its 2’s complement. The 1’s complement of a binary number is obtained by simply inverting each bit. The 2’s complement of a binary number is obtained by adding a 1 to the 1’s complement of the original number. In a 2’s complement form all negative binary numbers are represented in their 2’s complement form All such negative numbers have their most significant bit set to 1 signifying a negative number. All positive numbers are represented in their original form. Their most significant bit is a 0 specifying a positive number.
Addition and Subtraction with 2’s Complement 0101 +5 0101 +5 0010 +2 1110 -2 0111 +7 0011 +3 1011 -5 1011 -5 1110 -2 0010 +2 1001 -7 1101 -3 There are four cases of addition Both numbers are positive 0101 +5 0010 +2 0111 +7 Both numbers are negative 1011 -5 1110 -2 1001 -7 the carry generated from the msb is discarded One number is positive and its magnitude is larger than the negative number 0011 +3 the carry generated from the msb is discarded One number is positive and its magnitude is smaller than the negative number 1101 -3 By using signed number based on 2’s complement the addition operation serves to add and subtract numbers.
Range of Numbers Maximum Range Number of digits Decimal number example Binary number example Overflow The maximum range of values that can be represented in any number system depends upon the number of digits assigned to represent the value. A 6-digit car odometer can only count up to 999999 and then it rolls back to 000000. Similarly an 8-digit calculator can only handle integer numbers of the magnitude 99999999. A calculator that reserves the most significant digit to write + or – can only handle a maximum range of integer numbers from -9999999 to +9999999. A 3-bit unsigned binary number can have values ranging between 000 and 111. The total number of values are 8. A simple formula can used to determine the total number of unsigned value represented by n binary bits. 2n The magnitude of the largest binary number that can be represented is one less than the total number or range of values, 2n -1 If 100 and 111 unsigned numbers are added which give a result 1011, this result is considered to be out of range as 4 bits are required.
Range of Binary Numbers Decimal Number Sign Magnitude 2n-1, 2n-1 -1 2’s Complement Unsigned 2n, 2n - 1 0000 000 1 0001 001 2 0010 010 3 0011 011 4 0100 100 5 0101 101 6 0110 110 7 0111 111 Binary numbers can be represented in three different formats or methods Unsigned Signed 2’s complement In the unsigned method only positive numbers can be represented. To represent positive numbers from 0 to 7, a 3-bit binary combination is required. In the signed method an extra bit, the most significant bit is used to represent the sign of the number. 0 as the sign bit represents a positive number 1 as the sign bit represents a negative number Thus the signed representation of numbers 0 to 7 requires 4-bit binary combination. The 2’s complement representation only converts the negative values into their respective 2’s complement form, the positive values remain in their original form. Four bits are required to represent positive numbers 0 to 7 in their 2’s complement form.
Range of Binary Numbers Decimal Number Sign Magnitude 2n-1-1, -(2n-1 -1) 2’s Complement 2n-1, -2n-1 Unsigned -8 1000 -7 1111 1001 -6 1110 1010 -5 1101 1011 -4 1100 -3 -2 -1 In the unsigned form, negative numbers can not be represented thus numbers -1 to -8 have no unsigned binary representation In the signed representation the most significant bit is used to represent the sign of the number. Thus 4-bit signed binary representation is used to represent numbers -1 to -7. The most significant sign bit is 1 for all the negative numbers -8 can not be represented using 4-bits as the number 8 in its unsigned form requires 4-bits 1000. Representing -8 in signed format requires 5 bits 11000. In the 2’s complement form the numbers -1 to -8 can be represented in their 2’s complement form using 4 bits. A 4-bit sign magnitude number can handle a number range between -7 and +7. -8 can not be represented as 5-bits are required 11000. A 4-bit 2’s complement based signed number range is between -8 to +7. Let us find out the best way to store numbers. Since both negative and positive numbers are required, thus the unsigned binary representation is not appropriate as negative numbers can not be represented. The signed magnitude method although capable of representing both negative and positive numbers however the total range of numbers is one less than the 2’s complement method. Secondly, direct addition and subtraction of numbers using the signed magnitude representation does not give the actual result. It has to be converted back into signed magnitude format. 2,s complement based representation is the most efficient in terms of range of numbers and the fact that add and subtract operations directly result in a value which is represent able in 2’s complement number system without any translation or change.
Summary Binary to Decimal Conversion Sum-of-Weights Sum of non-zero terms Decimal to Binary Conversion Sum-of-Weights (in reverse) Repeated Division by 2
Summary Binary to Decimal fraction conversion Sum-of-Weights Repeated Multiplication by 2
Summary Binary Addition Binary Subtraction Binary Multiplication Multiplication by shift left operation Binary Division Division by shift right operation
Summary Unsigned Binary Signed Binary Sign Bit 2’s Complement Range of Binary Numbers
Lecture No. 2 Number Systems We end today’s lecture with this summary. As I mentioned at the end of my last lecture, you have to practice with binary numbers and conversion from one number system to the other. You are well aware of the decimal number system. You should start thinking in terms of the Binary Number system. In the next lecture we would be starting our discussion be looking at an alternate method of representing very large binary numbers having an integer part and a fraction part. The alternate method uses the floating point notation to represent large numbers. Good luck, Khuda hafiz and Asalam O Aleikum.