FAQ: Learn Python - Introduction to Bitwise Operators - The Man Behind the Bit Mask

This community-built FAQ covers the “The Man Behind the Bit Mask” exercise in Codecademy’s lessons on Python.

FAQs for the Codecademy Python exercise The Man Behind the Bit Mask:

Join the Discussion. We Want to Hear From You!

Have a new question or can answer someone else’s? Reply (reply) to an existing thread!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources about Python in general? Go here!

Want to take the conversation in a totally different direction? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account, billing, Pro, or Pro Intensive? Reach out to our support team!

None of the above? Find out where to ask other questions here!

Not seeing your question? It may still have been asked before – try (search) in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post (reply).

Other FAQs

The following are links to additional questions that our community has asked about this exercise:

  • This list will contain other frequently asked questions that aren’t quite as popular as the ones above.
  • Currently there have not been enough questions asked and answered about this exercise to populate this FAQ section.
  • This FAQ is built and maintained by you, the Codecademy community – help yourself and other learners like you by [contributing](https://discuss.codecadem
    y.com/t/how-to-contribute-to-t
    FAQ: Learn Python - Introduction to Bitwise Operators - The Man Behind the Bit Maskhe-faq-guide/266984)!

The instructions indicate that the argument is an int. Logically, I applied bin(input)–why does the following not work?

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

1 Like

Yes, I did the same. Instruction said the input would be an integer, so I coded it to translate that to binary.
Your code (and mine) doesn’t/didn’t work because the input is actually already in binary.

1 Like

I wrote the same exact code as you @chiquitaleslita , and I couldn’t figure out why it was wrong to use bin(input) either.

After some research, I believe it’s wrong because bin() needs to take an integer as an argument otherwise it’ll throw an error. Therefore, if the editor ran something non-integer through it then it would throw that error. The “&” operator can take integer or bin, so it always works

I did the same. I created a variable which stores the binary value of the input but didn’t work.
How is the input being converted into binary for comparision?

With the comments of everyone and my own research I came to realize that to take an input from a user, which will be a string, it will be very difficult to convert that input to bits and perform operations on it.
Because, bin(int()) will return a string, so how do we perform operations on the result?
How do we make the returned string possible for us to perform bitwise operations on it?

def check_bit4(input):

mask = 0b1000

desired = input & mask

if desired >0:

return "on"

else:

return "off"

print check_bit4(0b1000)

why does print not work if I use that instead of return inside the if statement?

The instructions request that those strings are returned from the value (it even asks you not to use print). It’d be marked as wrong just because those are two different things.

yes, but running to code for fun i cant get it to print when i use print instead return.