Python What Is the Use of the Bitwise OR Operator in Python?

  • Thread starter Thread starter ChrisVer
  • Start date Start date
  • Tags Tags
    Operator Python
AI Thread Summary
The discussion centers on the use of the bitwise OR assignment operator (|=) in Python, particularly in the context of custom classes. The |= operator is explained as a shorthand for objectA = objectA | set([object1, object2]), which performs a bitwise OR operation. It is noted that in Python, this operator can be overloaded for specific classes, such as sets, to add elements. Participants clarify that the behavior of the |= operator must be defined in the class, and if not, it defaults to the standard bitwise operation. The conversation also touches on the efficiency of bitwise operations in algorithms, highlighting their practical applications.
ChrisVer
Science Advisor
Messages
3,372
Reaction score
465
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.
 
Technology news on Phys.org
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".
 
ChrisVer said:
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.
 
ChrisVer said:
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.
 
  • Like
Likes 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...
 
ChrisVer said:
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.
 
ChrisVer said:
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.
 
  • #10
Integrand said:
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.

Integrand said:
|= is overloaded for Python sets to add elements.
Cool, I think that's it then...It merges the sets...

Integrand said:
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.

newjerseyrunner said:
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):
 
  • #11
ChrisVer said:
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.
 
  • Like
Likes ChrisVer and jedishrfu
  • #12
ChrisVer said:
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".
 
  • #13
D H said:
__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
 

Similar threads

Replies
2
Views
1K
Replies
4
Views
2K
Replies
11
Views
2K
Replies
3
Views
1K
Replies
14
Views
5K
Replies
23
Views
2K
Replies
3
Views
1K
Replies
10
Views
2K
Back
Top