位反转运算快速实现

 

#encoding:utf-8

def odda(a):
    for k in range(1,m):
        for j in range(0,k):
            j1 = j + ip[k]
            k1 = k + ip[j]
            swap(a,j1,k2)
            j1 += m
            k1 += m
            swap(a,j1,k1)

    print a

def swap(a,j1,k1):
    print j1,k1
    t1 = a[j1]
    t2 = a[k1]
    a[j1] = t2
    a[k1] = t1
    return a
def evena(a):
    for k in range(0,m):
        for j in range(0,k):
            #00情况
            j1 = j + ip[k]
            k1 = k + ip[j]
            swap(a,j1,k1)
            #01 10情况
            j1 += m
            k1 += 2*m
            swap(a,j1,k1)
            #10 01 情况
            j1 += m
            k1 -= m
            swap(a,j1,k1)
            #11 11情况
            j1 += m
            k1 += 2*m
            swap(a,j1,k1)
            print "\n"
        #j=k的情况下在偶数交换位状态的中间01情况下需要交换
        j1 = k+m+ip[k]#ip[k]+k+m
        k1 = j1 + m #ip[k] +k +2m
        swap(a,k1,j1)
        print "********"
    print a
n = 32*2
m = 1
l = n
ip = []
a = []
for i in range(0,n):
    ip.append(0)
    a.append(i)
cnt = 0
while (m<<2 < l):
    cnt += 1
    l>>=1
    for i in range(0,m):
        ip[m+i] = ip[i] + l
    m <<= 1
print "l,m",l,m
print ip
print m<<2,l
if m<<2 == l:
    evena(a)
else:
    odda(a)

 

posted @ 2022-07-17 21:39  Aemnprsu_wx  阅读(44)  评论(0编辑  收藏  举报