Man behind the bit mask


#1

Hello folks! I am doing something wrong but not sure what. I keep getting "Oops, try again. Your function fails on check_bit4(0b0). It returns "on" when it should return "off".". The code works.
on
None


Replace this line with your code. 
def check_bit4(input): # the "4" means the fourth bit from the right
    num = 0b1100 
    mask =0b1010 
    desired = num & mask
    if desired>0:  #desired = 0b1000
        return "on"
    else:
        return "off"
print check_bit4(num&mask)


#2

It's me again. This is not working for me because it assesses all four bits instead of the fourth from the right. How can I make the code assess only the fourth bit from the right as required by the instruction. Thank you


#3

Does the argument match with the parameter?

Consider,

>>> num = 0b1100
>>> mask = 0b1010
>>> num & mask
8
>>> bin(num & mask)
'0b1000'
>>>

#4

I am still getting the same codecademy error


#5

Oh I get it now thank you.
. I changed my code to
def check_bit4(input): # the "4" means the fourth bit from the right
num = 0b1100
mask = 0b1000
# desired = (num & mask)
if input>=0b1000:
return "on"
else:
return "off"
print check_bit4(input)
The main change is this if input>=0b1000
and commented out # desired = (num & mask)
I am still not sure why that should make a difference since we are supposed to check the fourth bit from the right
Much appreciated


#6

AND is mathematically translated to times; and, OR to plus.

0b1 * 0b1 == 0b1

I'll leave you to figure the OR part. It's a bit more complicated.

Hint: Sum is zero equals True.


#7

I understand the operands. OR is essentialy +. I have two codes The top code which does not satisfy the instruction is accepted by codecademy whatever the input is. The second code, which i think is what is required is not accepted even though the print out gives on or off depending on the input. I am not sure why? Please try ob 1011 or 10111 as inputs or num

def check_bit4(input): # the "4" means the fourth bit from the right
num = 0b1100
mask = 0b1000
if input>=mask: #desired = 0b1000
return "on"
else:
return "off"
print input()
print check_bit4(input)

def check_bit4(input): # the "4" means #the fourth bit from the right

#mask =0b1010 
#desired = num & mask
#if desired>0:  #desired = 0b1000
    #return "on"
#else:
    #return "off"

print input()

print check_bit4(num)


#8

Those lines don't quite look right. The exercise does not call for user input, so use literals.

print check_bit4(13)    # on
print check_bit4(16)    # pff
print check_bit(8)      # on

#9

Hiya, Thank you for writing back. I have used your advice together with my normal trial and improvement to get this code which works fine. The problem was the way, or the improvement I made was my not defining number properly. .. third line from bottom.. I also changed input to input1 --- line 1. I needed to put in the print statements because it helped me to understand what the code was doing/executing. Cheers pal thank you

def check_bit4(input1): # the "4" means #the fourth bit from the right
mask = 0b1000
desired = input1 & mask
if desired > 0:
return "on"
else:
return "off"
num = input ("pleas enter a number")
print num
print check_bit4(num)

pleas enter a number 0b101011
43
on
None

pleas enter a number 0b10111
23
off


#10

Remember to use the bin() function if you want to print binaries.

print (bin(43))    # 0b101011

#12

Thank you.That's kind of you.


#13

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