CheckSum Method and Parity Method
CheckSum Method and Parity Method
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;
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>
int main() {
int decimalNumber;
int countOnes;
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.