Define a function,
check_bit4 , with one argument,
input , an integer.
It should check to see if the fourth bit from the right is on.
If the bit is on,
return "on" (not
If the bit is off,
return "off" .
Check the Hint for some examples!
Here are some examples:
check_bit4(0b1) # ==> "off" check_bit4(0b11011) # ==> "on" check_bit4(0b1010) # ==> "on"
You’ll need to use a mask where all bits are off except for the fourth bit from the right.
def check_bit4(inpt): num = inpt mask = 0b1000 desired = num & mask if desired > 0: return "on" else: return "off" check_bit4(0b1101)
def check_bit4(input): mask = 0b1000 desired = input & mask if desired > 0: return "on" else: return "off"
When you use input as argument, like the Codecademy solution, it turns white…maybe its not a good idea to do that? Maybe its better to write something else, like inpt?
And Codecademy doesnt call their function…should we call it with an integer like they ask us in the instructions, with lets say 13, or should we call it with a binary number instead, lets say 13 in binary (0b1101)?
It seems that both work when i try, so maybe it doesnt matter? Or maybe 13 in binary(ob1101) is already considered as an integer and thats why both ways work?