Extended Theory - Parity Bits, Majority Voting, Check Digits
Data transmission is prone to errors, which can lead to incorrect or corrupted information being received. To ensure data integrity, several error detection and correction methods are employed. This document explores three such techniques: parity checking, majority voting, and check digits.
Parity Checking
In parity checking, an additional bit, known as a parity bit, is added to a string of binary data. This ensures that the total number of 1 bits in the string is either an odd or even number, depending on the chosen parity:
Even Parity: The total number of 1 bits is even.
Odd Parity: The total number of 1 bits is odd.
Parity checking can detect single-bit errors but cannot correct them.
Parity Bits
Let's use the BitArray class from the System.Collections namespace to demonstrate how to calculate the parity bit for an integer:
usingSystem;usingSystem.Collections;namespaceparity{classProgram{staticvoidMain(string[]args){Console.Write("Enter an integer (1..127) > ");intinputNumber=Convert.ToInt32(Console.ReadLine());BitArraybits=newBitArray(newint[]{inputNumber});// Convert to an array of bitsintcount=0;for(inti=0;i<bits.Length;i++){if(bits[i])count++;// Count the number of 1s}Console.WriteLine($"{count} 1s in {inputNumber}");// Check if the count is odd or even using modulus operatorif(count%2==1){bits[7]=true;// Set parity bit for even parity}// Convert back to integerint[]resultArray=newint[1];bits.CopyTo(resultArray,0);inputNumber=resultArray[0];Console.WriteLine($"With parity bit set: {inputNumber}");}}}
The BitArray class represents the bits as true (1) or false (0). We count the number of true values to determine the parity.
Majority Voting
Majority voting is another error detection method where each bit in the data is duplicated an odd number of times, such as three. This redundancy helps to correct certain errors. For example, the binary data 101 becomes 111 000 111.
If errors occur during transmission, the receiver takes the majority of each bit group to reconstruct the original data. Although not foolproof, this method can correct some errors without needing retransmission.
staticvoidMain(string[]args){// Get the character to encodeConsole.Write("Enter a character to encode: ");charch=Console.ReadLine()[0];stringbinCode=DecimalToBinary(ch);Console.WriteLine($"Encoding {ch} is: {binCode}");// Get the number of repetitions for each bitConsole.Write("Enter number of repeats for each bit: ");intrepetitions=Convert.ToInt32(Console.ReadLine());stringnewBinCode="";for(inti=0;i<binCode.Length;i++){for(intj=0;j<repetitions;j++){newBinCode+=binCode[i];}}Console.WriteLine($"The transmitted code is: {newBinCode}");// Introduce errors as a percentageConsole.Write("Enter percentage of errors to introduce: ");interrorPercent=Convert.ToInt32(Console.ReadLine());stringerrorBinCode="";Randomrnd=newRandom();for(inti=0;i<newBinCode.Length;i++){if(rnd.Next(100)<errorPercent){errorBinCode+=newBinCode[i]=='0'?'1':'0';}else{errorBinCode+=newBinCode[i];}}Console.WriteLine($"Code with errors introduced: {errorBinCode}");// Check and correct the errors using majority votingstringrepairedCode="";intn=0;while(n<errorBinCode.Length){intcount=0;for(inti=0;i<repetitions;i++){if(errorBinCode[n]=='1')count++;n++;}repairedCode+=count>repetitions/2?'1':'0';}Console.WriteLine($"Repaired code is: {repairedCode} or {Convert.ToChar(BinaryToDecimal(repairedCode))}");}
Check Digits
A check digit is a digit added to the end of data to help verify its accuracy. It is commonly used with credit card numbers, bank account numbers, and ISBNs.
For example, the ISBN-13 check digit is calculated as follows:
Multiply each digit alternately by 1 and 3.
Sum the products.
Find the remainder when the sum is divided by 10.
Subtract the remainder from 10 to get the check digit.
These error detection methods provide various ways to verify data integrity during transmission. While each has its strengths and weaknesses, they are crucial tools in ensuring accurate data communication.