I was writing some code earlier today, and forgot I was writing Java instead of Swift and accidentally typed &+
. Surprisingly, I didn't get any errors, and the code compiled and ran fine.
I played around a little bit, and found that &+
, &-
, |+
, |-
, ^+
, and ^-
all compile and run without errors. It looks like they all act like the first bitwise operator when acting on numbers:
1 &+ 2 == 1 & 2
3 |+ 4 == 3 | 4
7 ^+ 9 == 7 ^ 9
On the other hand, it seems like any other combination of a bitwise operator and a math operator, like +&
, &/
, and ^*
, all give compile-time errors.
Why do &+
, &-
, |+
, |-
, ^+
, and ^-
compile and run without errors? Do they have some purpose that I'm not seeing, or is it just some compilation oddity?
The +
stands for the Unary Plus Operator +
resulting in a positive number.
The -
means the Unary Minus Operator -
resulting in a negative number.
1 &+ 2
is1 & +2
3 |- 4
is3 | -4
7 ^+ 9
is7 ^ +9
Notice that *
and /
operators cannot be used in the same way (1 &/ 2
).