How to convert a int n into binary and test each bit of the resulting binary number?
I have just got the following after a lot of googling:
def check_bit_positions(n, p1, p2):
print int(str(n),2)
However i get an error invalid literal for int() with base 2. Let me know how can i get binary form of the input number and test each bit at position p1 and p2
EDIT:
binary = '{0:b}'.format(n)
if list(binary)[p1] == list(binary)[p2]:
print "true"
else:
print "false"
The above code works now, however how can i check for postions p1 and p2 from the end of the list?
l[-1]is the last element ofl,l[-2]is the second to last etc..binary[-p1] == binary[-p2]. And There's no need to convert a string to list, a string is iterable in python.binary[p1] == binary[p2]without list-izing it.