什么是“while(1<<n)<self.top公司:“卑鄙?

2024-09-30 04:33:49 发布

您现在位置:Python中文网/ 问答频道 /正文

Scapy在代码中看到了易失性.py. 在while条件下,(1 << n)的含义是什么?在

def __init__(self, inf, sup, seed=None, forever=1, renewkeys=0):
    self.forever = forever
    self.renewkeys = renewkeys
    self.inf = inf
    self.rnd = random.Random(seed)
    self.sbox_size = 256

    self.top = sup-inf+1

    n=0
    while (1<<n) < self.top:
        n += 1
    self.n =n

    self.fs = min(3,(n+1)/2)
    self.fsmask = 2**self.fs-1
    self.rounds = max(self.n,3)
    self.turns = 0
    self.i = 0

Tags: 代码pyselftopdef条件fsscapy
1条回答
网友
1楼 · 发布于 2024-09-30 04:33:49

<;<;是位运算符,如here所述:

x << y Returns x with the bits shifted to the left by y places (and new bits on the right-hand-side are zeros). This is the same as multiplying x by 2**y.


因此,在您提供的代码中,while循环一直到2的最大幂,它小于self.top,如果我们假设self.top公司是100,小于100的2的最大幂是64,如果是200,它会一直到128

In [17]: n = 0
In [18]: top = 100
In [19]: while ( 1 << n ) < top:
    ...:     print ( 1<<n)
    ...:     n+=1
    ...:     
1
2
4
8
16
32
64

相关问题 更多 >

    热门问题