Binary Codes
Binary Codes
BCD Numbers: Binary Numbers are prevalent in digital systems, but for
representation of decimal numbers, specially where man-machine interfaces
exist, Binary Coded Decimal codes are used for ease of communication.
Weighted codes obey position weighting system i.e. position of each binary
digit also represents the positional value (weight). For each group of four
binary digits, sum of weights of these positions where binary digit is '1',
represents the value of the decimal number.
Binary Codes For Decimal Numbers
Weighted codes may be
Positively weighted: There are total 17 positively weighted codes,
with 1st weight being 1, 2nd weight can be 1 or 2. Sum of all weights
should be 9. Examples: 8421, 2421, 5211, 3321,4311
Negatively weighted. In Negatively weighted codes, some weights
are -ve. Examples: 642-3, 631-1, 8 4-2-1, 74-2-1
Non-weighted codes do not have positional weighting system. Excess-3
(XS-3) code and Gray Code are non-weighted codes..
BCD Codes
Note: 8421 binary code is referred as Natural Binary Coded Decimal code (NBCD).
Since there are many possible weights that can be assigned to derive BCD code,
there are following two properties that give some figure of merit to a code over
another Code: Self-Complementing Property and Reflective Property
Binary Code Types
Sequential Codes:
In this type of coding, each code word is one binary number greater than
the preceding code word.
These codes facilitate mathematical operations on data.
XS-3 and 8421 are sequential codes. 5421, 2421 and 642-3 are not
sequential codes.
Cyclic Codes:
In these codes, each successive code word differs from the preceding
one in only one bit position.
These codes are also called Unit Distance Codes. Cyclic codes help in
detecting transitional errors.
Gray code is a cyclic code.
Binary Code Types
Reflective Codes:
A reflected code is characterized by the fact
that it is imaged about the center entries with
one bit changed.
The 9's complement of a reflected BCD code
is formed by simply changing only one of its
bits. Ex: 9's Complement of 2 is 7
Code of 2 0 0 1 0
only 1 bit change
Code of 7: 1 0 1 0
Gray code is reflective code.
Binary Code Types (Gray Code)
Unit Distance Codes:
Codes in which each successive code word differ
from the preceding code in only one bit position.
Unit distance codes minimize transitional errors
(flashing). It is used for translating an analog
quantity such as shaft position in to a digital value.
Gray code is an example of Unit Distance code. It is
a non-weighted code and is not suitable for
arithmetic operations. It is not a BCD code also.
Gray code is also a Reflective Code.
An N-bit Gray code can be obtained by reflecting N-1
bit code about an axis at the end of the code and
putting the
MSB of 0 above the axis and
MSB of 1 below the axis.
Binary Code Types
Self Complementing Codes: A code is self
complementing if the code word of the 9's
complement of N, i.e. 9-N can be obtained from
the code word of N by interchanging all the 0s and
1s. Hence, code for 9 is the complement code of
0, 8 is the complement of 1, 7 is the complement
of 2 and so on.
The 2421, 5211, 642-3, 84-2-1 and XS-3 are self
complementing codes. 8421 & 5421 are not self-
complementing codes.
For a code to be self complementing, the sum
of all its weights must be 9.
Only 4 positively-weighted self complementing
codes are 2421, 5211, 3321 & 4311.
There are 13 negatively-weighted self
complementing codes.
Adjacent table gives an example of negatively
weighted (631-1), positively weighted (2421) and
non-weighted (Excess-3) codes that are self-
complementing.
Excess-3 (XS-3) Code
Excess-3:
XS-3code is obtained by adding 3 to the
normal binary code of a decimal
number.
In a normal XS-3 Code, the bit pattern
for 0 (0000) and 9 (1001) are not unit
distance apart.
Excess-3 Gray
XS-3 Gray code removes the above
anomaly and provides unit distance
between bit patterns for 0 and 9.
Each decimal digit is encoded with the
Gray Code pattern of the digit that is
greater by 3 (refer slide-10 on Gray
Code to see the code of 3, which is
treated as XS-3 Gray code of decimal
0 and so on).
Code Chart
Alphanumeric Codes
A binary digit or bit can represent only two symbols as it has only two states
'0' or '1'. Additional symbols are required to represent 26 alphabets with
capital and small letters, numbers from 0 to 9, punctuation marks etc.
Alphanumeric codes represent numbers, alphabetic, numeric and other
characters such as special symbols and various instructions necessary for
conveying information.
Following are the three alphanumeric codes very commonly used for data
representation.
American Standard Code for Information Interchange (ASCII).
Extended Binary Coded Decimal Interchange Code (EBCDIC).
Five bit Baudot Code.
ASCII code is a 7-bit code and EBCDIC is an 8-bit code.
ASCII code is more commonly used worldwide while EBCDIC is used
primarily in large IBM computers.
Error Detecting Codes
Environmental interference and physical defects in the communication
medium can cause random bit errors during data transmission. The 1s may
change to 0s and 0s may change to 1s. Therefore, data errors need to be
detected and corrected to ensure integrity of data.
Error coding is a method of detecting and correcting these errors to ensure
information is transferred intact from its source to its destination.
Error coding uses mathematical formulas to encode data bits at the source
into longer bit words for transmission.
The "code word" is decoded at the destination to retrieve the information. The
extra bits in the code word provide redundancy for the destination to decode
and determine errors introduced in the communication medium and in some
cases correct them to avoid re-sending data.
Different error coding schemes are chosen depending on the types of errors
expected, expected error rate in the communication medium, and whether or
not data re-transmission is possible. Popular error detecting techniques are:
Simple Parity check
Two-dimensional Parity check
Checksum
Cyclic redundancy check
Error Detecting Codes
Parity: Simplest and least expensive technique is addition of an extra bit
called parity bit to the data word being transmitted. Parity is of two types
Even Parity and Odd Parity.
For Even Parity, the parity bit is set to 0 or 1 at the transmitter such that
number of 1 bits is even.
For Odd Parity, the parity bit is set to 0 or 1 at the transmitter such that
number of 1 bits is odd.
Parity Error
in 3rd Row
Parity Error in 5th Column Parity Errors in 2nd & 4th Column
Error Detecting Codes
Checksum:
In this error detection scheme, the data is divided into k segments each of
m bits.
At the senders end the segments are added using 1s complement
arithmetic to get the sum. The sum is complemented to get the checksum,
which is sent along with the data segments.
At the receivers end, all received segments are added using 1s
redundancy check bits, are appended to the end of data unit so that the
resulting data unit becomes exactly divisible by a predetermined binary
number.
At the Receiver end, the incoming data unit is divided by the same number,
Final Code:
7-bit Hamming Code Pattern is 1 0 1 0 1 0 1
P1 P2 D3 P4 D5 D6 D7
Error Detection & Correcting Code
Hamming Code
Example: A 7-bit hamming code sequence 1001001 0111001 1110110
0011011 is received through a noisy channel. Decode the message
assuming a single bit error.
Since the received sequence is coded using 7-bit hamming Code, the
received bit stream of 28 bits is divided in 4 groups as shown below:-
1001001 0111001 1110110 0011011
Analyze 1st Group 1001001 (Even Parity Considered)
Bits 1,3,5,7 (1001) no error c1 = 0
Bits 2,3,5,7 (0001) error c2 = 1
Bits 4,5,6,7 (1001) no error c3 = 0
Error word is c3c2c1 = 010 = decimal 2
Corrected bit pattern is obtained by complementing 2nd bit 1101001
Analyze 2nd Group 0111001 (Even Parity Considered)
Bits 1,3,5,7 (0101) no error c1 = 0
Bits 2,3,5,7 (1101) error c2 = 1
Bits 4,5,6,7 (1001) no error c3 = 0
Error word is c3c2c1 = 010 = decimal 2
Corrected bit pattern is obtained by complementing 2nd bit 0011001
Analyze 3rd Group 1110110 (Even Parity Considered)
Bits 1,3,5,7 (1110) error c1 = 1
Bits 2,3,5,7 (1110) error c2 = 1
Bits 4,5,6,7 (0110) no error c3 = 0
Error word is c3c2c1 = 011 = decimal 3
Corrected bit pattern is obtained by complementing 3rd bit 1100110
Analyze 4th Group 0011011 (Even Parity Considered)
Bits 1,3,5,7 (0101) no error c1 = 0
Bits 2,3,5,7 (0111) error c2 = 1
Bits 4,5,6,7 (1011) error c3 = 1
Error word is c3c2c1 = 110 = decimal 6
Corrected bit pattern is obtained by complementing 6th bit 0011001
Bit Designation P1 P2 P4 P8
Data Bits D3 D5 D6 D7 D9 D1 D D1 D1 D1 D
11 15
0 2 3 4
Bit Designation P1 P2 P4 P8
Data Bits D3 D5 D6 D7 D9 D1 D D1 D1 D1 D
11 15
0 2 3 4
Parity Bits 1 0 0 1
Data Bits 0 1 1 0 1 1 1 0 1 0 1
P1 will be set for bits located at bit positions 1, 3, 5, 7, 9, 11, 13, 15 = P0101111 = 1
P2 will be set for bits located at bit positions 2, 3, 6, 7, 10, 11,14,15 = P0101101 = 0
P4 will be set for bits located at bit positions 4, 5, 6, 7,12, 13,14,15 = P1100101 = 0
P8 will be set for bits located at bit positions 8, 9,10,11,12,13,14,15 =P1110101 = 1
Example Hamming Code
Example:Test the Hamming code sequence 101001011101011 for any error.
Bit Sequence 1 0 1 0 0 1 0 1 1 1 0 1 0 1 1
Bit Designation P1 P2 P4 P8
Data Bits D3 D5 D6 D7 D9 D1 D D1 D1 D1 D
11 15
0 2 3 4