The Python Oracle

Don't understand why (5 | -2) > 0 is False where (5 or -2) > 0 is True

This video explains
Don't understand why (5 | -2) > 0 is False where (5 or -2) > 0 is True

--

Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Droplet of life

--

Chapters
00:00 Question
00:51 Accepted answer (Score 44)
02:48 Answer 2 (Score 14)
03:46 Answer 3 (Score 5)
06:24 Answer 4 (Score 3)
07:44 Thank you

--

Full question
https://stackoverflow.com/questions/5522...

Accepted answer links:
[The logical or in python returns the first value that is true.]: https://docs.python.org/3/library/stdtyp...
[number where all bits are set that are set in at least one of the given numbers]: https://docs.python.org/3/library/stdtyp...
[two's complement]: https://en.wikipedia.org/wiki/Two%27s_co...
[any]: https://docs.python.org/3/library/functi...

Answer 4 links:
[bitwise]: https://en.wikipedia.org/wiki/Bitwise_op...
[two's complement]: https://en.wikipedia.org/wiki/Two%27s_co...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #conditional

#avk47



ACCEPTED ANSWER

Score 44


What is the difference between or and |?

or is a logical or and | is a bitwise or logical inclusive or.

The logical or

The logical or in python returns the first value that is true.

Example:

>>> None or False or 5
5
>>> -5 or 2
-5

The bitwise or logical inclusive or

The bitwise or logical inclusive or is represented by the | operator in python and creates a number where all bits are set that are set in at least one of the given numbers.

Example:

  • 2 is in binary 0010
  • 4 is in binary 0100

A logical or between the two results in 0110 which is 6.

>>> 2 | 4
6

How a negative number is stored is generally implementation specific. However on most systems a negative number is stored by creating the two's complement of the positive number by inverting each bit and adding 1.

That number in bitwise ore two any other number still results in a negative number:

>>> -5 | 2
-5

Neither of the two solves your problem

While using

(vals[1] or vals[0]) > 0

seems to work, it fails when you flip the values:

>>> vals = [2, -5]
>>> (vals[1] or vals[0]) > 0
False

You should check both values seperatly

>>> vals = [-5, 2]
>>> vals[0] > 0 or vals[1] > 0
True

For a larger input this may be inconvenient. You should use any with a generator expression:

>>> any(x > 0 for x in vals)
True



ANSWER 2

Score 14


You want the any function:

>>> any(x > 0 for x in vals)

x | y computes the bitwise OR of the two values, while x or y evaluates to the first "truthy" value. In both cases, the result is then compared to 0: (x or y) > 0 and (x | y) > 0.

What you want to compare each value to zero (as necessary), with

vals[0] > 0 or vals[1] > 0

If you had three values, you'd write

vals[0] > 0 or vals[1] > 0 or vals[2] > 0

The any function generalizes this to a list of any size, without the need to decide how many terms to or together based on the size of the list.




ANSWER 3

Score 3


| is a bitwise OR, and Python uses two's complement representation for integers. Evaluating 5 | -2 gives:

  ... 0000 0000 0000 0101 (+5)
| ... 1111 1111 1111 1110 (-2)
──────────────────────────────
= ... 1111 1111 1111 1111 (-1)

And -1 is not greater than zero, so (5 | -2) > 0 is false.

or is a logical OR. Unlike in other languages where this operator returns a Boolean (True/False) value, Python defines x or y as being equivalent to x if x else y (except that x is evaluated only once). Note that any nonzero numeric value is “truthy” in Python, so if x≠0, then x or y evaluates to x.

>>> 5 or -2
5
>>> -2 or 5
-2

That (5 or -2) > 0 evaluates to True was a stroke of luck from having the positive number first. In the other order, you would have gotten False.

In general (x or y) > 0 is not equivalent to (x > 0) or (y > 0), which is what you meant.




ANSWER 4

Score 2


When you do (5 | -2), you're doing a bitwise-OR. That will preserve the negation bit in the numbers. Therefore, you'll still have a negative number.

The (5 or -2) is a logical-OR, to the Python interpreter will extend that to the next logical operator (the greater-than).