├── Hamming_Encoder_and_Decoder.cpp ├── LICENSE └── README.md /Hamming_Encoder_and_Decoder.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | /*-------------------------------------------------------------------------*/ 8 | class Hamming{ 9 | 10 | public: 11 | string message; 12 | int codeword[100], temp[100]; 13 | int n, check; 14 | char parity; 15 | /*-------------------------------------------------------------------------*/ 16 | Hamming(){ 17 | parity = 'E'; 18 | message = ""; 19 | n = check = 0; 20 | 21 | for(int i = 0; i < 100; i++){ 22 | temp[i] = codeword[i] = 0; 23 | } 24 | } 25 | /*-------------------------------------------------------------------------*/ 26 | int findr(){ 27 | for(int i = 1 ;; i++){ 28 | if((n + i + 1) <= pow(2, i)) 29 | return i; 30 | } 31 | } 32 | }; 33 | /*-------------------------------------------------------------------------*/ 34 | /*-------------------------------------------------------------------------*/ 35 | class Transmitter : public Hamming{ 36 | 37 | public: 38 | 39 | void generate(){ 40 | do{ 41 | cout << "Enter the message to be Transmitted, in Binary (LSB to MSB) : "; 42 | cin >> message; 43 | }while(message.find_first_not_of("01") != string::npos); 44 | 45 | n = message.size(); 46 | cout << "Enter the Parity Type - Odd(O) / Even(E) : "; 47 | cin >> parity; 48 | 49 | for(unsigned int i = 0; i < message.size(); i++){ 50 | if(message[i] == '1'){ 51 | temp[i+1] = 1; 52 | } 53 | else{ 54 | temp[i+1] = 0; 55 | } 56 | } 57 | computeCode(); 58 | } 59 | /*-------------------------------------------------------------------------*/ 60 | void computeCode(){ 61 | check = findr(); 62 | cout << "\nNumber of Parity Bits : " << check << endl; 63 | cout << "Total Number of Bits to be Transmitted : " << (n+check) << endl << endl; 64 | 65 | for(int i = (n+check), j = n; i > 0; i--){ 66 | if((i & (i - 1)) != 0){ 67 | codeword[i] = temp[j--]; 68 | } 69 | else{ 70 | codeword[i] = setParity(i); 71 | } 72 | } 73 | 74 | cout << "PARITY BITS :\n"; 75 | 76 | for(int i = 0; i < check; i++){ 77 | cout << "P" << pow(2, i) << " : " << codeword[(int)pow(2, i)] << endl; 78 | } 79 | 80 | cout << "\nHamming Encoded Output : " << endl; 81 | 82 | for(int i = 1; i <= (n+check); i++){ 83 | cout << codeword[i]; 84 | } 85 | 86 | cout << endl; 87 | } 88 | /*-------------------------------------------------------------------------*/ 89 | int setParity(int x){ 90 | bool flag = true; 91 | int bit; 92 | 93 | if(x == 1){ 94 | bit = codeword[x+2]; 95 | for(int j = (x + 3); j <= (n+check); j++){ 96 | if(j % 2){ 97 | bit ^= codeword[j]; 98 | } 99 | } 100 | } 101 | 102 | else{ 103 | bit = codeword[x+1]; 104 | for(int i = x; i <= (n + check); i++){ 105 | if(flag){ 106 | if((i == x) || (i == (x+1))){ 107 | bit = codeword[x+1]; 108 | } 109 | else{ 110 | bit ^= codeword[i]; 111 | } 112 | } 113 | if(((i+1) % x) == 0){ 114 | flag = !flag; 115 | } 116 | } 117 | } 118 | 119 | if(parity == 'O' || parity == 'o'){ 120 | return (!(bit)); 121 | } 122 | else{ 123 | return (bit); 124 | } 125 | } 126 | }; 127 | /*-------------------------------------------------------------------------*/ 128 | /*-------------------------------------------------------------------------*/ 129 | class Receiver : public Hamming{ 130 | 131 | public: 132 | 133 | void correct(){ 134 | do{ 135 | cout << "Enter the Received Encoded Data (LSB to MSB) : "; 136 | cin >> message; 137 | }while(message.find_first_not_of("01") != string::npos); 138 | 139 | n = message.size(); 140 | cout << "Enter the Parity Type - Odd(O) / Even(E) : "; 141 | cin >> parity; 142 | 143 | for(unsigned int i = 0; i < message.size(); i++){ 144 | if(message[i] == '1'){ 145 | codeword[i+1] = 1; 146 | } 147 | else{ 148 | codeword[i+1] = 0; 149 | } 150 | } 151 | detect(); 152 | } 153 | /*-------------------------------------------------------------------------*/ 154 | void detect(){ 155 | int position = 0; 156 | check = findr(); 157 | 158 | for(int i = 0; i < (n-check); i++){ 159 | bool flag = true; 160 | int x = pow(2, i); 161 | int bit = codeword[x]; 162 | 163 | if(x == 1){ 164 | for(int j = (x+1); j <= (n+check); j++){ 165 | if(j % 2){ 166 | bit ^= codeword[j]; 167 | } 168 | } 169 | } 170 | 171 | else{ 172 | for(int k = (x+1); k <= (n+check); k++){ 173 | if(flag){ 174 | bit ^= codeword[k]; 175 | } 176 | if((k+1) % x == 0){ 177 | flag = !flag; 178 | } 179 | } 180 | } 181 | 182 | if((parity == 'E' || parity == 'e') && (bit == 1)){ 183 | position += x; 184 | } 185 | 186 | if((parity == 'O' || parity == 'o') && (bit == 0)){ 187 | position += x; 188 | } 189 | } 190 | 191 | cout << "\nPARITY BITS :\n"; 192 | 193 | for(int i = 0; i < check; i++){ 194 | cout << "P" << pow(2,i) << " : " << codeword[(int)pow(2,i)] << endl; 195 | } 196 | 197 | cout << "\nReceived Data : " << endl; 198 | 199 | for(int i = 1; i <= (n); i++){ 200 | cout << codeword[i]; 201 | } 202 | 203 | cout << endl; 204 | 205 | if(position != 0){ 206 | cout << "\nError Detected in the Transmitted Data at the Position : " << position << endl; 207 | codeword[position] = !codeword[position]; 208 | 209 | cout << "\nReceived Data Sequence After Correction : " << endl; 210 | for(int i = 1; i <= (n); i++){ 211 | cout << codeword[i]; 212 | } 213 | 214 | cout << endl; 215 | } 216 | 217 | else{ 218 | cout << "\nNo Error in the Transmitted Data." << endl; 219 | } 220 | 221 | cout << "\nHamming Decoded Output : "; 222 | for(int i = 1; i <= (n); i++){ 223 | if((i & (i - 1)) != 0){ 224 | cout << codeword[i]; 225 | } 226 | } 227 | cout << endl; 228 | } 229 | }; 230 | /*-------------------------------------------------------------------------*/ 231 | /*-------------------------------------------------------------------------*/ 232 | int main(){ 233 | 234 | string choice; 235 | 236 | Transmitter t; 237 | Receiver r; 238 | 239 | cout << "HAMMING ERROR DETECTION AND CORRECTION CODE\n\n"; 240 | 241 | sleep(1.5); 242 | 243 | cout << "Hamming Code is a Linear Error Detection and Correction Code used to Detect and Correct Bit Errors that can occur when data is Transmitted or Received."; 244 | 245 | sleep(1.5); 246 | 247 | cout << "\n\nIt can Detect upto Two simultaneous Bit Errors and is capable of Accurately Correcting Single Bit Errors."; 248 | 249 | sleep(1.5); 250 | 251 | cout << "\n\nThe Transmitter generates Parity Bits, which are added to Data, so that the validity of the Data can be checked when it is received.\n\n"; 252 | 253 | sleep(1.5); 254 | 255 | cout << "\nAre you the Transmitter or the Receiver ? "; 256 | cin >> choice; 257 | 258 | if ((choice == "transmitter")||(choice == "Transmitter")||(choice == "t")||(choice == "T")){ 259 | cout << endl; 260 | t.generate(); 261 | } 262 | 263 | if ((choice == "receiver")||(choice == "Receiver")||(choice == "r")||(choice == "R")){ 264 | cout << endl; 265 | r.correct(); 266 | } 267 | 268 | return 0; 269 | } 270 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2021, Deepthi Tabitha Bennet 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | 3. Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![License: BSD-3-Clause](https://img.shields.io/badge/License-BSD_3--Clause-blue.svg?style=for-the-badge)](https://opensource.org/licenses/BSD-3-Clause) 2 | 3 | 4 | 5 | ## Project1_HammingCode - Hamming Encoder and Decoder 6 | 7 | ![image](https://user-images.githubusercontent.com/71541429/128601631-85bc3b18-0cd5-4f91-b5bc-c656f359e9bb.jpeg) 8 | 9 | ## Demo : [Click Here!](https://drive.google.com/file/d/1GQSKhRZV8VZxYJol_QrJWVjDOM40bBzO/view?usp=drivesdk) 10 | 11 | ## Features of the Code : 12 | * Encodes Binary Data into Hamming Code, using a suitable number of Parity Bits in appropriate positions. 13 | * Variable-length Data Input Sequences can be Encoded successfully. 14 | * The Individual Parity Bits generated by the Encoder are also displayed. 15 | * The code also displays the length of the Encoded Data (Data Bits + Parity Bits) 16 | * Data Extraction from Hamming Encoded Sequences is also performed by this code. 17 | * Variable-length Transmitted Sequences can be Decoded. 18 | * The Received Sequence is checked for Errors. 19 | * If no Errors have occurred in transmission, the Decoded Binary Sequence is displayed. 20 | * Single Bit Errors in Received Data are Detected (if present), the Error Bit is located and displayed. 21 | * Received Data Sequences with Single Bit Errors are Corrected and the correct Binary Data is displayed. 22 | * Supports both Odd and Even Parity Encoding / Decoding. 23 | 24 | ### Please leave a 🌟 before you fork / clone this repo! 😃 25 | --------------------------------------------------------------------------------