Hamming Code: Error Detection and Finding Position of Changed Bit
Hamming Code: Error Detection and Finding Position of Changed Bit
AP19110010545
CSE - G
Objective : To detect the error bit in the sent message, the method employs parity bits and
redundant bits in the data, as well as even or odd parity. To determine the error position, it
checks for parity and transforms parity bits to decimal.
Problem Statement:
Write a program to implement the hamming code technique to detect errors in transmitted
messages.
Algorthim :
1. Take input of the data to be transmitted.
2. Find length of the data entered and pass it as parameter to the RedundantBits function.
3. The number of parity bits in the coded message is returned by the RedundantBits function.
Save the result in a variable called r.
4. The RedundantBits function accepts the variables r and data as parameters. The data is
structured in this function so that the final string contains 0 at parity points and data bits at other
positions. It returns the string res as a result.
5. Arrange the returned string in a variable called arr. The variables arr and r should be passed to
the ParityBits method.
6. We calculate the parity bits in the ParityBits function depending on the even or odd parity of
that particular place. The 0 in that parity location is subsequently replaced with 1 or 0 depending
on the parity. The coded message to be conveyed is returned in the last string.
7. Print the returning value by storing it in a variable called arr. 8. Take the input from a
transmitted message and store it in the msg variable. The variables msg and r should be passed to
the DetectError function as parameters.
9. It concatenates all the parity bits to an empty string variable res in the DetectError function.
The decimal value of the binary string saved in res is then returned by the function.
10. In a variable called correction, save the decimal value that was returned. If the correction
value is 0, no error will be printed; otherwise, the correction value will be returned. The point
where the error occurred is recorded in the corrective variable.
Code :
# Python program to dmeonstrate
# hamming code
def calcRedundantBits(m):
for i in range(m):
if(2**i >= m + i + 1):
return i
# String Concatenation
# (0 to n - 2^r) + parity bit + (n - 2^r + 1 to n)
arr = arr[:n-(2**i)] + str(val) + arr[n-(2**i)+1:]
return arr
# Data to be transferred
print("Data transferred is " + arr)
arr = '11101001110'
print("Error Data is " + arr)
correction = detectError(arr, r)
print("The position of error is " + str(correction))
Input 1
Enter data to be transmitted: 1101
Enter data transmitted: 1100110
Output 1
Data transferred is 1100110
Error Data is 1100110
No error
Input 2
Enter data to be transmitted: 1011001
Enter data transmitted: 11101001110
Output 2
Data transferred is 10101001110
Error Data is 11101001110
The position of error is 10