11/14 The man behind the bit mask

Can someone give me an example of a use case for this function please? I don’t really see the point - in the example given, we know that the 3rd bit is on, we can see it.

The two cases I thought of were:

  1. If we had an integer as opposed to the binary and wanted to find out if a bit was on.
    …………… but we could just do print bin(x).

  2. If the binary string was extremely long so looking was difficult
    ……………but we would have to type out an extremely long mask that would be difficult to get right

So, what’s the point?

I suspect they just want to give you an easy(ish) example so you can actually practice and understand the concept. I believe at the beginning of the lesson they said something along the lines of probably never having to use it however having the knowledge could be helpful just in case.

Thanks python_micro, you are probably right. I think I got a bit hasty posting this as the next few exercises gave some decent use cases.

my code is simple and more importantly works !!
def check_bit4(inout):
if inout >>3 >0:
return “on”

    return "off"

print check_bit4(0b01000)

1 Like

Yes it work… if you want to check only ONE bit. But when you want to check severals bits (eg: check the first, third an fifth bits in 0b10011010), it could be tricky… With mask it will bemore easier for that example with severals checks i think…