Skip to content

Instantly share code, notes, and snippets.

@khayyamsaleem
Created September 23, 2020 00:28
Show Gist options
  • Save khayyamsaleem/99d16b70914494a7c04cf91374985530 to your computer and use it in GitHub Desktop.
Save khayyamsaleem/99d16b70914494a7c04cf91374985530 to your computer and use it in GitHub Desktop.
def getBit(n, x):
return n&(1<<x)
def setBit(n,x,b):
return n&(~(1<<x)) if b == 0 else n|(1<<x)
def encodeChunk(word):
output = 0
for i,char in enumerate(map(ord, word)):
for c,j in enumerate(range(i,32,4)):
output = setBit(output, j, getBit(char, c))
return output
def encode(str):
n = 4
chunks = [str[i:i+n] for i in range(0, len(str), n)]
encoded = [encodeChunk(chunk) for chunk in chunks]
return encoded
if __name__ == "__main__":
print(encode("tacocat"))
print(encode("never odd or even"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment