Skip to content

Instantly share code, notes, and snippets.

@zst123
Created July 12, 2017 15:21
Show Gist options
  • Save zst123/9416da510f3b6dc014e00deafbc83a0f to your computer and use it in GitHub Desktop.
Save zst123/9416da510f3b6dc014e00deafbc83a0f to your computer and use it in GitHub Desktop.
Python script for 3 way XOR (aka. one and only one)
#!/usr/bin/env python
import binascii
import sys
# PARITY_MODE = True # for odd number detection
PARITY_MODE = False # for 1 and only 1
def xor(numb_list):
final = ""
numb_list = list(map((lambda x: int(x)), numb_list))
if not all(i >= 0 for i in numb_list):
print("Negative numbers are not supported")
return
while sum(numb_list) > 0:
lsb = list(map((lambda x: x & 0x01), numb_list))
if sum(lsb) is 1:
final += "1"
elif (sum(lsb) % 2 is 1) and PARITY_MODE:
final += "1"
else:
final += "0"
numb_list = list(map((lambda x: x >> 1), numb_list))
return final
def reverse(i):
return i[::-1]
arguments = list(sys.argv)
arguments.pop(0)
out = reverse(xor(arguments))
print int(out, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment