#technique used to detect error
#algo
#1. we have message bits and crc generator given to us
message= input("Enter message:")
crc= input("Enter crc:")
len_crc= len(crc)
#2. append (n-1) 0's where n is the no. of bits in crc generator to the message
temp= message+'0'*(len_crc-1)
#print(temp)
#3. perform xor operation(division by 2) on crc and temp
#find index in temp for 1's '1' from left
def find_index(temp):
length= len(temp)
i=0
while i<length and temp[i]=='0':
i+=1
return i
def solve(temp,crc):
#perform one time xor operation before.
#It is because when we perform xor operation first time
#then irrespective of the 1st bit(either 0 or 1) in temp we have to perform xor operation.
#After that, xor operation is performed with respect to first 1 from left.
xor(temp,0,crc)
#print(temp)
#find index in temp for first '1' from left
index= find_index(temp)
#print(index)
#length= len(temp)
while len(temp[index:])>=len_crc:
#print(index)
#we need to perform xor operation till we have 4 bits left
#if index+4<len
xor(temp,index,crc)
#find index in temp for first '1' from left
index= find_index(temp)
#print(index)
#print(temp)
#return the bits to be appended to the actual message
result= temp[index:]
#no. of zeros to add
n_zero= len_crc-1-len(result)
for i in range(n_zero): result.append('0')
return result
def xor(temp, index, crc):
i=0
j= index
while i<len_crc:
if crc[i]==temp[j]:
temp[j]= '0'
else:
temp[j]= '1'
j+=1
i+=1
#convert temp to a list
#because string is immutable
temp= [char for char in temp]
#bits to be added at the end of actual message
add_bits= ''.join(solve(temp, crc))
#message bits sent to receiver
receiver= message+add_bits
print(receiver)