Integer conversion with bit operators


#1

Hi Folks,

My code for “The Man behind the bit mask” was giving me an error,
it was something like: unsupported for & operand: ‘str’ & ‘int’.

This was my faulty code :slight_smile:

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

I didn’t need to convert the “input” (an integer) into binary with the bin function.

When using bit operators are integers converted to binary?
How does python compare an integer argument with a binary number?


#2

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