Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Reversing shift-XORed in python
def reverse_right_shift(value, shift, mult=0xffffffff):
output, i = 0, 0
while i * shift < 32:
compartment = int(bin(0xffffffff << (32 - shift))[-32:], 2) >> (shift * i)
part_output = value & compartment
value ^= part_output >> shift & mult
output |= part_output
i += 1
return output
def reverse_left_shift(value, shift, mult=0xffffffff):
output, i = 0, 0
while i * shift < 32:
compartment = int(bin((0xffffffff >> (32- shift)) << (shift * i))[-32:], 2)
part_output = value & compartment
value ^= part_output << shift & mult
output |= part_output
i += 1
return output
value = 3009615726
_value = value ^ value >> 10
assert reverse_right_shift(_value, 10) == value
_value = value ^ value << 15 & 15141003
assert reverse_left_shift(_value, 15, 15141003) == value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment