Each thread should call a function similar to this:
c = a % 2**b
if c >= 2**(b-1):
c = 1
else:
c = 0
_dict[b] = c
where
a is the decimal representation of your original number and
b is the position of the digit in the binary representation.
EDIT: Here's a working Python code that does that. The program will read the decimal number from command line argument (
sys.argv[1]), and launches 8 threads. The number 8 can be calculated dynamically, but it's not really the goal of the exercise. Maybe I'll look into it some other time.
#!/usr/bin/python
import threading
import sys
class foo(threading.Thread):
def run(self, idx):
c = sys.argv[1] % 2**idx
if c >= 2**(idx-1):
c = 1
else:
c = 0
print "{}\t: {}".format(idx,c)
for f in range(1,9):
w = foo()
w.run(f)
Try to understand the code, don't hesitate to ask any question. And my advice to you would be to try to reimplement this in C.
NB: Note for Python devs, if you're using CPython (most common python implementation), be aware that the
global interpreter lock will prevent effective concurrent execution of the threads. This code won't really execute in a concurrent way, so it will be difficult to provide real timing datas. There are ways around this limitation, but it's not the purpose of this exercise either.