I'm not sure, especially, what the last line does. I saw it in a Python book.
from random import randint
random_bits = 0
for i in range(64):
if randint(0, 1):
random_bits |= 1 << i
I'm not sure, especially, what the last line does. I saw it in a Python book.
from random import randint
random_bits = 0
for i in range(64):
if randint(0, 1):
random_bits |= 1 << i
You have here 2 special operators:
x << y
which shift the binary representation of x
by y
places x |= y
which do binaric or between x
and y
and store the result in x
With this knowledge you can see that your code produce a random 64 bits number. On each iteration it turn on the i'th
bit with probability of 50%.
From the docs :
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
I think the rest should be trivial from the naming of variables