Binary Codes Dr. Mudathir A. Fagiri
Binary System The following are some of the technical terms used in binary system: Bit: It is the smallest unit of information used in a computer system. It can either have the value 0 or 1. Derived from the words Binary ditit. Nibble: It is a combination of 4 bits. Byte: It is a combination of 8 bits. Word: It is a combination of 16 bits. Double word: It is a combination of 32 bits. Kilobyte (KB): It is used to represent the 1024 bytes of information. Megabyte (MB): It is used to represent the 1024 KBs of information. Gigabyte (GB): It is used to represent the 1024 MBs of information. Octal System The weight associated with each digit in the given octal number can be determined by raising 8 to a power equivalent to the position of the digit in the number.
4-Bit Binary Coded Decimal (BCD) Systems The BCD system is employed by computer systems to encode the decimal number into its equivalent binary number. This is generally accomplished by encoding each digit of the decimal number into its equivalent binary sequence. The main advantage of BCD system is that it is a fast and efficient system to convert the decimal numbers into binary numbers as compared to the pure binary system.
4-Bit Binary Coded Decimal (BCD) Systems The 4-bit BCD system is usually employed by the computer systems to represent and process numerical data only. In the 4-bit BCD system, each digit of the decimal number is encoded to its corresponding 4-bit binary sequence. The two most popular 4-bit BCD systems are: Weighted 4-bit BCD code Excess-3 (XS-3) BCD code
Weighted 4-Bit BCD Code The weighted 4-bit BCD code is more commonly known as 8421 weighted code. It is called weighted code because it encodes the decimal system into binary system by using the concept of positional weighting into consideration. In this code, each decimal digit is encoded into its 4-bit binary number in which the bits from left to right have the weights 8, 4, 2, and 1, respectively.
Weighted 4-Bit BCD Code Decimal digits Weighted 4-bit BCD code 0 0000 1 0001 2 0010 3 0011 4 0100 5 0101 6 0110 7 0111 8 1000 9 1001 Apart from 8421, some other weighted BCD codes are 4221, 2421 and 5211.
Weighted 4-Bit BCD Code Example: Represent the decimal number 5327 in 8421 BCD code. The corresponding 4-bit 8421 BCD representation of decimal digit 5 is 0101 The corresponding 4-bit 8421 BCD representation of decimal digit 3 is 0011 The corresponding 4-bit 8421 BCD representation of decimal digit 2 is 0010 The corresponding 4-bit 8421 BCD representation of decimal digit 7 is 0111 Therefore, the 8421 BCD representation of decimal number 5327 is 0101001100100111.
Excess-3 BCD Code The Excess-3 (XS-3) BCD code does not use the principle of positional weights into consideration while converting the decimal numbers to 4-bit BCD system. Therefore, we can say that this code is a non-weighted BCD code. The function of XS-3 code is to transform the decimal numbers into their corresponding 4-bit BCD code. In this code, the decimal number is transformed to the 4-bit BCD code by first adding 3 to all the digits of the number and then converting the excess digits, so obtained, into their corresponding 8421 BCD code. Therefore, we can say that the XS-3 code is strongly related with 8421 BCD code in its functioning.
Excess-3 BCD Code Decimal digits Excess-3 BCD code 0 0011 1 0100 2 0101 3 0110 4 0111 5 1000 6 1001 7 1010 8 1011 9 1100
Excess-3 BCD Code Example : Convert the decimal number 85 to XS-3 BCD code. Add 3 to each digit of the given decimal number as: 8+3=11 5+3=8 The corresponding 4-bit 8421 BCD representation of the decimal digit 11 is 1011. The corresponding 4-bit 8421 BCD representation of the decimal digit 8 is 1000. Therefore, the XS-3 BCD representation of the decimal number 85 is 1011 1000.
Excess-3 BCD Code Apart from XS-3 code, the other non-weighted BCD code is 4-bit Gray code. 4-bit BCD systems are inadequate for representing and handling non-numeric data. For this purpose, 6-bit BCD and 8-BCD systems have been developed.
8-Bit BCD Systems The 6-bit BCD systems can handle numeric as well as non-numeric data but with few special characters. The 8-bit BCD systems were developed to overcome the limitations of 6-bit BCD systems, which can handle numeric as well as nonnumeric data with almost all the special characters such as +, -, *, /, @, $, etc. Therefore, the various codes under the category of 8-bit BCD systems are also known as alphanumeric codes.
8-Bit BCD Systems The three most popular 8-bit BCD codes are: Extended Binary Coded Decimal Interchange Code (EBCDIC) American Standard Code for Information Interchange (ASCII) Gray Code
EBCDIC Code The EBCDIC code is an 8-bit alphanumeric code that was developed by IBM to represent alphabets, decimal digits and special characters, including control characters. The EBCDIC codes are generally the decimal and the hexadecimal representation of different characters. This code is rarely used by non IBM-compatible computer systems.
ASCII Code The ASCII code is pronounced as ASKEE and is used for the same purpose for which the EBCDIC code is used. However, this code is more popular than EBCDIC code as unlike the EBCDIC code this code can be implemented by most of the non-ibm computer systems. Initially, this code was developed as a 7-bit BCD code to handle 128 characters but later it was modified to an 8-bit code.
Gray Code Gray code is another important code that is also used to convert the decimal number into 8-bit binary sequence. However, this conversion is carried in a manner that the contiguous digits of the decimal number differ from each other by one bit only.
Gray Code Decimal Number 8-Bit Gray Code 0 00000000 1 00000001 2 00000011 3 00000010 4 00000110 5 00000111 6 00001111 7 00001011 8 00001001 9 00001101
Gray Code We can convert the Gray coded number to its binary equivalent by remembering the following two major rules: The Most Significant Bit (MSB) of the Gray coded number and the equivalent binary number is always the same. The next-to-most significant bit of the binary number can be determined by adding the MSB of the binary number to the next-to-most significant bit of the gray coded number.
Gray Code Example: Convert the Gray coded number 11010011 to its binary equivalent. The following table lists the steps showing the conversion of the Gray coded number into its binary equivalent: Hence, the binary equivalent of Gray coded number 11010011 is 10011101 Gray Coded Digit Binary Addition Operation Binary Digit 1 1 1 1+1 0 0 0+0 0 1 1+0 1 0 0+1 1 0 0+1 1 1 1+1 0 1 1+1 1
Gray Code We can also convert a number represented in the binary form to Gray code representation. For carrying out this conversion, we need to remember the following two rules: The Most Significant Bit (MSB) of the binary number and the gray coded number is always the same. The next MSD of the gray coded number can be obtained by adding the subsequent pair of bits of the binary number starting from the left.
Gray Code Example: Convert the binary number 10100011 to its equivalent Gray coded number. Binary Digit Binary Addition Operation Gray Coded Digit 1 1 0 1+0 1 1 0+1 1 Hence, the Gray coded equivalent of the binary number 10100011 is 11110010. 0 1+0 1 0 0+0 0 0 0+0 0 1 0+1 1 1 1+1 0
Signed/Unsigned Numbers The unsigned binary number is the number with a magnitude of either zero or greater than zero, and are usually represented using the unsigned-magnitude representation, which only represents the magnitude of the numbers. This type of representation does not take the sign of the binary numbers into consideration while representing these numbers.
Signed/Unsigned Numbers The signed binary numbers are the numbers that are always associated with a sign. This sign helps in identifying whether the given binary number is a positive quantity or a negative quantity. Signed-magnitude representation is a method used in the computer system for representing the signed binary numbers. In this method, an extra bit called sign bit is associated with the magnitude of the given number. This sign bit is used to indicate whether the given binary number is positive or negative. The value of the sign bit is 0 for the positive numbers and 1 for the negative numbers.
Signed/Unsigned Numbers Sign bit 0 0 1 1 0 0 1 0 50 Sign bit 1 0 1 1 0 0 1 0-50
Signed/Unsigned Numbers Advantages of the signed-magnitude representation: It is very easy to represent and understand positive as well as negative numbers using this representation. The binary multiplication and the binary division of the signed binary numbers can be easily performed. Represent equal number of positive and negative quantities that makes it a very symmetrical method of representation.
Signed/Unsigned Numbers Disadvantages of the signed-magnitude representation: It is not an easy task to perform the binary addition and the binary subtraction using this representation. It provides two different representations of zero, one for plus zero and another for negative zero but actually they are the same values. This could lead to some confusion while performing various arithmetic operations.
Complements of Binary Numbers The complement system can also be used to represent the signed binary numbers apart from the signedmagnitude representation method. In the complement system, the positive integers are represented in a similar manner as they are represented in the signed-magnitude representation. The following are the two most popular complement methods used in the computer system: One s complement Two s complement
One s Complement One s complement method can be used to represent negative binary numbers. A negative number can be represented using one s complement method by first computing the binary equivalent of the number and then changing all the zeros with ones and all the ones with zeros. For example, the binary equivalent of the decimal number 15 is 00001111. Therefore, -15 can be represented using one s complement method as 11110000.
One s Complement The one s complement method also uses the left most bit as the sign bit to indicate the sign of the number. Sign bit 1 1 1 1 0 0 0 0-15
One s Complement Integers One s complement representation -7 1000-6 1001-5 1010-4 1011-3 1100-2 1101-1 1110-0 1111 +0 0000 +1 0001 +2 0010 +3 0011 +4 0100 +5 0101 +6 0110 +7 0111 The one s complement method of representing signed numbers also has two different representations for the number, zero.
One s Complement Example: Represent -25 in the one s complement system in byte size. The equivalent binary representation of 25 is in byte size is 00011001. Now, change all the zeros to ones and all the ones to zeros in order to obtain the ones complement representation: 11100110 Therefore, the one s complement representation of -25 is 11100110.
Two s Complement Two s complement is the most widely used method for representing negative numbers in the computer system. The two s complement of the given integer can be obtained by adding 1 to the one s complement of that number. For example, the two s complement representation of -15 can be obtained by adding 1 to 11110000, which is the one s complement representation of -15. Therefore, the two s complement representation of -15 is 11110001.
Two s Complement The two s complement method also uses the left most bit as the sign bit to indicate the sign of the number. Sign bit 1 1 1 1 0 0 0 1-15
Two s Complement Integers Two s complement representation -7 1001-6 1010-5 1011-4 1100-3 1101-2 1110-1 1111-0 0000 +0 0000 +1 0001 +2 0010 +3 0011 +4 0100 +5 0101 +6 0110 +7 0111
Two s Complement Example : Represent -33 in the two s complement system in byte size. The equivalent binary representation of 33 in a byte is 00100001. Now, change all the zeros to ones and all the ones to zeros in order to obtain the one s complement representation: 11011110. Add 1 to the 11011110. Therefore, the two s complement representation of -33 is 11011111.
Binary Subtraction Using Complements The complement methods can be used to perform the binary subtraction of the signed integers: Smaller number from larger one Larger number from smaller one
Smaller Number from Larger One Using one s complement: (1) Obtain the one s complement of the smaller number; (2) Perform the binary addition on the one s complement and the larger number; (3) If a carry is generated, add it to the calculated result for obtaining the final result;
Smaller Number from Larger One Example : Subtract 3 from 8 using the one s complement method. The equivalent binary representation of the decimal number 8 is 1000. The equivalent binary representation of the decimal number 3 is 0011. The one s complement representation of the smaller number 3 is 1100. Now, perform the binary addition of the one s complement and the larger number as: + 1 0 0 0 1 1 0 0 1 0 1 0 0 Add the generated carry to the calculated result as: + 0 1 0 0 1 0 1 0 1 Therefore, the result of the binary subtraction performed on the given numbers using one s complement method is 0101.
Smaller Number from Larger One Using two s complement: (1) Obtain the two s complement of the smaller number; (2) Perform the binary addition on the two s complement and the larger number; (3) Discard the carry to obtain the final answer;
Smaller Number from Larger One Example : Subtract 13 from 15 using the two s complement method. The equivalent binary representation of the decimal number 15 is 1111. The equivalent binary representation of the decimal number 13 is 1101. Now, perform the binary addition of the two s complement and the larger number as: + 1 1 1 1 1 0 0 1 1 0 0 1 0 Discard the carry to obtain the final answer, which is 0010. The two s complement representation of the smaller number 13 is 0011.
Larger Number from Smaller One Using one s complement: (1) Obtain the one s complement of the larger number; (2) Perform the binary addition on the one s complement and the smaller number to obtain the final answer;
Larger Number from Smaller One Example : Subtract 8 from 3 using the one s complement method. - The equivalent binary representation of the decimal number 8 is 1000. - The equivalent binary representation of the decimal number 3 is 0011. Now, perform the binary addition of the one s complement and the smaller number as: + 0 1 1 1 0 0 1 1 1 0 1 0 Therefore, the result of the binary subtraction performed on the given numbers using one s complement method is 1010. - The one s complement representation of the larger number 8 is 0111.
Larger Number from Smaller One Using two s complement: (1) Obtain the two s complement of the larger number; (2) Perform the binary addition on the two s complement and the smaller number to obtain the final answer;
Larger Number from Smaller One Example : Subtract 6 from 4 using two s complement method. - The equivalent binary representation of the decimal number 6 is 0110. - The equivalent binary representation of the decimal number 4 is 0100. - The two s complement representation of the larger number 6 is 1010. Now, perform the binary addition on the two s complement and the smaller number as: + 1 0 1 0 0 1 0 0 1 1 1 0 Therefore, the result of the binary subtraction performed on the given numbers using two s complement method is 1110.