Professional Documents
Culture Documents
[Parity check]
Bin sequence
3. to use parity check to obtain parity check bit for error detection and correction
Introduction
Assume, for example, that two devices are communicating with even parity, the transmitting device sends data,
it counts the number of set bits in each group of bits. If the number of set bits is even, it sets the parity bit to 0; if
the number of set bits is odd, it sets the parity bit to 1. In this way, every sequence have an even number of set
bits. On the receiving side, the device checks to make sure that it has an even number of set bits. If it finds an
odd number of set bits, the receiver knows there was an error during transmission.
NOTE:
The sender and receiver must both agree to use parity checking and to agree on whether parity is to be odd or
even. If the two sides are not configured with the same parity sense, communication will be impossible.
Course Module
[MECH662-matlab]
2
[Parity check]
MATLAB Code for Even Parity Check & Adding Parity Bit, in an User Dfined Bin Sequence
clc;
close all;
clear all;
a=input('Enter the Number of bits in the sequence to test for Even parity');
disp('Enter the bit sequence One by one to test for Even parity: ')
for i=1:a
x(i)=input('');
end
t=0;
for i = 1:a
if(x(i))
t = t + 1; %increment by one if a bit is one
end
end
if(mod(t,2)~=0) %check if not even then attach another '1' to make the parity
even
y = [x 1];
disp('Parity bit generated : 1');
else %check if even then attach another '0' to let the parity be even
y = [x 0]; disp('Parity bit generated : 0');
end
disp('Input bit sequence:');
disp(x); %display the input bit sequence
disp('Bit sequence with parity (Even) bit : ');
disp(y); %display the resultant bit sequence after parity bit addition
Sample input output for Even Parity Check & Adding Parity Bit
(1)
Enter the Number of bits in the sequence to test for Even parity
7
Enter the bit sequence One by one to test for Even parity:
1
0
1
1
0
1
1
Parity bit generated : 1
Input bit sequence:
1 0 1 1 0 1 1
[Parity check]
(2)
Enter the Number of bits in the sequence to test for Even parity
7
Enter the bit sequence One by one to test for Even parity:
1
0
1
1
0
1
0
Parity bit generated : 0
Input bit sequence:
1 0 1 1 0 1 0
MATLAB Code for Odd Parity Check & Adding Parity Bit, in an User Dfined Bin Sequence
clc;
close all;
clear all;
a=input('Enter the Number of bits in the sequence to test for Odd parity');
disp('Enter the bit sequence one by one to test for Odd parity: ')
for i=1:a
x(i)=input('');
end
t=0;
for i = 1:a
if(x(i)==1)
t = t + 1; %increment by one if a bit is one
end
end
if(mod(t,2)~=1) %check if not odd then attach another '1' to make the parity odd
y = [x 1]; disp('Parity bit generated : 1');
else %check if odd then attach another '0' to let the parity be odd
y = [x 0]; disp('Parity bit generated : 0');
end
disp('Input bit sequence:');
disp(x); %display the input bit sequence
disp('Bit sequence with parity (Odd) bit : ');
disp(y); %display the resultant bit sequence after parity bit addition
[MECH662-matlab] 4
[Parity check]
Sample input output for Even Parity Check & Adding Parity Bit
(1)
Enter the Number of bits in the sequence to test for Odd parity
7
Enter the bit sequence one by one to test for Odd parity:
1
0
1
1
0
1
0
Parity bit generated : 1
Input bit sequence:
1 0 1 1 0 1 0
(2)
Enter the Number of bits in the sequence to test for Odd parity
7
Enter the bit sequence one by one to test for Odd parity:
1
0
1
1
0
1
1
Parity bit generated : 0
Input bit sequence:
1 0 1 1 0 1 1