Skip to content

Commit

Permalink
Update rot.py
Browse files Browse the repository at this point in the history
  • Loading branch information
daedalus committed Feb 12, 2024
1 parent 616a135 commit 3622869
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions rot.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 3622869

Please sign in to comment.