Unexpected precedence of `&` and `|`?

In my code I found recently an innocent looking bug of this type:

x=1
y=2
if x < 3 & y < 6
... do important stuff ...
end

What I meant to check in the if condition was of course

 (x < 3) & (y < 6)
## or
 x < 3 && y < 6

My current take-way is that I should never use & but always && except I really want know what I’m doing (i.e. fiddling with low-level stuff). Is this reasoning correct or did I miss a subtle point?

PS: Maybe it we should emphasize this difference more in the Noteworthy differences from R section

& is bitwise AND, && is short-circuiting logical AND. On booleans, these of course behave semantically the same, but if you chain things, precedence of & takes over compared to e.g. < and having some non-boolean on either side will lead to an unexpected result.

I usually avoid & for control flow for that reason and exclusively use && and || for control flow.

2 Likes