Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Python Bitwise OR operator in Python

  1. May 24, 2016 #1

    ChrisVer

    User Avatar
    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)
    Code (Python):

    objectA = ClassA(arguments)
    toy_obj1 = Class(args)
    toy_obj2 = Class(args)
    objectA |= set([object1, object2]) #??!??!?!
     
    Thanks.
     
  2. jcsd
  3. May 24, 2016 #2

    jedishrfu

    Staff: Mentor

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

    Code (Python):

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

    running an intereactive session in python yields:
    >>> x=1
    1
    >>> y=2
    2
    >>> x|y
    3
    >>> x
    1
    >>> x|=y
    >>> x
    3
     
     
  4. May 24, 2016 #3

    ChrisVer

    User Avatar
    Gold Member

    what happened at [in 3]:
    Code (Python):

    >>>x|y
    3
     
    ? neither x nor y have "3".
     
  5. May 24, 2016 #4

    jedishrfu

    Staff: Mentor

    Its a bitwise ORing operation. I should have used bitwise and not binary in my earlier post.
     
  6. May 24, 2016 #5

    Mark44

    Staff: Mentor

    For context ...
    Code (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.
     
  7. May 24, 2016 #6

    ChrisVer

    User Avatar
    Gold Member

    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...
     
  8. May 24, 2016 #7
    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.
     
  9. May 24, 2016 #8
    The behavior of it must be defined in that class. Bitwise operators are usually used with bitmasks, I would assume classA represents one.
     
  10. May 24, 2016 #9
    Bitmasks are not relevant here, the last line of his code makes it clear that ClassA behaves like set.
     
  11. May 24, 2016 #10

    ChrisVer

    User Avatar
    Gold Member

    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...

    It does behave like a set.

    Unfortunately it's not... If it was then in the class I was supposed to find such a "function":
    Code (Python):

    def __ior__(self,a):
     
     
  12. May 24, 2016 #11

    MarneMath

    User Avatar
    Education Advisor

    Not directly related to the current discussion, but I wanted to introduce to you some ideas why bit operations are neat.

    Code (Text):

    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.
     
  13. May 25, 2016 #12

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    __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".
     
  14. May 25, 2016 #13
    https://docs.python.org/3.5/library/operator.html
     
  15. May 25, 2016 #14

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Last edited by a moderator: Apr 27, 2017
  16. May 25, 2016 #15

    Mark44

    Staff: Mentor

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted