diff --git a/rot.py b/rot.py index c29100e..b115148 100644 --- a/rot.py +++ b/rot.py @@ -1,13 +1,15 @@ get_bin = lambda x, n: format(x, "b").zfill(n) -rol = lambda val, r_bits, max_bits: (val << r_bits % max_bits) & (2**max_bits - 1) | ( - (val & (2**max_bits - 1)) >> (max_bits - (r_bits % max_bits)) +mask = lambda max_bits: (1 << max_bits)-1 + +rol = lambda val, r_bits, max_bits: (val << r_bits % max_bits) & ((1 << max_bits) - 1) | ( + (val & ((1 << max_bits) - 1)) >> (max_bits - (r_bits % max_bits)) ) # Rotate right: 0b1001 --> 0b1100 ror = lambda val, r_bits, max_bits: ( - (val & (2**max_bits - 1)) >> r_bits % max_bits -) | (val << (max_bits - (r_bits % max_bits)) & (2**max_bits - 1)) + (val & ((1 << max_bits) - 1)) >> r_bits % max_bits +) | (val << (max_bits - (r_bits % max_bits)) & ((1 << max_bits) - 1)) max_bits = 16 # For fun, try 2, 17 or other arbitrary (positive!) values