Shashank Verma Shashank Verma - 2 years ago 118
Python Question

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

I am finding difficulty performing a bitwise & operation between the

in binary form. How can I achieve this goal?

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download