0% found this document useful (0 votes)
59 views50 pages

Binary Codes

The document discusses binary codes, including their classification into numeric and alphanumeric codes, and details various types of Binary Coded Decimal (BCD) codes. It also covers error detecting and correcting codes, including parity bits and Hamming codes, along with concepts like Hamming distance and self-complementing codes. Additionally, it introduces cyclic and reflective codes, as well as straight binary codes and BCD addition.

Uploaded by

21btc143
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views50 pages

Binary Codes

The document discusses binary codes, including their classification into numeric and alphanumeric codes, and details various types of Binary Coded Decimal (BCD) codes. It also covers error detecting and correcting codes, including parity bits and Hamming codes, along with concepts like Hamming distance and self-complementing codes. Additionally, it introduces cyclic and reflective codes, as well as straight binary codes and BCD addition.

Uploaded by

21btc143
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Binary Codes

by Dr. Sanjiv Tokekar


Binary Codes:
Classification of Binary Codes:
1. Numeric Codes.
2. Alphanumeric Codes.
•Numeric Codes are codes which represent numeric
information. ( 8 4 2 1, XS-3, Gray code)
• Numeric codes used to represent the decimal digits are
called Binary Coded Decimal (BCD) codes, 8421,2421, 5211
are BCD codes.
•Alphanumeric Codes: alphanumeric codes are codes which
represent alphanumeric information. ( EBCDIC, ASCII )
•In a decimal code digits of decimal numbers are encoded into
group of four binary digits.
•A sequence of binary digits of which represents a decimal
digit is called a code word. ( Six unassigned code words )
•BCD codes may be further divided as Weighted codes and
Non-weighted codes.
•Weighted codes are those which obey the position-weighting
principle. ( 8421, 2421, 8 4 -2 -1).
•Non – Weighted Codes are codes which are assign any
weight any digit position. ( Excess 3 code, Gray code )
•Positively weighted codes : Positively weighted codes are
those in which all weights assigned to the binary digits are
positive. ( 17 possible codes ) ( 8421, 2421, 5211, 3321 and
4311 ).
•Negatively weighted codes: Negatively-weighted codes are
those in which some of the weights assigned to binary digits
are negative. ( 642-3, 631-1, 84-2-1 and 74-2-1).
Binary Coded Decimal Codes
Decimal Digit 8421 2421 5211 5421 6 4 2 -3 8 4 -2 -1 XS-3

0 0000 0000 0000 0000 000 0 00 0 0 0011


1 0001 0001 0001 0001 010 1 01 1 1 0100
2 0010 0010 0011 0010 001 0 01 1 0 0101
3 0011 0011 0101 0011 100 1 01 0 1 0110
4 0100 0100 0111 0100 010 0 01 0 0 0111
5 0101 1011 1000 1000 101 1 10 1 1 1000
6 0 1 10 1100 1010 1001 011 0 10 1 0 1001
7 0111 1101 1100 1010 110 1 10 0 1 1010
8 1000 1110 1110 1011 101 0 10 0 0 1011
9 1001 1111 1111 1100 111 1 11 1 1 1100
Error Detecting and Error Correcting Codes
• Codes which allow only error detection are called error
detecting codes.
•Like: Parity, 2 out of 5.

• Codes which allow error detection as well as error correction


are called error correcting codes.
• Like: Hamming code.
Parity Bit

. Binary Parity Bit ( Even )


B1 B0 B1 B0 P
0 0 0 0 0
0 1 0 1 1
1 0 1 0 1
1 1 1 1 0
• 1011010111010 0 T

• 1010011111010 0 R
Two out of Five code
It can detect all single bit errors as well multiple
unidirectional errors
Decimal Digit 01236 74210
0 01100 11000
1 11000 00011
2 10100 00101
3 10010 00110
4 01010 01001
5 00110 01010
6 10001 01100
7 01001 10001
8 00101 10010
9 00011 10100
• 00110 T

• 01111 R
Error Correcting Codes
k-1
K bits at positions P1, P2, … Pk located at positions 2 from
message or data positions.
4 bit message (1101)
P1 P2 D3 P4 D5 D6 D7
x x 1 x 1 0 1
1,3,5,7 P1111 Even Parity P1=1,
2,3,6,7 P2101 Even Parity P2=0,
4,5,6,7 P4101 Even Parity P4=0,
Final Code 1010101
• Final Code 1010101 Transmitted

• Receiver 1010111
• P1=1, P2=0, D3 = 1 P4=0 D5=1, D6=1, D7=1.
• P1+D3+D5+D7 = 0
• P2+D3+D6+D7 = 1
• P4+D5+D6+D7 = 1
• P4,P2,P1 110 Convert D6=0. 1010101
• Final Code 1010101 Transmitted

• Receiver 1010101
• P1=1, P2=0, D3 = 1 P4=0 D5=0, D6=0, D7=1.
• P1+D3+D5+D7 = 0
• P2+D3+D6+D7 = 0
• P4+D5+D6+D7 = 0
• P4,P2,P1 000 No Error.
Hamming Code( 7 Bit, Data 4 Bit )
1 1 1 1,3,5,7
2 2 2 2,3,6,7
3 2+1 4 4,5,6,7
4 4
5 4+1
6 4+2
7 4+2+1
Decimal Number P4 P2 P1
0 0 0 0
1 0 0 1
2 0 1 0
3 0 1 1
4 1 0 0
5 1 0 1
6 1 1 0
7 1 1 1
Decimal Number P8 P4 P2 P1
0 0 0 0 0
1 0 0 0 1
2 0 0 1 0
3 0 0 1 1
4 0 1 0 0
5 0 1 0 1
6 0 1 1 0
7 0 1 1 1
8 1 0 0 0
9 1 0 0 1
10 1 0 1 0
11 1 0 1 1
12 1 1 0 0
13 1 1 0 1
14 1 1 1 0
Hamming Code( 11 Bit, Message Parity 4 Bit Total 15)
1 1 1 1,3,5,7,9,11,13,15
2 2 2 2,3,6,7,10,11,14,15
3 1+2 4 4,5,6,7,12,13,14,15
4 4 8 8,9,10,11,12,13,14,15
5 1+4
6 2+4
7 1+2+4
8 8
9 1+8
10 2+8
11 1+2+8
12 8+4
13 8+4+1
14 8+4+2
15 8+4+2+1
Hamming Code
D3 D5 D6 P1 P2 D3 P4 D5 D6
0 0 0 0 0 0 0 0 0
0 0 1 0 1 0 1 0 1
0 1 0 1 0 0 1 1 0
0 1 1 1 1 0 0 1 1
1 0 0 1 1 1 0 0 0
1 0 1 1 0 1 1 0 1
1 1 0 0 1 1 1 1 0
1 1 1 0 0 1 0 1 1
Hamming Distance
The number of code bit positions in which two code words differ is
called Hamming distance.
000 and 011 has hamming distance 2
Detect d errors Hamming Distance d+1
Correct d errors Hamming Distance 2d+1
Valid and Invalid code words
Parity : 00 01 10 11
000, 011, 101, 110 are valid code words with even parity,
001, 010, 100, 111 are invalid code words because they have odd
number of 1’s.
Hamming Distance
000 --- 001, 010, 100. 011 ---- 010, 001, 100.
101 ---- 100, 111, 001. 110 --- 111, 101, 010.

We can get 001 from 000,101 and 011.


Only error can be detected but not corrected.
Single bit error detection d=2.
• 0 0 000 000 00000 00000
• 0 1 000 111 00000 11111
• 10 111 000 11111 00000
• 1 1 111 111 11111 11111

• HD =3 HD=5
• Detect 2 errors, correct 1 error Detect 4 errors , correct 2 errors
• 0 000 001,010,100
Hamming Distance
Message bits = m = 7
Check Bits = r
If n are total bits to be transmitted.
Then n = m+r
m
2 legal sequences. Since n bits may change each at a
time so for every legal sequence there must be n invalid
sequences and 1 valid sequence. So total sequences for
each code words are n+1
Hamming Distance
Hence We must have (n+1)2m sequences.
2n => (n+1) 2m or 2(m+r) => (m+r+1) 2m means
2r => (m+r+1)
The minimum value of r which satisfies above equation for given m
should taken as r.
So for m=4, r=3, m=7 r=4, m=11, r=5, m=20
m=4
r=1 2>= 6
r=2 4>= 7
r=3 8>= 8
Hamming Distance
• m =8.
• 2r >= m+r+1
r=1 2>= 10
r=2 4>= 11
r=3 8>= 12
r= 4 16>=13 r=4 is okay for m=8. total message bits 12.
m=20
r=1
2>= 22 (m+r+1)
r=2
4>= 23
r=3 8>= 24
r=4 16>=25
r=5 32 >=26
m=20 r should taken as 5. P1, P2, P4, P8, P16
Sequential Codes
A sequential code is one, in which each succeeding code is
one binary number greater than its preceding code word.
Ex. 8421 code, Excess-3 code
Non-Sequential codes: 5211, 2421 and 632-3.
Facilitates mathematical manipulation of data.
Self-Complementing Codes
A code is said to be self complementing code, if the code
word of 9’s complement is same as 1’s complement.
Ex. 2421, 5211, 642-3, 84-2-1 and XS-3 are self
complementary.
8421 and 5421 are not self complementary.
Self complementary sum of weight is 9.
Useful when 9’s complement is required.
Cyclic Codes
Cyclic codes are those in which each successive code word
differs from the preceding one only in one bit position. They
are also called unit distance code.
Ex. Gray code
Decimal 0 1 2 3
Gray Code 00 01 11 10
Useful in translating analog quantity such as shaft position.
Reflective Codes
A reflective code is a binary code in which the n least
significant bits for code words 2n through 2n+1 -1 are the mirror
images of those 0 through 2n -1. The Gray code is reflective
code.
Reflective Codes
Decimal Number bit Gray 1 bit Gray 2 bit Gray 3 bit Gray 4 bit
0 0 00 0 00 0000
1 1 01 0 01 0001
2 11 0 11 0011
3 10 0 10 0010
4 1 10 0110
5 1 11 0111
6 1 01 0101
7 1 00 0100
8 1100
9 1101
10 1111
11 1110
12 1010
13 1011
• 001 001
• 010 011

• 001 000 010


• 001 011 010
Straight Binary Code
A code is straight binary code if code words are represented
in straight binary form. It is weighted code.
Ex. 8421 code
BCD Addition
• 232 0010 0011 0010
• +345 +0011 0100 0101
• ------ ----------------------------------
• 577 0101 0111 0111
• 237 0010 0011 0111
• +345 +0011 0100 0101
• ------ ----------------------------------
• 582 0101 0111 1100
• + 0000 0000 0110
• --------------------------
• 0101 1000 0010
• 1010, 1011, 1100, 1101, 1110,1111
• 1001, 0001
• 287 0010 1000 0111
• +395 + 0011 1001 0101
• ------ ----------------------------------
• 682 0110 0001 1100
• + 0000 0110 0110
• --------------------------
• 0110 1000 0010
9’complement BCD Subtraction
• 397 + 0011 1001 0111 0011 1001 0111
• -285 -0010 1000 0101 +0111 0001 0100
• ------ ---------------------------------- ---------------------------
• 112 0001 0001 0010 1010 1010 1011
• 0110 0110 0110
• ---------------------------
• 1 0001 0001 0001
• +1
• -------------------------
• 0001 0001 0010
10’complement BCD Subtraction
• 397 + 0011 1001 0111 0011 1001 0111
• -285 -0010 1000 0101 +0111 0001 0101
• ------ ---------------------------------- ---------------------------
• 112 0001 0001 0010 1010 1010 1100
• 0110 0110 0110
• ---------------------------
• 1 0001 0001 0010


10’complement BCD Subtraction
• 397 + 0011 1001 0111 0011 1001 0111
• -280 -0010 1000 0000 +0111 0010 0000
• ------ ---------------------------------- ---------------------------
• 117 0001 0001 0010 1010 1011 0111
• 0110 0110 0000
• ---------------------------
• 1 0001 0001 0111

Excess 3 Addition
• 45 0111 1000
• + 34 +0110 0111
• -------------------------------------------
• 79 1101 1111 1101 1111
• - 0011 -0011 +1101 1101
• ------------------ ------------------
• 1010 1100 1 1010 1 1100
• -0011 has 1’c complement 1100, so 2’s complement 1101. so add 1101.
Ignore the carry coming out out of group of 4 bits. Final answer is
• 1010 1100 (i.e. 79 )
Excess 3 Addition
• 162 0100 1001 0101
• + 273 + 0101 1010 0110
• -----------------------------------------------------
• 435 1010 0011 1011
• 1101 0011 1101
• ----------------------------
• 1 0111 0110 1 1000
• Ignore carry in both case. Final answer is 0111 0110 1000 (i.e. 435)
Excess 3 Subtraction 9’s Complement
• 273 0101 1010 0110 0101 1010 0110
• -162 -0100 1001 0101 1011 0110 1010
• ------------------------------------------------------------------------------------------
• 111 0001 0001 0001 1 0001 0001 0000
• +1
• -------------------------- --------------------------
• 0100 0100 0100 0001 0001 0001
• 0011 0011 0011
• --------------------------
• 0100 0100 0100
Excess 3 Subtraction 10’s Complement
• 749 1010 0111 1100 1010 0111 1100
• -363 - 0110 1001 0110 1001 0110 1010
• ------------------------------------------------------------------------------------------
• 386 Ignore carry 1 0011 1110 0110
• 0011 1101 0011
• --------------------------
• 0110 1 1011 1001
• Final answer ( 0110 1011 1001 I.e. 386)

Binary to Gray Conversion
• Binary 1010
• B3, B2, B1, B0 Binary digits and G3, G2, G1 and G0 Gray Digits
• G3 = B3
• G2 = B3 ( EXOR) B2
• G1 = B2 ( EXOR) B1
• G0 = B1 ( EXOR) B0
•B 1 0 1 0
•G 1 1 1 1
Binary to Gray Conversion
• Binary 0111
• B3, B2, B1, B0 Binary digits and G3, G2, G1 and G0 Gray Digits
• G3 = B3
• G2 = B3 ( EXOR) B2
• G1 = B2 ( EXOR) B1
• G2 = B1 ( EXOR) B0
•B 0 1 1 1
•G 0 1 0 0
Binary to Gray Conversion
•B 0 1 1 0
•G 0 1 0 1

•B 1 0 1 1 0 1
•G 1 1 1 0 1 1

•G 1 1 1 0 1 1
•B 1 0 1 1 0 1
Gray Binary Conversion
• 1011
• B3 = G3
• B2 = B3 (EXOR ) G2
• B1 = B2 (EXOR ) G1
B0 = B1 (EXOR) G0
G 1 0 1 1
B 1 1 0 1
Gray Binary Conversion
• G 1101
• B 1001

You might also like