-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrack_5_3.py
More file actions
67 lines (64 loc) · 1.06 KB
/
crack_5_3.py
File metadata and controls
67 lines (64 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def count_one(x):
con = 0
for c in range(32):
if x & 1:
con += 1
x >>= 1
return con
def nextAndPreview(x):
y = x
count_x = count_y = count_one(x)
while True:
x += 1
if count_one(x) == count_x:
print x
break
while True:
y -= 1
if count_one(y) == count_y:
print y
break
#smart method
def smart_nextAndPreview(x):
maxs = mins = x
max_move = 0
while True:
if maxs & 1 == 0:
maxs >>= 1
max_move += 1
elif maxs & 2 == 2:
maxs >>= 1
max_move += 1
elif maxs & 2 == 0:
maxs >>= 1
maxs |= 1
max_move += 1
maxs <<= max_move
break
for c in range(count_one(x)-count_one(maxs)):
temp = 1
temp <<= c
maxs |= temp
bit = 0
while bit < 32 and (mins&1)!=0:
mins >>= 1
bit += 1
while bit < 32 and (mins&1)!=1:
mins >>= 1
bit += 1
mins <<= bit
if bit == 32: mins = 0
temp = count_one(x) - count_one(mins)
x >>= bit
print temp
while temp > 0:
mins = (mins<<1) | 1
temp -= 1
bit -= 1
print bit
while bit >0:
mins <<= 1
bit -= 1
print maxs, ~(mins)
if __name__ == '__main__':
smart_nextAndPreview(1023)