# Bitwise OR operator in Python

• ChrisVer
The algorithm returns z = 3 because 3 is the only number that when divided by 2 leaves the remainder 1.f

#### ChrisVer

Gold Member
With an example, can someone explain me what the following code would do?
In fact I am not certain I understand how the |= operator is used. (I have checked for operator overloading within the classes but there is none, so it supposedly does the default operation)
Python:
objectA = ClassA(arguments)
toy_obj1 = Class(args)
toy_obj2 = Class(args)
objectA |= set([object1, object2]) #??!?

Thanks.

The |= is a binary OR assignment operator which is equivalent to:

Python:
objectA = objectA | set([object1, object2])

Python:
running an intereactive session in python yields:
>>> x=1
1
>>> y=2
2
>>> x|y
3
>>> x
1
>>> x|=y
>>> x
3

what happened at [in 3]:
Python:
>>>x|y
3
? neither x nor y have "3".

what happened at [in 3]:
Python:
>>>x|y
3
? neither x nor y have "3".

Its a bitwise ORing operation. I should have used bitwise and not binary in my earlier post.

what happened at [in 3]:
Python:
>>>x|y
3
? neither x nor y have "3".
For context ...
Python:
x = 1
y = 2
x|y
The last line is evaluating the bitwise OR of x with y. The bit representation of x is ...00012, and the bit representation of y is ...00102. The bitwise OR of these two expressions is ...000112, or 3.

• ChrisVer
Well that's new... thanks.
Though is there any particular reason to use such an operation?
In fact I found it in some piece of code I'm working on, and it doesn't have integers, but "objects"... so I am not quiet sure how to use it...

Well that's new... thanks.
Though is there any particular reason to use such an operation?
In fact I found it in some piece of code I'm working on, and it doesn't have integers, but "objects"... so I am not quiet sure how to use it...

You need to show the code. I suspect your OP includes an incorrectly rewritten summary of it that has led other posters astray. |= is overloaded for Python sets to add elements.

Well that's new... thanks.
Though is there any particular reason to use such an operation?
In fact I found it in some piece of code I'm working on, and it doesn't have integers, but "objects"... so I am not quiet sure how to use it...
The behavior of it must be defined in that class. Bitwise operators are usually used with bitmasks, I would assume classA represents one.

Bitmasks are not relevant here, the last line of his code makes it clear that ClassA behaves like set.

I suspect your OP includes an incorrectly rewritten summary of it that has led other posters astray

This is wrong. The only thing I've changed from the code are the names of the classes and the arguments. I tried rewriting the code exactly with what I have, but I ended up writing the same quote.

Cool, I think that's it then...It merges the sets...

Bitmasks are not relevant here, the last line of his code makes it clear that ClassA behaves like set.
It does behave like a set.

The behavior of it must be defined in that class.
Unfortunately it's not... If it was then in the class I was supposed to find such a "function":
Python:
def __ior__(self,a):

Well that's new... thanks.
Though is there any particular reason to use such an operation?
In fact I found it in some piece of code I'm working on, and it doesn't have integers, but "objects"... so I am not quiet sure how to use it...
Not directly related to the current discussion, but I wanted to introduce to you some ideas why bit operations are neat.

Code:
def russian_math(a,b):
x = a; y = b; z = 0
while x>0:
if x%2 == 1: z = z + y
y = y << 1
x = x >> 1
return z

If you haven't seen the operation << or >> in python before that essentially means shift the binary to the right or left by x spaces. So why would anyone want to do this? Let's find out.

Consider the problem russian_math(12,5). Let's trace through the algorithm to see what happens:

x = 12
y = 5
z = 0

Now Since 12 > 0 we check 12 % 2. That is 0, so we z = 0
The binary form of 5 is 0101, so we move that over to the left by 1 and get 1010 (10)
The binary form of 12 is 1100 so move that over to the right we get 0110 (6)

Now since 6 > 0 we check 6 %2 which is still 0 so z is 0
Moving 1010 to the left we get 0001 0100 which is 20
Moving 0110 to the right we get 0011 which is 3

Now Since 3 > 0 we check 3 % 2 which is 1 so now z is 0 + 20 = 20
y becomes 40
x becomes 1

Now Since 1 >0 we check 1%2 which is 1 so 40 + 20 = 60
y becomes 80
x becomes 0

Since 0 is not greater than 0 we return
60.

Anyway, this ends up being a rather efficient way to multiple large numbers. If I remember correctly it grows in the order of log.

• ChrisVer and jedishrfu
TUnfortunately it's not... If it was then in the class I was supposed to find such a "function":
Python:
def __ior__(self,a):
__ior__ overloads ||, the boolean inclusive or operator. This thread is asking about bitwise inclusive or, denoted by |, and the overload for that is __or__. Note the presence or lack thereof of the letter "i".

__ior__ overloads ||, the boolean inclusive or operator. This thread is asking about bitwise inclusive or, denoted by |, and the overload for that is __or__. Note the presence or lack thereof of the letter "i".

https://docs.python.org/3.5/library/operator.html

Last edited by a moderator:
Same thing, but from the 3.4.2 docs:
operator.ior(a, b)
operator.__ior__(a, b)

a = ior(a, b) is equivalent to a |= b.