0% found this document useful (0 votes)
322 views6 pages

CheckSum Method and Parity Method

Error Detection Method : CheckSum and Parity
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)
322 views6 pages

CheckSum Method and Parity Method

Error Detection Method : CheckSum and Parity
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/ 6

Lab Experiment 2 Name : Nikhil Parkar

Reg No : 23BAI1199

Date : 04.08.2024
Error Detection Methods

A – CheckSum Method

Aim : Generate and validate sent and received data using error
detection methods ; using CheckSum method

Code :
#include <iostream>
#include <string>
#include <bitset>
using namespace std;

string decimalToBinary(unsigned long decimal, int bits) {


return bitset<32>(decimal).to_string().substr(32 - bits);
}

void calculateChecksum(string num, int bytes) {


int numParts = num.length() / bytes;
unsigned long sum = 0;

for (int i = 0; i < numParts; ++i) {


string part = num.substr(i * bytes, bytes);
bitset<32> partBits(part);
unsigned long partValue = partBits.to_ulong();
sum += partValue;

if (sum > (1 << bytes) - 1) {


sum = (sum & ((1 << bytes) - 1)) + 1; }
}

unsigned long checksum = ~sum & ((1 << bytes) - 1);


string sumBinary = decimalToBinary(sum, bytes);
string checksumBinary = decimalToBinary(checksum, bytes);
cout << "Sum (binary): " << sumBinary << endl;
cout << "Checksum (1's complement, binary): " << checksumBinary << endl;

if (checksumBinary == string(bytes, '0')) {


cout << "Data is not corrupted." << endl;
}
else {
cout << "Data is corrupted." << endl; }
}

int main() {
string num;
int bytes;
cout << "Enter the binary number: ";
cin >> num;
cout << "Enter the number of bytes for checksum: ";
cin >> bytes;
calculateChecksum(num, bytes);
return 0;
}
Algorithm :
1. Read a binary number ‘num’ and number of bytes ‘bytes’
2. Initialize sum to 0
3. Divide num into segments of ‘bytes’ length each
4. Handle overflow by wrapping around, if necessary
5. Compute the CheckSum as the bitwise NOT of sum masked by ((1<<bytes)-1)
6. Output sum and the calculated CheckSum
7. Check If checksum is zero to determine if data is valid or corrupted
8. Print the final output

Output :

Result :
Thus, by using the CheckSum method we can detect the error.
B – Parity Bit Method

Aim : Generate and validate sent and received data using error
detection methods ; using Parity Bit method

Code :
#include <stdio.h>

void printBinaryAndCountOnes(int num, int *count) {


*count = 0; // Initialize count to 0
for (int i = 7; i >= 0; i--) {
int bit = (num >> i) & 1;
printf("%d", bit);
if (bit == 1) {
(*count)++;
}
}
}

int main() {
int decimalNumber;
int countOnes;

printf("Enter a decimal number (0-255): ");


scanf("%d", &decimalNumber);

if (decimalNumber < 0 || decimalNumber > 255) {


printf("Error: The number must be between 0 and 255.\n");
return 1;
}

printf("The 8-bit binary representation is: ");


printBinaryAndCountOnes(decimalNumber, &countOnes);
printf("\n");

if (countOnes % 2 == 0) {
printf("The number of ones is even. Appending 0.\n");
printf("Binary with appended 0: ");
printBinaryAndCountOnes(decimalNumber, &countOnes);
printf("0\n");
}
else {
printf("The number of ones is odd. Appending 1.\n");
printf("Binary with appended 1: ");
printBinaryAndCountOnes(decimalNumber, &countOnes);
printf("1\n");
}

return 0;
}

Algorithm :
1. Get the number as an input from the user and store it in ‘decimalNumber’
2. Check if ‘decimalNumber’ Is less than 0 or greater than 255. If true, print
“Error”. If false, proceed to the next step
3. Within ‘printBinaryAndCountOnes’ function, compute the bit and print the
computed bit and if computed bit Is 1, increment ‘*count’ by 1, where ‘*count
= 0’ was initialised.
4. Print “The 8-bit binary representation is; “ and then call the
“printBinaryAndCountOnes” again to display the binary representation and
update “countOnes”
5. If countOnes is even, print binary with “appending 0” and if countOnes is odd,
print the binary with “appending 1”
6. Now print the final output
Output :

Result :
Thus, by using the Parity Bit method we can detect the error.

You might also like