-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
edced33
commit d5549be
Showing
12 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
|
||
# Returns XOR of 'a' and 'b' | ||
# (both of same length) | ||
def xor(a, b): | ||
|
||
# initialize result | ||
result = [] | ||
|
||
# Traverse all bits, if bits are | ||
# same, then XOR is 0, else 1 | ||
for i in range(1, len(b)): | ||
if a[i] == b[i]: | ||
result.append('0') | ||
else: | ||
result.append('1') | ||
|
||
return ''.join(result) | ||
|
||
|
||
# Performs Modulo-2 division | ||
def mod2div(divident, divisor): | ||
|
||
# Number of bits to be XORed at a time. | ||
pick = len(divisor) | ||
|
||
# Slicing the divident to appropriate | ||
# length for particular step | ||
tmp = divident[0 : pick] | ||
|
||
while pick < len(divident): | ||
|
||
if tmp[0] == '1': | ||
|
||
# replace the divident by the result | ||
# of XOR and pull 1 bit down | ||
tmp = xor(divisor, tmp) + divident[pick] | ||
|
||
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-0s divisor. | ||
tmp = xor('0'*pick, tmp) + divident[pick] | ||
|
||
# increment pick to move further | ||
pick += 1 | ||
|
||
# For the last n bits, we have to carry it out | ||
# normally as increased value of pick will cause | ||
# Index Out of Bounds. | ||
if tmp[0] == '1': | ||
tmp = xor(divisor, tmp) | ||
else: | ||
tmp = xor('0'*pick, tmp) | ||
|
||
checkword = tmp | ||
return checkword | ||
|
||
# Function used at the sender side to encode | ||
# data by appending remainder of modular division | ||
# at the end of data. | ||
def encodeData(data, key): | ||
|
||
l_key = len(key) | ||
|
||
# Appends n-1 zeroes at end of data | ||
appended_data = data + '0'*(l_key-1) | ||
remainder = mod2div(appended_data, key) | ||
|
||
# Append remainder in the original data | ||
codeword = data + remainder | ||
print("Remainder : ", remainder) | ||
print("Encoded Data (Data + Remainder) : ", | ||
codeword) | ||
|
||
# Driver code | ||
data = "10110010" | ||
key = "101" | ||
encodeData(data, key) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.