0% found this document useful (0 votes)
8 views3 pages

Lab P4

jytujtyutryutrytr

Uploaded by

bgscse ise
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)
8 views3 pages

Lab P4

jytujtyutryutrytr

Uploaded by

bgscse ise
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/ 3

LAB P4: Develop a program for error detecting code using CRC-CCITT (16- bits).

Problem

You are given:

1. Message (data): 11010011101100 (binary)


2. Polynomial (divisor): 10001000000100001 (binary) — CRC-16-CCITT standard
polynomial.

The task is:

1. Compute the CRC value (remainder) for the given data.


2. Append the CRC to the data for transmission.
3. Verify the data integrity by recalculating the CRC and checking for errors.

Solution Steps

Step 1: Append Zeros


To compute the CRC, append n-1 zeros to the data, where n is the length of the divisor (polynomial).
• Divisor length: n=17
• Data with zeros: 1101001110110011010011101100 →
110100111011000000000000110100111011000000000000 (appending 16 zeros).

Step 2: Perform Binary Division


Divide the appended data by the polynomial using binary division.

Steps:

1. Align the divisor with the leftmost 1 of the dividend.


2. XOR the divisor with the dividend.
3. Shift the divisor and repeat until the entire message is processed.

Example:
Dividend (initial): 110100111011000000000000110100111011000000000000
Divisor (aligned): 1000100000010000110001000000100001
XOR operation: 010110111010000000000000010110111010000000000000
Shift divisor: Repeat until remainder is 16 bits

Final remainder after division (CRC): 0110100110010110.

Step 3: Append CRC

Append the CRC to the original data:

• Original Data: 1101001110110011010011101100


• CRC: 01101001100101100110100110010110
• Transmitted Message:
110100111011000110100110010110110100111011000110100110010110

Step 4: Verify Data Integrity


At the receiver’s end, perform the same division on the received message:

• Received Message:
110100111011000110100110010110110100111011000110100110010110
• Perform binary division by the same polynomial.
• If the remainder is 00000000, the data is error-free.

Programmatic Solution in Java


public class CRC16CCITT {
private static final int POLYNOMIAL = 0x1021; // CRC-16-CCITT Polynomial

public static void main(String[] args) {


String data = "11010011101100"; // Binary string of the message

// Convert binary string to byte array


byte[] dataBytes = binaryStringToByteArray(data);

// Compute CRC
int crc = computeCRC(dataBytes);
System.out.printf("Computed CRC: %04X%n", crc);

// Append CRC to data (simulate transmission)


byte[] transmittedData = appendCRC(dataBytes, crc);

// Verify at receiver's end


boolean isValid = verifyCRC(transmittedData);
System.out.println("Verification Result: " + (isValid ? "No Errors" : "Errors Detected"));
}

// Compute CRC-16-CCITT
public static int computeCRC(byte[] data) {
int crc = 0xFFFF; // Initial CRC value

for (byte b : data) {


crc ^= (b << 8); // XOR byte into top 8 bits of CRC
for (int i = 0; i < 8; i++) {
if ((crc & 0x8000) != 0) {
crc = (crc << 1) ^ POLYNOMIAL;
} else {
crc = (crc << 1);
}
}
}
return crc & 0xFFFF; // Return 16-bit CRC
}

// Append CRC to data


public static byte[] appendCRC(byte[] data, int crc) {
byte[] dataWithCRC = new byte[data.length + 2];
System.arraycopy(data, 0, dataWithCRC, 0, data.length);
dataWithCRC[data.length] = (byte) ((crc >> 8) & 0xFF); // High byte
dataWithCRC[data.length + 1] = (byte) (crc & 0xFF); // Low byte
return dataWithCRC;
}

// Verify data with CRC


public static boolean verifyCRC(byte[] dataWithCRC) {
return computeCRC(dataWithCRC) == 0;
}

// Convert binary string to byte array


public static byte[] binaryStringToByteArray(String binary) {
int byteCount = (binary.length() + 7) / 8;
byte[] result = new byte[byteCount];
for (int i = 0; i < binary.length(); i++) {
if (binary.charAt(i) == '1') {
result[i / 8] |= (1 << (7 - (i % 8)));
}
}
return result;
}
}

You might also like