## Short-Circuit: Why Use their Bitwise Counterparts?

New to FreeBASIC? Post your questions here.
datwill310
Posts: 355
Joined: May 29, 2015 20:37

### Short-Circuit: Why Use their Bitwise Counterparts?

My main preference when coding is to use the bitwise operators (such as OR, AND).

However I am also aware of the short-circuit "equivalents" (i.e. ORELSE, ANDALSO), and after some reflection, I'm not too sure why the bitwise operators would be used in their LOGICAL uses (i.e. if x > 5 and y < 8), except that they can overloaded and short-circuit operators can't. The main point to my argument is that short-circuit operations can be faster (not both operands have to be calculated, just the first one before the program/computer can determine whether to continue calculating).

Is this assumption about speed correct? Is there something about short-circuit operators which may cause issues with some logical statements? Or should we all just be using AND and OR only for the few logical statements that need it for some reason, and really we should be using ANDALSO and ORELSE?
MrSwiss
Posts: 3786
Joined: Jun 02, 2013 9:27
Location: Switzerland

### Re: Short-Circuit: Why Use their Bitwise Counterparts?

Personal preference is: short-cut operators (speed, boolean comparison).

However, can also be done without (the binary operators) but, with more code to get close, e.g.:

Code: Select all

`If (cond1) Then    If (cond2) Then        If (cond3) Then            If (cond4) Then                ...            End If        End If    End IfEnd If`
With the short-cut operators:

Code: Select all

`If (cond1) AndAlso (cond2) Then    If (cond3) AndAlso (cond4) Then        ...    End IfEnd If`
Last edited by MrSwiss on Jul 03, 2017 14:30, edited 1 time in total.
datwill310
Posts: 355
Joined: May 29, 2015 20:37

### Re: Short-Circuit: Why Use their Bitwise Counterparts?

MrSwiss wrote:Personal preference is: short-cut operators (speed).

However, can also be done without (the binary operators) but, with more code to get close, e.g.:

Code: Select all

`If (cond1) Then    If (cond2) Then        If (cond3) Then            If (cond4) Then                ...            End If        End If    End IfEnd If`
With the short-cut operators:

Code: Select all

`If (cond1) AndAlso (cond2) Then    If (cond3) AndAlso (cond4) Then        ...    End IfEnd If`

Thanks for the clarification.
fxm
Posts: 10264
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

### Re: Short-Circuit: Why Use their Bitwise Counterparts?

Have you seen the already existing feature request: #246 Execution speed optimization of 'Andalso' and 'Orelse' ?
You can add text inside 'discussion' (need to login)!
The included test program provides the performances for 3 syntaxes used in a particular case.

In any case, I always find that the nested conditional branchings are the fastest.
datwill310
Posts: 355
Joined: May 29, 2015 20:37

### Re: Short-Circuit: Why Use their Bitwise Counterparts?

fxm wrote:Have you seen the already existing feature request: #246 Execution speed optimization of 'Andalso' and 'Orelse' ?
You can add text inside 'discussion' (need to login)!
The included test program provides the performances for 3 syntaxes used in a particular case.

In any case, I always find that the nested conditional branchings are the fastest.

Thanks for the info and will see which is faster!
TeeEmCee
Posts: 321
Joined: Jul 22, 2006 0:54
Location: Auckland

### Re: Short-Circuit: Why Use their Bitwise Counterparts?

Oh, I'm glad to see that dkl optimised andalso and orelse, back in 2015. I've been avoiding those for years, due to the shocking assembly that was generated for them. They aren't much slower than and/or anymore. And these days I use -gen gcc for speed, so no longer have any reason to avoid them.
xlucas
Posts: 302
Joined: May 09, 2014 21:19
Location: Argentina

### Re: Short-Circuit: Why Use their Bitwise Counterparts?

Oh! I hadn't seen there had been discussion about the speed of these operators. It's very interesting.
Now, aside from speed, I believe there are good reasons to sometimes use And/Or and sometimes AndAlso/OrElse. It is better, in my opinion, to use the latter when one knows that the expression on the right has a chance of evaluating to an error and this is acceptable. That's very clear. On the other hand, it's important to use the former whenever one believes none of the two expressions should ever evaluate to an error, so that if that assumption were wrong, one can detect it more easily. In general, I'm very fond of the short-circuit operators and I use them frequently.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

### Re: Short-Circuit: Why Use their Bitwise Counterparts?

xlucas wrote:I believe there are good reasons to sometimes use And/Or and sometimes AndAlso/OrElse.

I use AND/OR when I mimic real logic stuff, so when I want to compute some binary integers (c = a AND b). Otherwise ANDALSO/ORELSE are more useful for conditional testing. And it's a good thing if they are fast, otherwise this would be quite sad.
Example, you have a pointer P:

Code: Select all

`if P<>0 andAlso *P=33 then      ...else      ? "null pointer!"end if`

It checks for a null pointer before trying to evaluate on it.

Return to “Beginners”

### Who is online

Users browsing this forum: No registered users and 11 guests