Why can I combine a bitwise operator with a math operator, like &+, in Java?

Jojodmo :

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?

Nikolas :

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 is 1 & +2
  • 3 |- 4 is 3 | -4
  • 7 ^+ 9 is 7 ^ +9

Notice that * and / operators cannot be used in the same way (1 &/ 2).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=88494&siteId=1