def convert_scan_cursor(cursor, max_cursor):
    """
    See https://engineering.q42.nl/redis-scan-cursor/

    Apply mask from max_cursor and reverse bits order
    """
    N = len(bin(max_cursor)) - 2
    value = bin(cursor)[2:]
    s = ("{:>0%ss}" % N).format(value)
    return int(s[::-1], base=2)