# The Man behind the Bit Mask

#1

Hello everyone. I am stuck at this exercise. I do not understand this whole introduction to bitwise concept

I have tried the possible answers but it keeps giving a " Your code looks a bit off (pun super intended)–it threw a “unsupported operand type(s) for &: ‘unicode’ and ‘int’” error. Check the Hint if you need help! "

``````def check_bit4(input1):
if desired >= 0:
return "on"
else:
return "off"
num = raw_input("pleas enter a number")
print num
print check_bit4(num)
``````
def check_bit4(input1): mask = 0b1000 desired = input1 & mask if desired >= 0: return "on" else: return "off" num = raw_input("pleas enter a number") print num print check_bit4(num)

#2

In binary, `>= 0` covers all the possibilities. This will always be true.

``````if bin(input1) & mask:
return 'on'
``````

The following uses a Python ternary expression to write everything into one return statement:

``````>>> def check_bit4(a):
return 'on' if a & 0b1000 else 'off'

>>> check_bit4(13)
'on'
>>> check_bit4(0b10101)
'off'
>>>
``````

#3

Oh… Thank you. I get it now. Thanks a lot for your help

#4

Note to anyone struggling with int(s) error. The exercise states to use “input” as an argument to the check_bit4 function, but this is a special operator and cannot be used. Rename your argument anything else and it will work.

#5

This would be a problem…

``````input = raw_input('...')
``````

since it would overwrite the `input` function in the global namespace.

This however, will not be a problem,

``````def check_bit4(input):
``````

since it is a locally defined variable that has no conflicts with the global namespace.

#6

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