python - How do you print the output this [6,4,2,0] in a single row rather that sequence -


i using below manual method happens find high bits =1 of binary value given integer.

for eg: when enter 85, 6th, 4th, 2nd , 0th bit high , getting output 6 4 2 0 in sequence. need output this: [6,4,2,0]. 1 me in ?

def high_bit(num):     in range(0,100):         if (num/(pow(2,i)))==1:              print             num = (num - (pow(2,i)))             y = high_bit(num)             return y 

i find recursion bit confusing, can work:

def high_bit(num, lst=none):     if lst == none:         toplevel = true         lst = []     else:         toplevel = false     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 recommend doing iteratively, though.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -