The Man Behind the Bit Mask: Need help


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

https://www.codecademy.com/courses/learn-python/lessons/introduction-to-bitwise-operators/exercises/the-man-behind-the-bit-mask-?link_content_target=interstitial_lesson

Your function fails on check_bit4(0b0). It returns “on” when it should return “off”.

Have looked at other people’s code, and I fail to see the error. What am I missing?

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

#2

Use your input value, or change input to num (which is the better option since input is a reserved word).

Extra Study

Look up Python ternary expression statements to get a handle on the syntax that follows; but, also read up truthy and falsy to get the gist of the logic used…

def bit4set(num): return 'on' if num & 0b1000 else 'off'

#3

Okay, so I’ve tried this

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

Hasn’t worked. I feel like I’m missing something obvious here. Error message is the same too:
Your function fails on check_bit4(0b0). It returns “on” when it should return “off”.


#4

Remove that line. It is overwriting the parameter value.


#5

Problem solved. Now I feel stupid.

EDIT: Might look at the “Extra Study” thing later, don’t have time right now.


#6

That is not unexpected. There is a lot of ground to cover before going down that road. It is up ahead, though, so bookmark this topic. My goal is not to interfere with the progress you are making. It’s a sneak peek of what’s to come. Hone your skills with conditionals at every opportunity and the progress will be smooth.


#7

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