1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
#include <iostream> #include <vector> #include <bitset> // Function to calculate parity bits std::bitset<7> encodeHamming(const std::bitset<4>& data) { std::bitset<7> encoded; // Set data bits encoded[2] = data[0]; encoded[4] = data[1]; encoded[5] = data[2]; encoded[6] = data[3]; // Calculate parity bits encoded[0] = encoded[2] ^ encoded[4] ^ encoded[6]; // p1 encoded[1] = encoded[2] ^ encoded[5] ^ encoded[6]; // p2 encoded[3] = encoded[4] ^ encoded[5] ^ encoded[6]; // p3 return encoded; } // Function to decode and correct errors std::bitset<4> decodeHamming(const std::bitset<7>& encoded) { std::bitset<4> data; // Calculate parity bits bool p1 = encoded[0]; bool p2 = encoded[1]; bool p3 = encoded[3]; bool d1 = encoded[2]; bool d2 = encoded[4]; bool d3 = encoded[5]; bool d4 = encoded[6]; // Compute syndromes bool s1 = p1 ^ (d1 ^ d2 ^ d4); bool s2 = p2 ^ (d1 ^ d3 ^ d4); bool s3 = p3 ^ (d2 ^ d3 ^ d4); // Calculate error position int errorPos = s1 | (s2 << 1) | (s3 << 2); // Correct error if present if (errorPos) { std::cout << "Error detected at position: " << errorPos << "\n"; if (errorPos <= 7) { encoded.flip(errorPos - 1); // Correct the error } } // Extract data bits data[0] = encoded[2]; data[1] = encoded[4]; data[2] = encoded[5]; data[3] = encoded[6]; return data; } int main() { std::bitset<4> data; std::cout << "Enter 4-bit data (e.g., 1010): "; std::cin >> data; // Encode the data std::bitset<7> encoded = encodeHamming(data); std::cout << "Encoded data: " << encoded << "\n"; // Simulate transmission (Introduce a single-bit error for demonstration) std::bitset<7> received = encoded; int errorBit; std::cout << "Introduce a single-bit error (position 1-7, or 0 for no error): "; std::cin >> errorBit; if (errorBit > 0 && errorBit <= 7) { received.flip(errorBit - 1); std::cout << "Received data with error: " << received << "\n"; } // Decode and correct the received data std::bitset<4> decoded = decodeHamming(received); std::cout << "Decoded data: " << decoded << "\n"; return 0; } |
Explanation
- Function
encodeHamming
:- Purpose: Encodes 4-bit data using the Hamming(7,4) code.
- Process:
- Sets the data bits in their positions.
- Calculates the parity bits (
p1
,p2
,p3
) and sets them accordingly. - Returns the encoded 7-bit data.
- Function
decodeHamming
:- Purpose: Decodes the 7-bit Hamming code and detects/corrects errors.
- Process:
- Calculates parity bits from the received data.
- Computes the syndromes to identify error positions.
- Corrects the error if detected.
- Extracts and returns the 4-bit data from the corrected 7-bit code.
- Main Function:
- Setup: Prompts the user to input a 4-bit data value.
- Encoding: Encodes the data using the
encodeHamming
function. - Simulate Error: Allows the user to introduce a single-bit error for demonstration purposes.
- Decoding and Correction: Decodes the received data, corrects any errors, and displays the decoded data.
Usage
- Error Detection and Correction: Demonstrates a basic error correction mechanism using the Hamming(7,4) code.
- User Interaction: Allows users to input data, simulate errors, and observe the correction process.