-2

I'm reading through a tutorial for a Python based trading platform, and it is recommending the use of bitwise (~, &, |) rather than boolean (not, and, or) operators for combining boolean expressions.

Like factors, filters can be combined. Combining filters is done using the & (and) and | (or) operators.

Is there a good reason for this? I've never seen it before. I am not entirely sure about all the properties of booleans in Python but I do believe that in C and C++ booleans are represented by an integer 1 or 0 and can be operated on as such. Is Python similar? I can see how an & and an | at least could work in that case.

What could be the purpose for using these bitwise operators instead of boolean? Is it faster?

ayhan
  • 57,258
  • 12
  • 148
  • 173
Luke
  • 2,214
  • 6
  • 33
  • 58
  • Well what does the tutorial say about why it recommends that? – Stefan Pochmann Aug 19 '17 at 13:23
  • If they're recommending you bitwise operators vs the normal boolean operators in _conditional expressions_, then that's a problem. – Christian Dean Aug 19 '17 at 13:24
  • It doesn't. It actually implies that it thinks that they are the boolean operators. `Like factors, filters can be combined. Combining filters is done using the & (and) and | (or) operators.` – Luke Aug 19 '17 at 13:25
  • "for combining boolean expressions": that's conditional. – Jean-François Fabre Aug 19 '17 at 13:25
  • @Luke That quote doesn't even talk about the "boolean operators". What are their factors and filter? Maybe they're like numpy arrays and that's why they say to use the "bitwise operators". – Stefan Pochmann Aug 19 '17 at 13:28
  • @StefanPochmann The filters are boolean expressions as far as I can tell. Defined using `>`, `==`, ` – Luke Aug 19 '17 at 13:29
  • @Luke Well then that really sounds like you *must* use the "bitwise" ones. Because then the "boolean" ones won't work at all. – Stefan Pochmann Aug 19 '17 at 13:31
  • 1
    @Luke Python expressions do not have a type so there's no such a thing like "boolean expressions". Most likely things like "factors" and "filters" are not boolean, but we cannot tell for sure without more context. Expressions using `>`, etc. not always evaluate to a boolean. – Stop harming Monica Aug 19 '17 at 13:35

1 Answers1

5

Bitwise operators are usually the very wrong tool for the job. Bitwise operators are not faster (they have to do more work actually), and have a different precedence, so are bound to different parts of a larger expression compared to boolean operators.

However, in some specific frameworks, bitwise operators replace boolean operators because they can be hooked into. There are __or__ and __and__ specal methods that let you control the return value for | and &, but there are no such hooks for the boolean operators (as the latter short-circuit; evaluating both expressions to pass to a hook would defeat that).

So if the article is talking about numpy (or numpy-derived frameworks such as Pandas or Scipy), or an ORM framework like SQLAlchemy, Peewee or Django, then there is a good reason to use bitwise operators.

Martijn Pieters
  • 889,049
  • 245
  • 3,507
  • 2,997
  • I believe they have their own framework which is tightly integrated into the platform. You have to use an online IDE to develop with it, so I suspect this is the answer. I was unaware of those hooks. Thanks! I will accept your answer when I am able to! – Luke Aug 19 '17 at 13:28
  • good answer, for a very vague question. I closed as duplicate, maybe your answer could be merged into the original, because noone tackles the problem like you did. – Jean-François Fabre Aug 19 '17 at 13:33
  • Looks like they are using pandas https://www.quantopian.com/tutorials/getting-started#lesson5 – ayhan Aug 19 '17 at 13:34
  • @ayhan You mean https://www.quantopian.com/tutorials/pipeline#lesson6 – Stefan Pochmann Aug 19 '17 at 13:40
  • @StefanPochmann Yes that's the page OP quoted from but I wanted to link the data object (and its relation to a pandas Series or a DataFrame). – ayhan Aug 19 '17 at 13:43
  • @ayhan Ah, ok. But it wasn't clear what the page you linked to has to do with the OP. It just looked like you for no good reason showed a random page on the web that talks about pandas. – Stefan Pochmann Aug 19 '17 at 13:47
  • @StefanPochmann You are right. I've edited the question to include the link you mentioned. – ayhan Aug 19 '17 at 13:52