├── README.md ├── crc.py └── hammingCode.py /README.md: -------------------------------------------------------------------------------- 1 | # Error-Detecting-Codes -------------------------------------------------------------------------------- /crc.py: -------------------------------------------------------------------------------- 1 | def crc_remainder(input_bitstring, polynomial_bitstring): 2 | print("Input String: " + str(input_bitstring)) 3 | print("Polynom: " + str(polynom)) 4 | 5 | print("Starting Algorithm!") 6 | print("Bitstring:") 7 | print(' '.join(list(input_bitstring))) 8 | print("The first step is to add padding, we will add " + str(len(polynomial_bitstring)-1) + " bits of padding") 9 | polynomial_bitstring = polynomial_bitstring.lstrip('0') 10 | len_input = len(input_bitstring) 11 | initial_padding = "0" * (len(polynomial_bitstring) - 1) 12 | input_padded_array = list(input_bitstring + initial_padding) 13 | print("Added padding, now starting algorithm!") 14 | print(' '.join(input_padded_array)) 15 | while '1' in input_padded_array[:len_input]: 16 | cur_shift = input_padded_array.index('1') 17 | print(' '*2*cur_shift+' '.join(list(polynomial_bitstring))) 18 | for i in range(len(polynomial_bitstring)): 19 | input_padded_array[cur_shift + i] = str(int(polynomial_bitstring[i] != input_padded_array[cur_shift + i])) 20 | print(' '.join(input_padded_array)) 21 | 22 | return ''.join(input_padded_array)[len_input:] 23 | 24 | 25 | def crc_check(input_bitstring, polynomial_bitstring, check_value): 26 | print("Input String: " + str(input_bitstring)) 27 | print("Polynom: " + str(polynom)) 28 | 29 | print("Starting Algorithm!") 30 | print("Bitstring:") 31 | print(' '.join(list(input_bitstring))) 32 | print("The first step is to add the CRC as padding") 33 | 34 | polynomial_bitstring = polynomial_bitstring.lstrip('0') 35 | len_input = len(input_bitstring) 36 | initial_padding = check_value 37 | input_padded_array = list(input_bitstring + initial_padding) 38 | print("Added padding, now starting algorithm!") 39 | print(' '.join(input_padded_array)) 40 | while '1' in input_padded_array[:len_input]: 41 | cur_shift = input_padded_array.index('1') 42 | print(' '*2*cur_shift+' '.join(list(polynomial_bitstring))) 43 | for i in range(len(polynomial_bitstring)): 44 | input_padded_array[cur_shift + i] = str(int(polynomial_bitstring[i] != input_padded_array[cur_shift + i])) 45 | print(' '.join(input_padded_array)) 46 | 47 | return ('1' not in ''.join(input_padded_array)[len_input:]) 48 | 49 | string_input = '11010011101100' 50 | polynom = '1011' 51 | print("Solver of CRC from Computer Networks exam:") 52 | print("Choose an option:") 53 | print("0 for computing CRC") 54 | print("1 for checking CRC") 55 | option = input() 56 | if option == '0': 57 | print("Type the input bitstring:") 58 | string_input = input() 59 | print("Type the polynom: ") 60 | polynom = input() 61 | crc = crc_remainder(string_input, polynom) 62 | print("The CRC IS:") 63 | print(crc) 64 | elif option == '1': 65 | print("Type the input bitstring:") 66 | string_input = input() 67 | print("Type the polynom: ") 68 | polynom = input() 69 | print("Type the CRC: ") 70 | crc = input() 71 | check = crc_check(string_input, polynom, crc) 72 | print(check) 73 | else: 74 | print("Wrong option") 75 | -------------------------------------------------------------------------------- /hammingCode.py: -------------------------------------------------------------------------------- 1 | def generate_array(bitstring): 2 | arr=[] 3 | arr.append('inf') 4 | arr.append('inf') 5 | p2 = 4 6 | i = 3 7 | for item in bitstring: 8 | if i != p2: 9 | arr.append(item) 10 | i += 1 11 | continue 12 | if i == p2: 13 | arr.append('inf') 14 | arr.append(item) 15 | p2 *= 2 16 | i += 2 17 | continue 18 | return arr 19 | 20 | 21 | 22 | def hamming_code(bitarr): 23 | print(' '.join(bitarr)) 24 | print("Starting algorithm: ") 25 | print(" ") 26 | print(" ") 27 | for i in range(len(bitarr)): 28 | if bitarr[i] == 'inf': 29 | positions = [] 30 | print("We update the party bit from the index " + str(i+1)) 31 | bitarr[i] = 0 32 | k = i+1 33 | sm = 0 34 | # making parity 35 | j = i-1 36 | while j < len(bitarr): 37 | j += 1 38 | for l in range(j, j+k): 39 | try: 40 | sm += int(bitarr[l]) 41 | positions.append(l) 42 | except: 43 | break 44 | j = j+2*k-1 45 | print("This bit will contain the parity of the sum of bits on indices: " + str(positions)) 46 | if sm % 2 == 0: 47 | bitarr[i] = '0' 48 | print("The sum is even!") 49 | else: 50 | bitarr[i] = '1' 51 | print("The sum is odd!") 52 | print("The updated bitstring: ") 53 | print(" ".join(bitarr)) 54 | print(" ") 55 | 56 | return bitarr 57 | 58 | def fix_error(hamming): 59 | print(" ") 60 | print(" ") 61 | print("Starting algorithm: ") 62 | bitarr = [] 63 | p2 = 4 64 | bitarr.append('inf') 65 | bitarr.append('inf') 66 | for i in range(2, len(hamming)): 67 | if i == p2-1: 68 | bitarr.append('inf') 69 | p2 *= 2 70 | else: 71 | bitarr.append(hamming[i]) 72 | failed_checks = [] 73 | 74 | for i in range(len(bitarr)): 75 | if bitarr[i] == 'inf': 76 | positions = [] 77 | print("We compute the party bit from the index " + str(i+1)) 78 | bitarr[i] = 0 79 | k = i + 1 80 | sm = 0 81 | # making parity 82 | j = i - 1 83 | while j < len(bitarr): 84 | j += 1 85 | for l in range(j, j + k): 86 | try: 87 | sm += int(bitarr[l]) 88 | positions.append(l) 89 | except: 90 | break 91 | j = j + 2 * k - 1 92 | print("This bit will contain the parity of the sum of bits on indices: " + str(positions)) 93 | print("The computed bit is: " + str(sm % 2)) 94 | print("The actual bit is : " + str(hamming[i])) 95 | if sm % 2 == 0: 96 | if hamming[i] != '0': 97 | failed_checks.append(int(i+1)) 98 | print("The check has failed, appending to failed checks list.") 99 | else: 100 | if hamming[i] != '1': 101 | print("The check has failed, appending to failed checks list.") 102 | failed_checks.append(int(i+1)) 103 | print("Failed checks list: ") 104 | print(str(failed_checks)) 105 | print(" ") 106 | 107 | if len(failed_checks) == 0: 108 | print(" ") 109 | print("We encountered 0 failed checks.") 110 | print("The hamming code is correct!") 111 | return hamming 112 | wrong_bit = sum(failed_checks) 113 | print(" ") 114 | print("Computing the wrong bit (sum of failed checks bits).") 115 | print("wrong bit: " + str(wrong_bit)) 116 | wrong_bit -= 1 117 | print("Fixing the wrong bit.") 118 | if hamming[wrong_bit] == '1': 119 | hamming[wrong_bit] = '0' 120 | else: 121 | hamming[wrong_bit] = '1' 122 | return hamming 123 | 124 | def calculate_hamming(test_string): 125 | print("The initial bitstring is: ") 126 | print(" ".join(list(test_string))) 127 | print("The first step is to add the parity bits.") 128 | bitarr = generate_array(test_string) 129 | print("The bitstring with the parity bits added is:") 130 | print(" ".join(bitarr)) 131 | print("Now we update the inf with the partity.") 132 | hamming = hamming_code(bitarr.copy()) 133 | print(" ") 134 | print(" ") 135 | print("Hamming code: ") 136 | print(" ".join(hamming)) 137 | return hamming 138 | 139 | # 10011010 140 | print("Solver of CRC from Computer Networks exam:") 141 | print("Choose Option:") 142 | print("0 for computing hamming code for a given bitstring") 143 | option = input() 144 | if option == '0': 145 | print("Type the bitstring: ") 146 | bitstring = input() 147 | hamming = calculate_hamming(bitstring) 148 | 149 | print(" ") 150 | print(" ") 151 | print("Choose an option:") 152 | print("0 for altering a bit of the hamming code and checking + fixing") 153 | print("1 for checking the hamming code") 154 | noption = input() 155 | if noption == '0': 156 | print("Type the index of the bit you want to switch: (starting from 1)") 157 | ind = int(input()) 158 | ind -= 1 159 | if hamming[ind] == '0': 160 | hamming[ind] = '1' 161 | else: 162 | hamming[ind] = '0' 163 | print("Altered hamming code: ") 164 | print(" ".join(hamming)) 165 | 166 | print("Checking and fixing the hamming code:") 167 | new_hamming = fix_error(hamming) 168 | print("The fixed hamming code is: ") 169 | print(" ".join(new_hamming)) 170 | elif noption == '1': 171 | print("Checking the hamming code: ") 172 | new_hamming = fix_error(hamming) 173 | print("The hamming code is: ") 174 | print(" ".join(new_hamming)) 175 | else: 176 | print("Wrong option") 177 | else: 178 | print("Wrong option") 179 | --------------------------------------------------------------------------------