11/14 - The Man Behind the Bit Mask


#1
def check_bit4(i):
    m = 0b1000
    if i&m>0:
        return "on"
    else:
        return "off"

So I know this works. I originally had

def check_bit4(i):
    i = bin(i)
    m = 0b1000
    if i&m>0:
        return "on"
    else:
        return "off"

Can someone explain why this didn't work? The error I got was

Does your check_bit4 function take exactly one argument (an integer)? Your code threw a "unsupported operand type(s) for &: 'str' and 'int'" error. As per the instructions, I was presuming I would get an integer as an input and therefore would have to convert the int to a bin

Thanks


#2

The thing about input is - you should never presume what the input is, therefore type checks should be made (just to be sure). In this case (as you've probably guessed), the & operator can't work on 'str' and 'int'.

If you check out the definition of the function bin in the python documentation, the result of that function is always binary string. So you input int in bin and as a result you get string.
Hopefully this explained it!


#3

Ah ok - that does clear it up.

Thanks!


#4

maybe my question is very basic, cause I'm new to this bitwise world...can somebody explain to me about this "bits are on or off" I don't really get it..and we use it for what ?...duh, getting hard now..i guessed..:cry:


#5

A particular bit is "on" when it's 1 and it's "off" when it's 0. Think of a regular light switch - it can be in one of two states - ON or OFF. 1 or 0. On a very low-level every computer operates with 0's and 1's to transfer and compute data. So essentially, everything (strings of texts and numbers for example) can be (and is!) translated into 0's and 1's. The bitwise operators allow you to "operate" on variables (strings of texts and numbers for example) via manipulation of their bits.


#6

Here is what worked for me:

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

Important point is to not forget about the mask where you pass input to it and then use the & operator to get a matched binary number for checking. I don't understand why it can't do this based on one entry but will figure it out eventually... for now, Im accepting the idiom.


#7

Update, solved! (I hope it's correct ) :smile: I won't delete it, maybe it helps someone.

def check_bit4(i):
i = int(i)
mask=0b1000
desired = int(bin(i),2)&int(bin(mask),2)
if desired > 0:
return "on"
else:
return "off"


Hi all,
I am struggling with this example and have already found a solution, that results with "correct" but I'm pretty sure it's incorrect.
def check_bit4(i):
i=int(i)
mask=0b1000
desired = i&mask #I think most of already posted codes compare 16&0b1000 instead of 0b1000
if desired > 0:
return "on"
else:
return "off"

for check_bit4(16) it comes up with off :frowning:
I have also tried out several other solutions,
for example
def check_bit4(i):
i=int(i,2) # or int(str(i),2)
mask=0b1000
desired = i&mask
if desired > 0:
return "on"
else:
return "off"

I've also tried bit(i), but the global variablebit is not defined

could someone give me a hint? thanks in advance!


#8

thanks @shadowmar ..that's very kind of you....thank you so much...::relaxed:


#9

By setting mask = input, aren't you guaranteeing a match? Input is always equal to input?


#10

Not exactly. Its desired = match & input. As I understand it, this exercise is to see if the binary is on (1) or off (0). In base 2, matching two binaries against themselves will produce a greater then zero outcome that can confirm a 1 or 0 via the code match in the binary strings:

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

#11

Thanks for the response, sir.
I was specifically asking about your line 2 in which you seem to assign the variable 'mask' to input, not the bitwise operator in line 4. Further, is there a reason to give your 'input' a value of 0b1000. Shouldn't input be just that, input from the elsewhere, not the author?

Why not just assign the mask to 0b1000 and leave the input to be given later.

My code follows. Thank you again for your time and patience.

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

#12

Commenting because I see quite a few people declaring the input as a binary, e.g.: input = 0b1000

@shadowmar is correct that type-checking is a good practice, but in this educational example they state:

Define a function, check_bit4, with one argument, input, an integer.

And because bitwise operators will work equally well on a decimal integer, there's no need for this exercise to go through hoops converting the input to a different base. This will work just fine:

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

The >0 could be omitted and still work.


#13

This corny joke cracked me up so I had to share lol. This is also the answer to your question!!!
The only fix this code needs is a : when defining the check_bit4 function!!!

Hope I could help!


#14

ahhhh i get what i need to but the problem is i don't whether it's "on" or "off"

or it is just to reminds me when my bit is ok or not ,,,,
wahhh i'm really confuse to this on and off, but i do get how to do it...
can anybody pleaseeeee explain :sweat:.......
thanks


#15

My code works too:
def check_bit4 (input):
if input >= 8:
return 'on'
else:
return 'off'
print check_bit4 (input)

But i don't understand how and why... Can anybody help me to explain it?


#16

The fourth bit on which mean its values is 1. So the input would be like this:
1000, 1001,1010... and so on.
1000 = 2^3 + 0 + 0+ 0 = 8.
I hope this help!


#17

'''
def check_bit4(i):
a=str(i)
b=list(a)
c=b[::-1]
if c[3]==1:
return "on"
else:
return "off"

if i want to do this different way, is this correct?


#18

This works for me:

def check_bit4(integer):
    num = 0b1000
    my_number = integer
    desired = num & my_number
    if desired > 0:
        return 'on'
    else:
        return 'off'

#19

See the binary value for 8 is "1000". So if the button is on, that means means the fourth bit from right is 1.

If the bit value is greater or equal to 8, it means the 4th bit from right is 1("on").
I hope this helped.


#20

sir, this is not the right way to do it.

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