Skip to content

Support ^ operator with bools #7229

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
chr-1x opened this issue Nov 26, 2020 · 7 comments
Closed

Support ^ operator with bools #7229

chr-1x opened this issue Nov 26, 2020 · 7 comments
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@chr-1x
Copy link

chr-1x commented Nov 26, 2020

For complex predicates where you want to check if two conditions are different, it's more straightforward to write:

if (some_long_predicate() ^ some_other_long_predicate()) {
  // ...
}

than

if ((some_long_predicate() || some_other_long_predicate() && !(some_long_predicate() && some_other_long_predicate()) {
  // ...
}

especially if you only want to evaluate the predicates once.

If using this bitwise operator with bools is undesirable for some reason, a keyword operator xor similar to and and or could be added.

(Current compiler behavior:

./src/repro_xor.zig:12:12: error: expected token ')', found '=='
        if (thing == null ^ other_thing == null) {

)

Note that this works trivially in C because bool is implicitly convertible to int. To do the same conversion in zig we would have to write @boolToInt around each predicate and compare the result of the xor to 1.

@pfgithub
Copy link
Contributor

!= is the same as boolean xor but the intent is a bit less readable

if (some_long_predicate() != some_other_long_predicate()) {
  // ...
}

@daurnimator daurnimator added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Nov 26, 2020
@AssortedFantasy
Copy link

AssortedFantasy commented Nov 26, 2020

It's good to know that xor is just not-equal (!=) while xnor is equals (==).

Actually, I believe using and, or, xor and a combination of negating inputs and negating outputs gives the full set of two variable Boolean functions, other than the trivial ones like always true and always one, or ones which simplify down to a single variable (not dependant on one of the inputs) [Comma operator rolling in its grave rn]. With a few redundant ways of doing things.

Some of the more useful ones:

A nand B = not ( A and B ) = not(A) or not(B)
A nor B = not( A or B ) = not(A) and not(B)
A implies B = not(A) or B

@rohlem
Copy link
Contributor

rohlem commented Nov 27, 2020

Technically a duplicate of #5805 , which was marked as "closed".
Bit operators were however considered for vectors of bool, and for uniformity potentially all bool, but then I don't think that follow-up proposal was ever opened.

!= is the same as boolean xor but the intent is a bit less readable

For the intent in the OP, "check if two conditions are different", to me != looks more readable than ^.
What looks unusual to me however is composing multiple: a != b != c != d vs a ^ b ^ c ^ d

For keywords, note that currently Zig keywords influence control flow (short-circuit and and or, the only exception to this are function calls), while symbol operators don't (regular & and |).
As xor cannot be short-circuit-evaluated, a keyword would go against the convention up to now.

@apg360
Copy link

apg360 commented Nov 28, 2020

Hi,

A small comment about : a != b != c != d vs a ^ b ^ c ^ d

I might agree readable yes but "a != b != c != d" is more how mathematics would write it.

Assumption : At time C was built it was mostly by people doing lot of math for software engineering, and Zig = C fixed so going forward with how we would write it in mathematics makes sens.

Let me know what you think.

Kinds regards

@SpexGuy
Copy link
Contributor

SpexGuy commented Nov 28, 2020

"a != b != c != d" is more how mathematics would write it.

AFAIK in mathematics this would imply a != b and b != c and c != d, but in Zig this is equivalent to (a != b or c != d) and (a == b or c == d)

@apg360
Copy link

apg360 commented Nov 28, 2020

Yes, you are right.
Thank you

@andrewrk andrewrk added this to the 0.8.0 milestone Nov 30, 2020
@andrewrk
Copy link
Member

No ^ operator with bools

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

8 participants