Short-Circuit: Why Use their Bitwise Counterparts?

New to FreeBASIC? Post your questions here.
datwill310
Posts: 352
Joined: May 29, 2015 20:37
Location: England
Contact:

Short-Circuit: Why Use their Bitwise Counterparts?

Postby datwill310 » Jul 03, 2017 13:27

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

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

Postby MrSwiss » Jul 03, 2017 13:58

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: 352
Joined: May 29, 2015 20:37
Location: England
Contact:

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

Postby datwill310 » Jul 03, 2017 14:20

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
Posts: 7272
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

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

Postby fxm » Jul 03, 2017 16:45

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: 352
Joined: May 29, 2015 20:37
Location: England
Contact:

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

Postby datwill310 » Jul 03, 2017 17:36

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

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

Postby TeeEmCee » Jul 07, 2017 2:52

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

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

Postby xlucas » Jul 07, 2017 20:38

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: 2197
Joined: Jun 02, 2015 16:24

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

Postby Tourist Trap » Aug 13, 2017 11:38

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: Makoto WATANABE, Yahoo [Bot] and 2 guests