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

8
  • Well what does the tutorial say about why it recommends that? Commented Aug 19, 2017 at 13:23
  • If they're recommending you bitwise operators vs the normal boolean operators in conditional expressions, then that's a problem. Commented Aug 19, 2017 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. Commented Aug 19, 2017 at 13:25
  • "for combining boolean expressions": that's conditional. Commented Aug 19, 2017 at 13:25
  • 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. Commented Aug 19, 2017 at 13:35

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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!
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.
Looks like they are using pandas quantopian.com/tutorials/getting-started#lesson5
@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).
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.