The man behind bit mask. Guys shouldn't this work?


#1



https://www.codecademy.com/courses/python-intermediate-en-KE1UJ/2/1?curriculum_id=4f89dab3d788890003000096#


Folks .... any idea why I'm getting this error ?

Oops, try again. Your function fails on check_bit4(0b100). It returns "on" when it should return "off".


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


#2

@markiscoding,
Maybe you will find an explanation
https://discuss.codecademy.com/t/bit-mask/46938/2?u=leonhard.wettengmx.n


#3

For starters, do you agree with the error message?
How would you do it manually?
What does your code do differently from that? After all it's just instructions, so how do these instructions differ from what you do?


#4

I updated the code:

def check_bit4(input):
mask = 0b0100

if input == mask:
    return "on"
else:
    return "off"

check_bit4(0b0100)

I did some research in Stack Overflow about this topic but still can't find what's bugging me here ....


#5

I think I understand what I'm asked to do. Get returned ON if fourth bit is on.


#6

You avoided the first thing you need to consider, the thing that decides what you need to do next.

Do you agree with the error message?

If yes: change the code to do as you would do
If no: get a better understanding about what you're supposed to do

See how those are completely separate paths and which one you should take is determined by the answer to that question?

And then you keep asking questions like that until you have narrowed down to what is preventing you from solving it.


#7

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