The Man with the bit mask error


#1


This code looks right to me but it wont pass. When I submit the code, the input fails at 0b0 yet when i test it with 0b0, i get the output of "off"

def check_bit4(input):
num = input

mask = int(0b1000)
print mask
desired = num & mask
if desired > 0:
    print "on"
elif input == "0b0":
    print "off"
else:
    print "off"

print check_bit4(0b0)


Replace this line with your code.


#2

An int is an int regardless of whether you write it in base10 or base2. Converting int to int doesn't do much.

You're also comparing to both ints and strings, and you're looking at more than one variable and two branches have the same result.
You've also got two variables refering to the same value.

Those are all very strange things to do, so maybe you just need to reconsider which exact steps your function should be performing and make sure it does nothing other than that.


#3

Let's simplify the code and force an exception:

def check_bit(num, mask = int(0b1000)):
    return num & mask > 0

print check_bit("0b0")

which raises,

Traceback (most recent call last):
  File "<pyshell#245>", line 1, in <module>
    check_bit("0b0")
  File "<pyshell#244>", line 2, in check_bit
    return num & mask > 0
TypeError: unsupported operand type(s) for &: 'str' and 'int'

The error message is telling us that we cannot AND a string and an integer. The input is a string. Just for the fun of it, let's try it with strings...

def check_bit(num, mask = "0b1000"):
    return num & mask > 0

print check_bit("0b0")

raises,

TypeError: unsupported operand type(s) for &: 'str' and 'str'

so we now know that strings are not the desired data type. We can raise an exception when this happens so that bugs in the program are easily found.

def check_bit(num, mask=int(0b1000)):
    try:
        return int(num) & mask > 0        
    except ValueError:
        print ("Something is wrong with the input value")
        raise

Tests

>>> check_bit("0b0")
Something is wrong with the input value
Traceback (most recent call last):
  File "<pyshell#262>", line 1, in <module>
    check_bit("0b0")
  File "<pyshell#261>", line 3, in check_bit
    return int(num) & mask > 0
ValueError: invalid literal for int() with base 10: '0b0'
>>> check_bit(15)
True
>>> check_bit(0b1111)
True
>>> check_bit(33, 0b1)
True

So this at least is something we can build upon.


#4

Thanks for the tips guys. I finally figured it out. my original code was simpler than what I posted here. and would have worked had i used return instead of print.


#5

I failed to post my final solution that works directly with binary numbers.

def check_bit(num, mask=0b1000):
    try:
        return num & mask > 0        
    except ValueError:
        print ("Something is wrong with the input value")
        raise

This is what @ionatan was talking about: numbers of any base are integers.


#6

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

Here is my code,The function request to accept one parameter is input,not anything you defined,something was hard to me that about input that builtin function of python, the & not works on str,but int,so like that above my solution will work for you ,if you test that


#7

Actually, my code is not meant for lesson submission, just experimentation.


#8

Got it!:smiley::smiley:


#9

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