0
我看到这段代码here,我想知道下面的代码是如何工作的。我理解逻辑,但只是想知道如何使用好位逻辑的实现。它是如何得到相同的前缀和后缀?
我修改了代码,使其为4位。我想知道如何检测普通前缀,即在下面的情况5中?第12行和第13行的数学推理是什么?
1 import random
2 #suppose the range of number is 8 bits
3 #and we are working with 4bits prefix
4 #and 4bits suffix
5 data = []
6 bits = 4
7 for i in range(100000):
8 data.append(random.randint(0, 100))
9 suffix = 5
10 for i in data:
11 i = int(i)
12 s = i^(suffix<<bits)
13 if s < (1 << bits):
14 print("s is ", s, "prefix of", i, "is", s, "and suffix is ", suffix)
只是想知道你是如何得到这么快?我花了很多时间弄清楚了。一旦你回答了这个问题,我会标记它是正确的。哦,现在我明白我正在使用^作为不是异或者的力量。 –
@nomanpouigt是的,你链接的页面有点令人困惑,因为解释使用^来表示权力,但代码使用它来表示异或。这是一个常见的混淆之处,因为它在不同的语言中有不同的用法。 – m69