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

In summary: The algorithm returns z = 3 because 3 is the only number that when divided by 2 leaves the remainder 1.
  • #1
ChrisVer
Gold Member
3,378
464
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
  • #2
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
what happened at [in 3]:
Python:
>>>x|y
3
? neither x nor y have "3".
 
  • #4
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.
 
  • #5
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
  • #6
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
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.
 
  • #8
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.
 
  • #9
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
 

What does the Bitwise OR operator do in Python?

The Bitwise OR operator in Python is used to perform a bitwise OR operation on two integers. This operator compares the binary representation of each integer and returns a new integer with a 1 in any position where at least one of the original integers had a 1.

How is the Bitwise OR operator represented in Python?

The Bitwise OR operator is represented by the symbol "|". It is placed between the two integers that are being compared.

What is the result of using the Bitwise OR operator on two integers?

The result of using the Bitwise OR operator on two integers is a new integer that represents the binary OR operation of the two original integers. The resulting integer will have a 1 in any position where at least one of the original integers had a 1.

Can the Bitwise OR operator be used on non-integer values in Python?

No, the Bitwise OR operator can only be used on integer values in Python. Attempting to use this operator on non-integer values will result in a TypeError.

What are some practical applications of the Bitwise OR operator in Python?

The Bitwise OR operator can be used for tasks such as setting or unsetting specific bits in a binary number, checking for the presence of certain flags or permissions, and performing efficient bitwise operations on large sets of data.

Similar threads

  • Programming and Computer Science
Replies
25
Views
3K
  • Programming and Computer Science
Replies
3
Views
776
  • Programming and Computer Science
Replies
2
Views
771
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
14
Views
4K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
18
Views
2K
  • Programming and Computer Science
Replies
11
Views
2K
Back
Top