Short-Circuit: Why Use their Bitwise Counterparts?

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

Short-Circuit: Why Use their Bitwise Counterparts?

Post by datwill310 »

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: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

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

Post by MrSwiss »

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 If
End If
With the short-cut operators:

Code: Select all

If (cond1) AndAlso (cond2) Then
    If (cond3) AndAlso (cond4) Then
        ...
    End If
End 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?

Post by datwill310 »

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 If
End If
With the short-cut operators:

Code: Select all

If (cond1) AndAlso (cond2) Then
    If (cond3) AndAlso (cond4) Then
        ...
    End If
End If
Thanks for the clarification.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

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

Post by fxm »

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?

Post by datwill310 »

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: 375
Joined: Jul 22, 2006 0:54
Location: Auckland

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

Post by TeeEmCee »

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: 334
Joined: May 09, 2014 21:19
Location: Argentina

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

Post by xlucas »

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?

Post by Tourist Trap »

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