Shashank Verma Shashank Verma - 6 months ago 14
Python Question

How to do & operation on binary no.(0b1100) and bin(4)?

I am finding difficulty performing a bitwise & operation between the

bin()
and
int
in binary form. How can I achieve this goal?

Answer

If you are trying to perform the bitwise AND operation (using the & operator) on two integers, there is no need to convert them to strings of their binary representations.

Take this example:

x = 4 # 0b100
y = 5 # 0b101
print(x & y) # => 4, which is 0b100, because 0b100 & 0b101 = 0b100
Comments