The man behind the mask - wrong code is recognized as right


#1



The man behind the mask


I solved this exercise in the wrong way but it was recognized as right by code academy. I now figured out the right way to solve this by introducing a mask, but can someone explain why my original solution (see below) was recognized as right?


I also haven't really figured out how exactly bit masks work or what the point is in using them, so this might help to understand :slight_smile:


Replace this line with your code. 

def check_bit4(input):
    if input >= 8:
        return 'on'
    else:
        return 'off'


#2

I'm assuming that's because Codecademy checks your code by plugging in certain values into your function.

For instance, for a function:
def adding(x):
x + 1
Codecademy tests the function by plugging in 2, -8, and 0 (for example) and checks to see the function returns 3, -7, and 1.

Your solution technically does identify whether or not the fourth bit is on, just through a different process than expected, so it was marked correct because it passed Codecademy's "test."

As for what bit masks do and how to use them, they can come in handy in a variety of situations such as when we want to turn off certain bits. If I have 0b111000111, I can use the mask 0b111111111 with XOR to turn on the middle three bits. XOR outputs a 1 when inputs don't match, OR outputs a 1 when one or more inputs are 1, and AND outputs a 1 if both outputs are 1.


#3

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