0% found this document useful (0 votes)
97 views

Hamming Code: Error Detection and Finding Position of Changed Bit

This document describes the Hamming code technique for error detection in transmitted messages. It explains how parity bits and redundant bits are used to detect errors and find the position of any changed bits. The algorithm takes input data, calculates redundant bits, determines parity bits, transmits the coded message, detects errors by comparing parity bits, and identifies the position of any errors. Code examples are provided to demonstrate implementing Hamming code for error detection and correction.

Uploaded by

dfdfed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views

Hamming Code: Error Detection and Finding Position of Changed Bit

This document describes the Hamming code technique for error detection in transmitted messages. It explains how parity bits and redundant bits are used to detect errors and find the position of any changed bits. The algorithm takes input data, calculates redundant bits, determines parity bits, transmits the coded message, detects errors by comparing parity bits, and identifies the position of any errors. Code examples are provided to demonstrate implementing Hamming code for error detection and correction.

Uploaded by

dfdfed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Kishwar Parveen

AP19110010545

CSE - G

Hamming Code : Error Detection and Finding Position of Changed Bit

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):

# Use the formula 2 ^ r >= m + r + 1


# to calculate the no of redundant bits.
# Iterate over 0 .. m and return the value
# that satisfies the equation

for i in range(m):
if(2**i >= m + i + 1):
return i

def posRedundantBits(data, r):

# Redundancy bits are placed at the positions


# which correspond to the power of 2.
j=0
k=1
m = len(data)
res = ''

# If position is power of 2 then insert '0'


# Else append the data
for i in range(1, m + r+1):
if(i == 2**j):
res = res + '0'
j += 1
else:
res = res + data[-1 * k]
k += 1

# The result is reversed since positions are


# counted backwards. (m + r+1 ... 1)
return res[::-1]

def calcParityBits(arr, r):


n = len(arr)

# For finding rth parity bit, iterate over


# 0 to r - 1
for i in range(r):
val = 0
for j in range(1, n + 1):
# If position has 1 in ith significant
# position then Bitwise OR the array value
# to find parity bit value.
if(j & (2**i) == (2**i)):
val = val ^ int(arr[-1 * j])
# -1 * j is given since array is reversed

# 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

def detectError(arr, nr):


n = len(arr)
res = 0

# Calculate parity bits again


for i in range(nr):
val = 0
for j in range(1, n + 1):
if(j & (2**i) == (2**i)):
val = val ^ int(arr[-1 * j])

# Create a binary no by appending


# parity bits together.
res = res + val*(10**i)

# Convert binary to decimal


return int(str(res), 2)

# Enter the data to be transmitted


data = '1011001'

# Calculate the no of Redundant Bits Required


m = len(data)
r = calcRedundantBits(m)

# Determine the positions of Redundant Bits


arr = posRedundantBits(data, r)

# Determine the parity bits


arr = calcParityBits(arr, r)

# Data to be transferred
print("Data transferred is " + arr)

# Stimulate error in transmission by changing


# a bit value.
# 10101001110 -> 11101001110, error in 10th position.

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

You might also like