Cyclic Redundancy Check and Modulo | Returns XOR of ' a ' and ' b ' ( both of same length ) ; initialize result ; Traverse all bits , if bits are same , then XOR is 0 , else 1 ; Performs Modulo - 2 division ; Number of bits to be XORed at a time . ; Slicing the divident to appropriate length for particular step ; replace the divident by the result of XOR and pull 1 bit down ; else : If leftmost bit is '0' If the leftmost bit of the dividend ( or the part used in each step ) is 0 , the step cannot use the regular divisor ; we need to use an all - 0 s divisor . ; increment pick to move further ; For the last n bits , we have to carry it out normally as increased value of pick will cause Index Out of Bounds . ; Function used at the sender side to encode data by appending remainder of modular division at the end of data . ; Appends n - 1 zeroes at end of data ; Append remainder in the original data ; Driver code
def xor ( a , b ) : NEW_LINE INDENT result = [ ] NEW_LINE for i in range ( 1 , len ( b ) ) : NEW_LINE INDENT if a [ i ] == b [ i ] : NEW_LINE INDENT result . append ( '0' ) NEW_LINE DEDENT else : NEW_LINE INDENT result . append ( '1' ) NEW_LINE DEDENT DEDENT return ' ' . join ( result ) NEW_LINE DEDENT def mod2div ( divident , divisor ) : NEW_LINE INDENT pick = len ( divisor ) NEW_LINE tmp = divident [ 0 : pick ] NEW_LINE while pick < len ( divident ) : NEW_LINE INDENT if tmp [ 0 ] == '1' : NEW_LINE INDENT tmp = xor ( divisor , tmp ) + divident [ pick ] NEW_LINE tmp = xor ( '0' * pick , tmp ) + divident [ pick ] NEW_LINE DEDENT pick += 1 NEW_LINE DEDENT if tmp [ 0 ] == '1' : NEW_LINE INDENT tmp = xor ( divisor , tmp ) NEW_LINE DEDENT else : NEW_LINE INDENT tmp = xor ( '0' * pick , tmp ) NEW_LINE DEDENT checkword = tmp NEW_LINE return checkword NEW_LINE DEDENT def encodeData ( data , key ) : NEW_LINE INDENT l_key = len ( key ) NEW_LINE appended_data = data + '0' * ( l_key - 1 ) NEW_LINE remainder = mod2div ( appended_data , key ) NEW_LINE codeword = data + remainder NEW_LINE print ( " Remainder ▁ : ▁ " , remainder ) NEW_LINE print ( " Encoded ▁ Data ▁ ( Data ▁ + ▁ Remainder ) ▁ : ▁ " , codeword ) NEW_LINE DEDENT data = "100100" NEW_LINE key = "1101" NEW_LINE encodeData ( data , key ) NEW_LINE