def reverse_bits(num, nofBits):
    """Reverse the order of the number of bits in the number value, e.g. to perform a index bit flip for an FFT"""
    result = 0
    for i in range(nofBits):
        result = (result << 1) + (num & 1)   # flip order of bits
        num >>= 1
    return result