Advertisement
3th1ca14aX0r

Untitled

Aug 20th, 2024 (edited)
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.26 KB | Source Code | 0 0
  1. def hamming_decode_remove_first_bit(encoded_str):
  2.     # Remove the first bit
  3.     encoded_str = encoded_str[1:]
  4.    
  5.     # Convert the string to a list of integers
  6.     encoded = list(map(int, list(encoded_str)))
  7.    
  8.     # Calculate parity bits positions (1-based index)
  9.     parity_positions = [1, 2, 4, 8, 16]
  10.    
  11.     # Calculate error position (1-based index)
  12.     error_position = 0
  13.    
  14.     for p in parity_positions:
  15.         count = 0
  16.         for i in range(1, len(encoded) + 1):
  17.             if i & p == p:
  18.                 count += encoded[i - 1]
  19.         if count % 2 != 0:
  20.             error_position += p
  21.    
  22.     # If there's an error, correct it
  23.     if error_position != 0:
  24.         encoded[error_position - 1] ^= 1
  25.    
  26.     # Remove the parity bits to extract the data bits
  27.     data_bits = []
  28.     for i in range(1, len(encoded) + 1):
  29.         if i not in parity_positions:
  30.             data_bits.append(str(encoded[i - 1]))
  31.    
  32.     # Convert the binary data bits to a decimal integer
  33.     data_bits_str = ''.join(data_bits)
  34.     return int(data_bits_str, 2)
  35.  
  36. # The encoded string with the intention to remove the first bit
  37. encoded_str = '01001000000100001001110000011000'
  38. decoded_value = hamming_decode_remove_first_bit(encoded_str)
  39. decoded_value
  40.  
Tags: python
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement