You are on page 1of 53

Binary Code

Computer codes are codes that help in converting the data


( text, image, sound, numeric data , alphanumeric data),
entered by the users into the binary form.

Computer codes use binary coding schemes. In binary


coding, every symbol( a, H, 23, %, #) that appears in the data is
represented by a group of bits.
Commonly used computer
codes
•BCD CODE

•EXCESS – 3 CODE

•GRAY CODE

•EBCDIC

•ASCII
1. BCD CODE
•BCD stands for Binary-Coded Decimal.

•In this code , decimal digit o through 9 are


represented(coded)by their natural binary equivalents using
four bits and each decimal digit of a decimal number is
represented by this four bit code individually.

•It is a weighted code.

•This code is also known as 8 4 2 1 code. where this 842 and 1 are
the weights of the 4-bit of the binary code of each decimal digit.
Decimal Symbol Binary BCD BCD
0 0000 0000
1 0001 0001
2 0010 0010
3 0011 0011 Table:
4 0100 0100 Binary-Coded
5 0101 0101 Decimal(BCD)
6 0110 0110
7 0111 0111
8 1000 1000
9 1001 1001
10 1010 0001 0000
11 1011 0001 0001
12 1100 0001 0010
13 1101 0001 0011
14 1110 0001 0100
Example: 1

Decimal number 4926 4 9 2 6

8421 BCD coded number 0100 1001 0010 0110

(4926)10 = (0100100100100110)BCD
EX:2 Convert the decimal number 350 to its
BCD equivalent.

Decimal Number 3 5 0

BCD Coded Number 0011 0101 0000

(350)10 = (001101010000)BCD
EX:3 Convert the decimal number 365 to its BCD
equivalent.
(365)10 = (0011 0110 0101)BCD

This is different to converting to binary, which is


(365)10 = (101101101)2

Clearly, BCD requires more bits. BUT, it is easier to


understand/interpret
EX:4 Convert the BCD coded number
1000 0111 0001 into decimal.

BCD Coded Number 1000 0111 0001

Decimal Number 8 7 1
4. BCD enables fast conversions from denary to
binary for applications such as pocket calculators.
Each digit on a calculator corresponds directly to a
four-bit block in BCD.
BCD Addition

There are three case:

(1) Sum <= 9, Final Carry=0 Answer is correct

(2) Sum <= 9, Final Carry=1 Answer is incorrect, then add 6 (0110)

(3) Sum > 9, Final Carry=0 Answer is incorrect , then add 6 (0110)
Ex: Find the BCD sum of 184+576
184 0001 1000 0100
+ 576 + 0101 0111 0110
0110 1111 1010 invalid BCD code(add 0110)
0110 0110
760 0111 0110 0000 (760)10

Ans= (011101100000)BCD
BCD SUBTRACTION

Using 10’s Complement.

Step 1 : Convert number to be subtracted to its 10’s complement form

Step 2: Perform the addition .

Step 3: (i) If the final carry is generated then the result is in its true
form. Discard end carry.

(ii) If final carry is not generated. result obtained in step 2 is


negative and in the 10’s complement form.
Examples:
(a)375-240 (b)3250-72532 (C ) 72532-3250
Sol: (a) 375+(-240)
= 375
10’s complement of (240) = + 760
Sum = 1135
Discard the end carry
Answer = 135
(b) 3250+(-72532)
= 03250
10’s complement of (72532) = + 27468
Sum = 30718

There is no end carry. Therefore, the answer is negative and in the 10’s complement
form
Answer = - (10’s complement of 30718)
= -69282
(C ) 72532+ (-3250 )
= 72532
10’s complement of (03250 ) = + 96750
Sum = 169282
Discard the end carry
Answer = 69282
Both the numbers must have the same number of digits ,so we write 3250 as 03250.
Example: subtract 984-599 using 10’s complement.
Example: subtract 984-599 using 9’s complement.
2. EXCESS-3 CODE
Excess-3 code is also treated as XS-3 Code. The Excess-3 code is a
un-weighted & self complementary BCD code used to represent the
decimal numbers.

Excess - 3 code is another form of BCD code.

The Excess-3 code for each decimal digit is obtained by adding


decimal 3 to the natural BCD code of the digit or Add 3 to each digit of
decimal and convert to 4-bit binary form.

This code plays an important role in arithmetic operation because it


resolves deficiencies encountered when we use the 8421 BCD code for
adding two decimal digit whose sum is greater than 9.
Self Complementing Property: This code is a self complementing
code. which means 1’s complement of the coded number yields 9’s
complement of the number itself.
Table: Excess-3 Code
Decimal BCD Code +3 Excess-3 Code
Digit
0 0000 0000+0011 0011
1 0001 0001+0011 0100
2 0010 0010+0011 0101
3 0011 0011+0011 0110
4 0100 0100+0011 0111
5 0101 0101+0011 1000
6 0110 0110+0011 1001
7 0111 0111+0011 1010
8 1000 1000+0011 1011
9 1001 1001+0011 1100
Ex: Excess-3 Code for Decimal
Numbers:
(a) 24
Ans: Decimal Number 2 4
BCD Coded Number 0010 0100
Add 3 0011 0011
0101 0111

(24)10 = (01010111)xs-3

(b) 658
Decimal Number 6 5 8
BCD Coded Number 0110 0101 1000
Add 3 0011 0011 0011
1001 1000 1011
(658)10 = (100110001011)xs-3
3. Gray code
•Un weighted Code.
•Unit distance code & Minimum error code.
•It is very useful code in which a decimal number is
represented in binary form in such a way so that each
gray code number differ from the preceding & the
succeeding number by a single bit.
•The Gray code is NOT a BCD code.
DECIMAL BINARY GRAY CODE
0 0000 0000
1 0001 0001
2 0010 0011
3 0011 0010
4 0100 0110
5 0101 0111
6 0110 0101
Table: GRAY CODE
7 0111 0100
8 1000 1100
9 1001 1101
10 1010 1111
11 1011 1110
12 1100 1010
13 1101 1011
14 1110 1001
15 1111 1000
Binary to Gray Code Conversion
Step 1: Record the MSB as it is.
Step 2: Add the MSB of the binary code to the next bit of binary code, record
the sum and neglect the carry.
Step 3: Repeat the process.

Binary code b3 b2 b1 b0
Gray Code g3 g2 g1 g0
g3=b3 (b3 +b2 ) (b2+b1) (b1+b0)
Ex: Convert binary number to Gray code.
(a) 1011 (b) 1110

Answer (a) 1110 (b) 1001


Gray to Binary Code Conversion

Step 1: Record the MSB of gray code as it is.


Step 2: Add the MSB of the binary code to the next bit of gray code,
record the sum and neglect the carry.
Step 3: Repeat the process.

Gray code g3 g2 g1 g0
Binary Code b3 b2 b1 b0
g3 = b3 (b3+g2 ) (b2+g1) (b1+g0)
Ex: Convert Gray code to binary code.
(a) 1110 (b) 1001
Ans: (a) 1011 (b) 1110
There are two main coding systems that provide conversions of
keyboard characters into binary:

EBCDIC
ASCII
4. EBCDIC CODE
• EBCDIC stands for Extended Binary Coded Decimal
Interchange Code.
• It uses 8 bits (4 bit for zone , 4 bit for digit) to represent a
symbol in the data.
• It can represent 256 different characters.
• It is used to represent decimal numbers(0-9), lower case
letters(a-z), uppercase letters(A-Z), Special characters, and
control characters(printable and non printable).
• EBCDIC codes are mainly used in the mainframe computer
developed by IBM.
Coding of Alphabetic and Numeric Characters in
Ebcdic
Character Zone Digit

a–i 1000 0001 - 1001

j–r 1001 0001 - 1001

s- z 1010 0010 - 1001

A–I 1100 0001 - 1001

J–R 1101 0001 –-1001

S–Z 1110 0010 - 1001

0 - 9 1111 0000 - 1001


Coding of Special Characters in EBCDIC
Example: Using binary notation, write EBCDIC coding for
the word BIT.
Solution:
B = 1100 0010 in EBCDIC binary notation
I = 1100 1001 in EBCDIC binary notation
T = 1110 0011 in EBCDIC binary notation

Hence, EBCDIC coding for the word BIT in binary notation will
be

11000010 11001001 11100011


B I T
5. ASCII CODE
• ASCII stands for the American Standard Code for Information
Interchange.
• It has been adopted as the industry-standard way of representing
keyboard characters as binary codes.
• Every keyboard character is given a corresponding binary code.
• ASCII is of two types ASCII-7 and ASCII-8
• ASCII-7 uses 7 bits to represent a symbol and can represent 128
different characters
• ASCII-8 uses 8 bits to represent a symbol and can represent 256
different characters
• First 128 characters in ASCII-7 and ASCII-8 are same.
• Most computers use ASCII codes to represent text, which makes It
possible to transfer data from one computer to another. ANSI(American
National Standard Institute) invented this code.
8-bit ASCII Chart
7-bit ASCII Chart
Example: Write binary coding for the word BOY in ASCII-7.

Solution:

B = 1000010 in ASCII-7 binary notation


O = 1001111 in ASCII-7 binary notation
Y = 1011001 in ASCII-7 binary notation
Hence, binary coding for the word BOY in ASCII-7 will be

1000010 1001111 1011001


B O Y
Example: Decode the following ASCII Code:
1010011 1001011 1011001
Solution:
1010011 = S in ASCII-Code
1001011 = K in ASCII-Code
1011001 = Y in ASCII-Code
Hence, binary coding for the word BOY in ASCII-7 will be
1010011 1001011 1011001
S K Y
Error Detection & Correction Codes
We know that the bits 0 and 1 corresponding to two different range of analog voltages.
So, during transmission of binary data from one system to the other, the noise may also be
added. Due to this, there may be errors in the received data at other system.
That means a bit 0 may change to 1 or a bit 1 may change to 0. We can’t avoid the
interference of noise. But, we can get back the original data first by detecting whether any
errors present and then correcting those errors. For this purpose, we can use the following
codes.
Error detection codes and Error correction codes

Types Of Errors
1. Single Bit Errors
The change in one bit in the whole data sequence , is called “Single bit error”. Occurrence of
single bit error is very rare in serial communication system.
2. Burst Errors
The change of set of bits in data sequence is called “Burst error”. It means two or more bits
in the data unit has changed .The burst error is calculated in from the first bit change to last
bit change.
Error detection codes − are used to detect the errors present in the
received data bit stream. These codes contain some bits, which are
included appended to the original bit stream. These codes detect the error, if it is
occurred during transmission of the original data bit stream.
Example  − Parity code, Hamming code.

Error correction codes − are used to correct the errors present in the


received data bit stream so that, we will get the original data. Error correction
codes also use the similar strategy of error detection codes.
Example  − Hamming code.

“ Therefore, to detect and correct the errors, additional bits are appended to


the data bits at the time of transmission.”
Parity Code
It is easy to include append one parity bit either to the left of MSB or
to the right of LSB of original bit stream. There are two types of parity
codes, namely even parity code and odd parity code based on the type of
parity being chosen.

1.Even Parity Code:


The value of even parity bit should be zero, if even number of ones
present in the binary code. Otherwise, it should be one. So that, even
number of ones present in even parity code. Even parity code contains
the data bits and even parity bit.
The following table shows the even parity codes corresponding to each 3-bit binary code. Here, the even
parity bit is included to the right of LSB of binary code.

Binary Code Even Parity bit Even Parity Code


000 0 0000
001 1 0011
010 1 0101
011 0 0110
100 1 1001
101 0 1010
110 0 1100
111 1 1111

Here, the number of bits present in the even parity codes is 4. So, the possible even number of ones in these
even parity codes are 0, 2 & 4.
• If the other system receives one of these even parity codes, then there is no error in the received data. The bits
other than even parity bit are same as that of binary code.
• If the other system receives other than even parity codes, then there will be an errors in the received data. In this
case, we can’t predict the original binary code because we don’t know the bit positions of error.
Therefore, even parity bit is useful only for detection of error in the received parity code. But, it is not sufficient
to correct the error.
2. Odd Parity Code:
The value of odd parity bit should be zero, if odd number of ones present in the
binary code. Otherwise, it should be one. So that, odd number of ones present in odd
parity code. Odd parity code contains the data bits and odd parity bit.
The following table shows the odd parity codes corresponding to each 3-bit
binary code. Here, the odd parity bit is included to the right of LSB of binary code.

Binary Code Odd Parity bit Odd Parity Code


000 1 0001
001 0 0010
010 0 0100
011 1 0111
100 0 1000
101 1 1011
110 1 1101
111 0 1110
Hamming Code
This error detecting and correcting code technique is developed by R. W.
Hamming . This code not only identifies the error bit, in the whole data
sequence and it also corrects it. This code uses a number of parity bits
located at certain positions in the codeword. The number of parity bits
depends upon the number of information bits. The hamming code uses
the relation between redundancy bits and the data bits and this code can
be applied to any number of data bits.

What  is a Redundancy Bit?


Redundancy means “The difference between number of bits of the actual
data sequence and the transmitted bits”. These redundancy bits are used
in communication system to detect and correct the errors, if any.
Hamming code is useful for both detection and correction of error
present in the received data. This code uses multiple parity bits and
we have to place these parity bits in the positions of powers of 2.

2P >= n + P +1
Here, n represents the number of bits in the data string.
P represents number of parity bits.

How the Hamming code actually corrects the errors?


In Hamming code, the redundancy bits are placed at certain
calculated positions in order to eliminate errors. The distance
between the two redundancy bits is called “Hamming distance”.
To understand the working and the data error correction and
detection mechanism of the hamming code, let’s see to the
following stages.
Number of parity bits
As we learned earlier, the number of parity bits to be added to a data string
depends upon the number of information bits of the data string which is to
be transmitted. Number of parity bits will be calculated by using the data
bits. This relation is given below.
2P >= n + P +1
Here, n represents the number of bits in the data string.
P represents number of parity bits.
For example, if we have 4 bit data string(e.g. 0111 , 0011), i.e.
n = 4, then the number of parity bits to be added can be found by using
trial and error method. Let’s take P = 2, then
2P = 22 = 4 and n+P+1=4+2+1=7
This violates the actual expression.
So let’s try P = 3, then
2P = 23 = 8 and n+P+1=4+3+1=8
So we can say that 3 parity bits are required to transfer the 4 bit data
with single bit error correction.
Where to Place these Parity Bits?
After calculating the number of parity bits required, we should know
the appropriate positions to place them in the information string, to
provide single bit error correction.
In the above considered example, we have 4 data bits and 3 parity bits.
So the total codeword to be transmitted is of 7 bits (4 + 3). We
generally represent the data sequence from right to left, as shown
below.
bit 7, bit 6, bit 5, bit 4, bit 3, bit 2, bit 1
The parity bits have to be located at the positions of powers of 2
(20 ,21 , 22 , 23 , 24 …… ). i.e. at 1, 2, 4, 8 and 16 etc. Therefore the
codeword after including the parity bits will be like this
D7, D6, D5, P4, D3, P2, P1
Here P1, P2 and P4 are parity bits. D3, D5, D6, D7 are data bits.
Example: Encode the data 1101 in even parity, by using Hamming
code.

Step 1
Calculate the required number of parity bits.
2P >= n + P + 1 here n = 4
Let P = 2, then
2P = 22 = 4 and n + P + 1 = 4 + 2 + 1 = 7.
2 parity bits are not sufficient for 4 bit data.
So let’s try P = 3, then
2P = 23 = 8 and n + P + 1 = 4 + 3 + 1 = 8
Therefore 3 parity bits are sufficient for 4 bit data.
 The total bits in the code word are 4 + 3 = 7
Step 2
Location of parity bit
The parity bits have to be located at the positions of powers of 2.
Here three parity bits to be appended. So (20 ,21 , 22 )i.e. at 1, 2, 4.
So the position of parity bit will be P1 , P2 , P4.
Therefore the codeword after including the parity bits will be like this
D7, D6, D5, P4, D3, P2, P1
Here P1, P2 and P4 are parity bits. D3, D5, D6, D7 are data bits.
Step 3
Determine the parity bits. ( D7, D6, D5, P4, D3, P2 P1 )
( 1 1 0 P4 1 P2 P1 )

For P1 : 3, 5 and 7 bits are having two 1’s so for even parity, P1 = 0.
(For P1 : check 1 bit and skip 1 bit, starting from P1)
For P2 : 3, 6 and 7 bits are having three 1’s so for even parity, P2 = 1.
(For P2 : check 2 bit and skip 2 bit, starting from P2),
For P4 : 5, 6 and 7 bits are having two 1’s so for even parity, P4 = 0.
(For P4 : check 4 bit and skip 4 bit, starting from P4)
By entering / inserting the parity bits at their respective positions, codeword
can be formed and is transmitted. It is
1100110.
Example: If received Hamming code is 1001111 with even
parity . Then detect and correct error.
Step 1
Calculate the required number of parity bits.
2P >= n + P + 1 = (n + P) + 1 = 7+1=8
2P >= 8 = 23
P = 3 Therefore 3 parity bits & 4 bit data.
  The total bits in the code word are 4 + 3 = 7.
Step 2
Location of parity bit
The parity bits have to be located at the positions of powers of 2.
Here three parity bits to be appended. So (20 ,21 , 22 )i.e. at 1, 2, 4.
So the position of parity bit will be P1 , P2 , P4.
Therefore the codeword after including the parity bits will be like this
D7, D6, D5, P4, D3, P2, P1
Here P1, P2 and P4 are parity bits. D3, D5, D6, D7 are data bits.
Step 3
Detect the error . ( D7, D6, D5, P4, D3, P2 P1 )
( 1 0 0 1 1 1 1 )

For P1 : 3, 5 and 7 bits are having two 1’s so for even parity, P1 = 1.
(For P1 : check 1 bit and skip 1 bit, starting from P1)
For P2 : 3, 6 and 7 bits are having three 1’s so for even parity, P2 = 1.
(For P2 : check 2 bit and skip 2 bit, starting from P2),
For P4 : 5, 6 and 7 bits are having two 1’s so for even parity, P4 = 0.
(For P4 : check 4 bit and skip 4 bit, starting from P4)
Step 4:
Correction of error : The Decimal value of check bits(new parity bit) gives the position of error in
received hamming code.
P4 P2 P1 = (011)2 = (3)10
There , the error present in third bit(b3) of Hamming code. Just complement the value present in that
bit and remove parity bits in order to get the original binary code.
Corrected data = 1001011

You might also like