-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.6.5.py
69 lines (55 loc) · 1.34 KB
/
3.6.5.py
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
68
69
from bitarray import bitarray
def step(array,taps,shift):
t = 0
for tap in taps:
t ^= array[tap]
for i in range(shift,0,-1):
array[i]=array[i-1]
array[0] = t
def xStep(array):
step(array,[13,16,17,18],18)
def yStep(array):
step(array,[20,21],21)
def zStep(array):
step(array,[7,20,21,22],22)
def getMajority(x,y,z):
return 1 if ((x[0] + y[0] + z[0]) > 1) else 0
def stepRegistersAsNeeded(x,y,z,maj):
if (x[0] == maj):
xStep(x)
if (y[0] == maj):
yStep(y)
if (z[0] == maj):
zStep(z)
def getBit(x,y,z):
return x[18]^y[21]^z[22]
def nextBit(x,y,z):
maj = getMajority(x,y,z)
stepRegistersAsNeeded(x,y,z,maj)
return getBit(x,y,z)
def homeworkProblem():
x = bitarray('1010101010101010101')
y = bitarray('1100110011001100110011')
z = bitarray('11100001111000011110000')
cs = bitarray(32)
for i in range (0,32):
cs[i] = nextBit(x,y,z)
print 'cs = '+str(cs)
print 'x = '+str(x)
print 'y = '+str(y)
print 'z = '+str(z)
def drawImage():
import Image, ImageDraw
xA = bitarray('1010101010101010101')
yA = bitarray('1100110011001100110011')
zA = bitarray('11100001111000011110000')
im = Image.new("1", (1024,1024))
for x in range(0,1024):
for y in range (0,1024):
im.putpixel((x,y),nextBit(xA,yA,zA))
# write to stdout
im.save("out.png", "PNG")
def main():
drawImage()
if __name__ == "__main__":
main()