How Do You Print The Output This [6,4,2,0] In A Single Row Rather That Sequence
I am using the below manual method which happens to find the high bits =1 of the binary value for a given integer. For eg: when we enter 85, the 6th, 4th, 2nd and 0th bit are high
Solution 1:
I find your recursion a bit confusing, but it can work:
def high_bit(num, lst=None):
if lst == None:
toplevel = True
lst = []
else:
toplevel = False
for i in range(0,100):
if (num/(pow(2,i)))==1:
lst.append(i)
num = (num - (pow(2,i)))
high_bit(num, lst)
if toplevel:
print lst
high_bit(85) # prints [6, 4, 2, 0]
I would recommend doing it iteratively, though.
Post a Comment for "How Do You Print The Output This [6,4,2,0] In A Single Row Rather That Sequence"