Question Ex.8 :How to from str to binary again


#1

https://www.codecademy.com/courses/learn-python/lessons/introduction-to-bitwise-operators/exercises/a-bit-of-this-or-that?action=lesson_resume

I tried to do the little challenge the exercise 8 has, not to use the OR ‘|’ operator. I did it with lists and my problem is that, i get the correct result but in a string form. How i can do it in binary again. I will post only the first 3 and the last lines of code where my question is.

```

a=0b0101010
b=0b000101
result=a|b

print rlst               #prints ['0', 'b', '1', '0', '1', '1', '1', '1']
rrlst=''.join(rlst)

print 'printing the 0b101111 gives', 0b101111                  #prints 47
print 'printing the rrlst which is 0b101111 gives', rrlst  #prints 0b101111
print type(rrlst)            #checking what i got in the end ==> 'str'

I want rrlst to print 47 not 0b101111


#2

Hi @aliatros,

The following commented example performs the process of converting a list of characters of a binary string into an int, for output, in four steps …

# create a list of characters of a binary string
bin_list = ['0', 'b', '1', '0', '1', '1', '1', '1']
# join the characters into a string
bin_str = "".join(bin_list)
# interpret the string in base 2 to convert it to an int
num = int(bin_str, 2)
# output the result
print num

Output …

47

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.