You are on page 1of 8

Error Detection

Error
A condition when the receiver’s information does not matches with the sender’s information.
During transmission, digital signals suffer from noise that can introduce errors in the binary
bits travelling from sender to receiver. That means a 0 bit may change to 1 or a 1 bit may
change to 0.

Error Detecting Codes (Implemented either at Data link layer or Transport Layer of
OSI Model)
Whenever a message is transmitted, it may get scrambled by noise or data may get
corrupted. To avoid this, we use error-detecting codes which are additional data added to a
given digital message to help us detect if any error has occurred during transmission of the
message.

Basic approach used for error detection is the use of redundancy bits, where additional bits
are added to facilitate detection of errors.
Some popular techniques for error detection are:
1. Simple Parity check
2. Two-dimensional Parity check
3. Checksum
4. Cyclic redundancy check

1. Simple Parity check


Blocks of data from the source are subjected to a check bit or parity bit generator form,
where a parity of :

 1 is added to the block if it contains odd number of 1’s, and


 0 is added if it contains even number of 1’s
This scheme makes the total number of 1’s even, that is why it is called even parity
checking.
2. Two-dimensional Parity check
Parity check bits are calculated for each row, which is equivalent to a simple parity check
bit. Parity check bits are also calculated for all columns, then both are sent along with the
data. At the receiving end these are compared with the parity bits calculated on the
received data.

3. Checksum
 In checksum error detection scheme, the data is divided into k segments each of m
bits.
 In the sender’s end the segments are added using 1’s complement arithmetic to get
the sum. The sum is complemented to get the checksum.
 The checksum segment is sent along with the data segments.
 At the receiver’s end, all received segments are added using 1’s complement
arithmetic to get the sum. The sum is complemented.
 If the result is zero, the received data is accepted; otherwise discarded.
4. Cyclic redundancy check (CRC)
 Unlike checksum scheme, which is based on addition, CRC is based on binary
division.
 In CRC, a sequence of redundant bits, called cyclic redundancy check bits, are
appended to the end of data unit so that the resulting data unit becomes exactly
divisible by a second, predetermined binary number.
 At the destination, the incoming data unit is divided by the same number. If at this
step there is no remainder, the data unit is assumed to be correct and is therefore
accepted.
 A remainder indicates that the data unit has been damaged in transit and therefore
must be rejected.

Example:
CRC Error Detection Algorithm in Java

import java.util.*;

class CRC {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int n;

//Accept the input


System.out.println("Enter the size of the data:");
n = scan.nextInt();
int data[] = new int[n];
System.out.println("Enter the data, bit by bit:");
for(int i=0 ; i < n ; i++) {
System.out.println("Enter bit number " + (n-i) + ":");
data[i] = scan.nextInt();
}

// Accept the divisor


System.out.println("Enter the size of the divisor:");
n = scan.nextInt();
int divisor[] = new int[n];
System.out.println("Enter the divisor, bit by bit:");
for(int i=0 ; i < n ; i++) {
System.out.println("Enter bit number " + (n-i) + ":");
divisor[i] = scan.nextInt();
}

// Divide the inputted data by the inputted divisor


// Store the remainder that is returned by the method
int remainder[] = divide(data, divisor);
for(int i=0 ; i < remainder.length-1 ; i++) {
System.out.print(remainder[i]);
}
System.out.println("\nThe CRC code generated is:");

for(int i=0 ; i < data.length ; i++) {


System.out.print(data[i]);
}
for(int i=0 ; i < remainder.length-1 ; i++) {
System.out.print(remainder[i]);
}
System.out.println();

// Create a new array


// It will have the remainder generated by the above method appended
// to the inputted data
int sent_data[] = new int[data.length + remainder.length - 1];
System.out.println("Enter the data to be sent:");
for(int i=0 ; i < sent_data.length ; i++) {
System.out.println("Enter bit number " + (sent_data.length-i)
+ ":");
sent_data[i] = scan.nextInt();
}
receive(sent_data, divisor);
}

static int[] divide(int old_data[], int divisor[]) {


int remainder[] , i;
int data[] = new int[old_data.length + divisor.length];
System.arraycopy(old_data, 0, data, 0, old_data.length);
// Remainder array stores the remainder
remainder = new int[divisor.length];
// Initially, remainder's bits will be set to the data bits
System.arraycopy(data, 0, remainder, 0, divisor.length);

// Loop runs for same number of times as number of bits of data


// This loop will continuously exor the bits of the remainder and
// divisor
for(i=0 ; i < old_data.length ; i++) {
System.out.println((i+1) + ".) First data bit is : "
+ remainder[0]);
System.out.print("Remainder : ");
if(remainder[0] == 1) {
// We have to exor the remainder bits with divisor bits
for(int j=1 ; j < divisor.length ; j++) {
remainder[j-1] = exor(remainder[j],
divisor[j]);
System.out.print(remainder[j-1]);
}
}
else {
// We have to exor the remainder bits with 0
for(int j=1 ; j < divisor.length ; j++) {
remainder[j-1] = exor(remainder[j], 0);
System.out.print(remainder[j-1]);
}
}
// The last bit of the remainder will be taken from the data
// This is the 'carry' taken from the dividend after every step
// of division
remainder[divisor.length-1] = data[i+divisor.length];
System.out.println(remainder[divisor.length-1]);
}
return remainder;
}

static int exor(int a, int b) {


// This simple function returns the exor of two bits
if(a == b) {
return 0;
}
return 1;
}

static void receive(int data[], int divisor[]) {


// This is the receiver method
// It accepts the data and divisor (although the receiver already has
// the divisor value stored, with no need for the sender to resend it)
int remainder[] = divide(data, divisor);
// Division is done
for(int i=0 ; i < remainder.length ; i++) {
if(remainder[i] != 0) {
// If remainder is not zero then there is an error
System.out.println("There is an error in received
data...");
return;
}
}
//Otherwise there is no error in the received data
System.out.println("Data was received without any error.");
}
}
Output:
Enter the size of the data:
7
Enter the data, bit by bit:
Enter bit number 7:
1
Enter bit number 6:
0
Enter bit number 5:
0
Enter bit number 4:
1
Enter bit number 3:
1
Enter bit number 2:
0
Enter bit number 1:
1
Enter the size of the divisor:
4
Enter the divisor, bit by bit:
Enter bit number 4:
1
Enter bit number 3:
0
Enter bit number 2:
1
Enter bit number 1:
1
1.) First data bit is : 1
Remainder : 0101
2.) First data bit is : 0
Remainder : 1010
3.) First data bit is : 1
Remainder : 0011
4.) First data bit is : 0
Remainder : 0110
5.) First data bit is : 0
Remainder : 1100
6.) First data bit is : 1
Remainder : 1110
7.) First data bit is : 1
Remainder : 1010
101
The CRC code generated is:
1001101101
Enter the data to be sent:
Enter bit number 10:
1
Enter bit number 9:
0
Enter bit number 8:
0
Enter bit number 7:
1
Enter bit number 6:
1
Enter bit number 5:
0
Enter bit number 4:
1
Enter bit number 3:
1
Enter bit number 2:
0
Enter bit number 1:
1
1.) First data bit is : 1
Remainder : 0101
2.) First data bit is : 0
Remainder : 1010
3.) First data bit is : 1
Remainder : 0011
4.) First data bit is : 0
Remainder : 0111
5.) First data bit is : 0
Remainder : 1110
6.) First data bit is : 1
Remainder : 1011
7.) First data bit is : 1
Remainder : 0000
8.) First data bit is : 0
Remainder : 0000
9.) First data bit is : 0
Remainder : 0000
10.) First data bit is : 0
Remainder : 0000
Data was received without any error.

You might also like