11. The Man Behind the Bit Mask


#1



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


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


I don't know what's wrong.


def check_bit4(input):
    mask = 0b01000
    if mask > 0:
        return "on"
    else:
        return "off"


#2

Instruction says---

A bit mask can help you turn specific bits on, turn others off, or just collect data from an integer about which bits are on or off.

Here we have to check 4th bit.
so we need 4 - bit bitmask value--
mask = 0b1000

Now how to mask using & operator?
You have and (&) operator,the number and mask number right?

Hint**

I hope it helps! :slight_smile:
do reply if it solved the problem or not!


#3

Thank you soo much!!! It has solved my problem :grin:

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

#4

Great!!!! :slight_smile:
Keep up the good work :thumbsup:


#5

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