Bitwise OR operator in Python

  • Python
  • Thread starter ChrisVer
  • Start date
  • #1
ChrisVer
Gold Member
3,331
438

Main Question or Discussion Point

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.
 

Answers and Replies

  • #2
11,500
5,045
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
 
  • #3
ChrisVer
Gold Member
3,331
438
what happened at [in 3]:
Python:
>>>x|y
3
? neither x nor y have "3".
 
  • #4
11,500
5,045
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.
 
  • #5
33,279
4,986
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.
 
  • #6
ChrisVer
Gold Member
3,331
438
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...
 
  • #7
66
24
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.
 
  • #8
1,516
617
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.
 
  • #9
66
24
Bitmasks are not relevant here, the last line of his code makes it clear that ClassA behaves like set.
 
  • #10
ChrisVer
Gold Member
3,331
438
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.

|= is overloaded for Python sets to add elements.
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):
 
  • #11
MarneMath
Education Advisor
549
198
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.
 
  • #12
D H
Staff Emeritus
Science Advisor
Insights Author
15,393
683
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
66
24

Related Threads on Bitwise OR operator in Python

  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
13
Views
841
Replies
1
Views
3K
Replies
7
Views
884
Replies
14
Views
3K
Replies
2
Views
3K
Replies
12
Views
5K
Replies
2
Views
328
  • Last Post
Replies
5
Views
1K
  • Last Post
Replies
20
Views
32K
Top