def gray2bin(val):
    """  convert a binary Gray code number to reflected binary number.
    :param val: value to convert (gray code)
    :return: binary value """
    bits = 64
    tmp = val
    max_val = 0
    for i in range(bits):
        max_val |= 1 << i
    try:
        assert tmp <= max_val
    except AssertionError:
        print('nb should be {}bits max'.format(bits))
        return
    bits >>= 1
    while bits > 0:
        tmp ^= tmp >> bits
        bits >>= 1
    return tmp
