11. The Man Behind the Bit Mask - test cases not coming out as I would expect


#1

Hi,

I have completed the code on the below exercise and received a 'Way to go!' correct message:

But...
I decided to test it out to make sure I understood it and one of my examples returned "off" in the console even though the fourth bit from the right was 1. Is anyone able to explain this? Does the binary string have to be of equal length to the 'mask' and if so why?

Here is my entire code with the example:

def check_bit4(input):
mask = 0b1000
desired = input & mask
if desired > 0:
return "on"
else:
return "off"

print check_bit4(11111111111)

Obviously, all of the bits are '1' but this still returned as "off".

Actually this is even the case for:

print check_bit4(1111)

It returns "off" so seems not related to string length.

print check_bit4(1010), returns "off" too but print check_bit4(1000) returns "on"

Confused! Any help appreciated!
Alex


#2

You need the 0b prefix for those numbers to be in binary notation. Try ...

print check_bit4(0b1111111111)
print check_bit4(0b1111)
print check_bit4(0b1010)
print check_bit4(0b1000)

This, which is not in binary notation, will also have the fourth bit from the right on ...

print check_bit4(8)

It is the same as ...

print check_bit4(0b1000)

#3

Ah yes, that works and makes sense. Thanks!


#4

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