0% found this document useful (0 votes)
9 views5 pages

3 Experiment 3: Cyclic Redundancy Check (CRC) : 3.1 Objective

The document outlines an experiment on implementing Cyclic Redundancy Check (CRC) for error detection in data transmission. It explains the theory behind CRC, the steps for its implementation, and provides C++ code for both sender and receiver sides. The conclusion emphasizes the effectiveness of CRC in ensuring data integrity across various communication protocols and systems.

Uploaded by

swetaku7091
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)
9 views5 pages

3 Experiment 3: Cyclic Redundancy Check (CRC) : 3.1 Objective

The document outlines an experiment on implementing Cyclic Redundancy Check (CRC) for error detection in data transmission. It explains the theory behind CRC, the steps for its implementation, and provides C++ code for both sender and receiver sides. The conclusion emphasizes the effectiveness of CRC in ensuring data integrity across various communication protocols and systems.

Uploaded by

swetaku7091
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/ 5

3 Experiment 3: Cyclic Redundancy Check

(CRC)
3.1 Objective
The objective of this lab is to understand and implement Cyclic Redundancy
Check (CRC) for error detection in data transmission. This experiment will
help you learn how CRC is computed and how it helps in detecting errors in
transmitted messages.

3.2 Theory
Cyclic Redundancy Check (CRC) is a technique used to detect errors in data
transmission. It is based on dividing the input data by a known generator
polynomial, then appending the remainder (CRC code) to the message. At
the receiving end, the same polynomial is used to check if the message is
error-free.
The generator polynomial is chosen as a divisor, and the message is di-
vided by it using modulo-2 division. The remainder of this division becomes
the CRC code, which is appended to the original data before transmission.

3.2.1 Steps for CRC Implementation


1. Input data (message) and the polynomial (generator).

2. Perform modulo-2 division to calculate the CRC checksum.

3. Append the CRC checksum to the original message for transmission.

4. On the receiver’s side, perform the same division to check for errors.

5. If the remainder is zero, the message is valid; otherwise, it indicates an


error in the transmission.

3.3 C++ Code for CRC with Receiver-Side Validation


3.3.1 Sender Side (Transmitter) Code:
The sender computes the CRC checksum and appends it to the data message
before transmission.
1 # include < iostream >
2 # include < string >
3 using namespace std ;

9
4

5 // Function to perform XOR operation between two binary


strings
6 string xorOperation ( string dividend , string divisor ) {
7 for ( int i = 0; i < divisor . length () ; i ++) {
8 dividend [ i ] = ( dividend [ i ] == divisor [ i ]) ? ’0 ’
: ’1 ’;
9 }
10 return dividend ;
11 }
12

13 // Function to perform Modulo -2 Division ( CRC


Calculation )
14 string computeCRC ( string data , string generator ) {
15 int dataLen = data . length () ;
16 int genLen = generator . length () ;
17 // Append zero bits equal to the length of the
generator minus one
18 string dividend = data + string ( genLen - 1 , ’0 ’) ;
19

20 for ( int i = 0; i <= dataLen - 1; i ++) {


21 if ( dividend [ i ] == ’1 ’) {
22 dividend . replace (i , genLen , xorOperation (
dividend . substr (i , genLen ) , generator ) ) ;
23 }
24 }
25 return dividend . substr ( dataLen , genLen - 1) ; //
Remainder ( CRC )
26 }
27

28 // Function to check for errors using CRC


29 bool checkCRC ( string received , string generator ) {
30 int dataLen = received . length () ;
31 int genLen = generator . length () ;
32

33 for ( int i = 0; i <= dataLen - genLen ; i ++) {


34 if ( received [ i ] == ’1 ’) {
35 received . replace (i , genLen , xorOperation (
received . substr (i , genLen ) , generator ) ) ;
36 }
37 }
38

39 // If remainder is all zeros , the message is error -

10
free
40 return ( received . substr ( dataLen - genLen + 1) ==
string ( genLen - 1 , ’0 ’) ) ;
41 }
42

43 int main () {
44 string message , generator ;
45

46 // Input binary message and generator polynomial


47 cout << " Enter ␣ binary ␣ message : ␣ " ;
48 cin >> message ;
49 cout << " Enter ␣ generator ␣ polynomial ␣ ( binary ␣ string ) :
␣";
50 cin >> generator ;
51

52 // Compute CRC
53 string crc = computeCRC ( message , generator ) ;
54 string encodedMessage = message + crc ;
55

56 cout << " \ nCRC ␣ Checksum : ␣ " << crc ;


57 cout << " \ nEncoded ␣ Message ␣ ( Message ␣ + ␣ CRC ) : ␣ " <<
encodedMessage << endl ;
58

59 // Simulate transmission and reception


60 cout << " \ nSimulating ␣ transmission ...\ n " ;
61 string receivedMessage = encodedMessage ;
62

63 // Check for errors in received message


64 if ( checkCRC ( receivedMessage , generator ) ) {
65 cout << " No ␣ errors ␣ detected ␣ in ␣ received ␣ message
.\ n " ;
66 } else {
67 cout << " Error ␣ detected ␣ in ␣ received ␣ message .\ n " ;
68 }
69

70 // Introduce an error ( flip a bit )


71 receivedMessage [2] = ( receivedMessage [2] == ’0 ’) ? ’
1 ’ : ’0 ’;
72 cout << " \ nReceived ␣ Message ␣ with ␣ Error : ␣ " <<
receivedMessage << endl ;
73

74 if ( checkCRC ( receivedMessage , generator ) ) {


75 cout << " No ␣ errors ␣ detected ␣ in ␣ received ␣ message

11
.\ n " ;
76 } else {
77 cout << " Error ␣ detected ␣ in ␣ received ␣ message .\ n " ;
78 }
79

80 return 0;
81 }

3.3.2 Explanation:
- The **sender side** computes the CRC checksum by performing modulo-2
division on the data and appends it to the original data for transmission. -
The **receiver side** performs the same modulo-2 division on the received
data (including the CRC checksum) and checks whether the remainder is
zero. If the remainder is zero, the data is valid; otherwise, it indicates an
error in the transmission.

3.3.3 Sample Input and Output:


**Input:**

Enter binary message: 11010011101100


Enter generator polynomial (binary string): 10011

**Output:**

CRC Checksum: 1110


Encoded Message (Message + CRC): 110100111011001110

Simulating transmission...
No errors detected in received message.

Introducing an error in transmission...


Received Message with Error: 111100111011001110
Error detected in received message.

3.4 Conclusion
This implementation demonstrates how CRC can effectively detect errors in
transmitted messages. It is widely used in communication protocols, storage
devices, and embedded systems to ensure data integrity. The error detection
process works by verifying if the received data, including the appended CRC

12
checksum, results in a remainder of zero after modulo-2 division. If the
remainder is non-zero, an error has occurred in the transmission.

13

You might also like