def bit_perm(x, p, n):
    """
    Find the permutation of an index.

    Parameters
    ----------
    x: ndarray<int>, int
        a vector of indices
    p: ndarray<int>
        permutation vector, ex: bit-reversal is (0,1,...,n-1)
    n: int
        number of bits per index in ``x``

    Returns
    ----------
    ndarray<int>, int
        permuted indices

    """

    result = 0
    for i in range(n):
        b = (x >> p[i]) & 1
        result ^= (-b ^ result) & (1 << (n - i - 1))
    return result