Skip to content

Can't do bit-wise operations on Booleans. #27396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
indiealexh opened this issue Sep 20, 2016 · 3 comments
Closed

Can't do bit-wise operations on Booleans. #27396

indiealexh opened this issue Sep 20, 2016 · 3 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. closed-duplicate Closed in favor of an existing report library-core

Comments

@indiealexh
Copy link

indiealexh commented Sep 20, 2016

Take the following code:

bool val1 = true;
bool val2 = false;
bool result = val1 ^ val2;

The above quite logical code would throw:

Unhandled exception:
Class 'bool' has no instance method '^'.

As a result to be able to xor booleans together I have to do the following:

main() {
  bool val1 = true;
  bool val2 = false;
  bool result = xorBool(val1,val2);
}

bool xorBool(bool item1, bool item2) {
  var xorValue = (item1?1:0) ^ (item2?1:0);
  return (xorValue==1?true:false);
}

This seems seriously in error considering in JavaScript, Java, C# etc I can directly do bit-wise operations against a Boolean value.

Or as someone else pointed out (val1 != val2)

@lrhn
Copy link
Member

lrhn commented Sep 20, 2016

The recommended solution is to use != for xor on booleans.

It is possible to add ^ (and & and | as non-shortcircuiting versions of && and ||) as operators on the bool class, but we have so far decided not to do that.

(I'm not sure I will include JavaScript in the list of languages that define ^ on booleans because the result is not a boolean value - it's just doing numeric conversion on the operators and then integer bitwise xor on that result. That said, it does work because the result can be converted back to a boolean automatically too).

@indiealexh
Copy link
Author

Its not a blocking problem, but coming from many languages that do directly allow bitwise operators on booleans seemed like an odd decision.

What is the reasoning behind this?

@lrhn lrhn added area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-core closed-duplicate Closed in favor of an existing report labels Sep 20, 2016
@lrhn
Copy link
Member

lrhn commented Sep 20, 2016

The original request was #1080. The reasoning was that it could be confusing to have & do one thing on integers and another on bools.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. closed-duplicate Closed in favor of an existing report library-core
Projects
None yet
Development

No branches or pull requests

2 participants