# Add XOR (Exclusive OR) operator support for Bool type

**URL:** <https://forums.swift.org/t/add-xor-exclusive-or-operator-support-for-bool-type/41346>\
**Category:** Pitches\
**Tags:** extensions, operators, extension\
**Created:** [October 21, 2020, 8:41am UTC](https://forums.swift.org/t/add-xor-exclusive-or-operator-support-for-bool-type/41346 "2020-10-21T08:41:21Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonathan-Hoch](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@Jonathan-Hoch](https://forums.swift.org/u/Jonathan-Hoch)\
**Post date:** [October 21, 2020, 8:41am UTC](https://forums.swift.org/t/add-xor-exclusive-or-operator-support-for-bool-type/41346/1 "2020-10-21T08:41:22Z")

</div>

The ^ (XOR - Exclusive OR) operator is currently not supported for Bool type.  
It is commonly used in other languages such as C and Java.

I propose adding support via an extension.

So, Instead of having to write something like this:

```swift
if switch1 != (switch2 != (switch3 != switch4)) {
	isOn = true
} else {
	isOn = false       
}

```

Adding support - We could then write it like this:

```swift
if switch1 ^ switch2 ^ switch3 ^ switch4 {
	isOn = true
} else {
	isOn = false
}

```

P.S. If excepted, I would like to add it myself to the language if an expert contributor would guide me through the processes. Thanks.

---

<div class="post-metadata">

**Author:** ![SusanCheng](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/susancheng/32/9439_2.png) [@SusanCheng](https://forums.swift.org/u/SusanCheng)\
**Post date:** [October 21, 2020, 9:30am UTC](https://forums.swift.org/t/add-xor-exclusive-or-operator-support-for-bool-type/41346/2 "2020-10-21T09:30:00Z")

</div>

like this?

```
infix func ^ (lhs: Bool, rhs: Bool) -> Bool {
    return lhs != rhs
}

```

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [October 21, 2020, 4:25pm UTC](https://forums.swift.org/t/add-xor-exclusive-or-operator-support-for-bool-type/41346/3 "2020-10-21T16:25:54Z")

</div>

It isn’t an oversight but a deliberate decision.

`Bool` also doesn’t have `&` and `|`, and as you know, `&&` and `||` behave somewhat differently in terms of precedence, associativity, and short circuiting. Additionally, in Swift, the precedence of operators (and particularly of bitwise operators) has been rationalized and differ from other C-family languages.

Swift’s design also reflects the decision that `Bool` does _not_ represent a bit; it represents _only_ a logical `true` or `false` value. (By contrast, the integer types model both the integers and a sequence of bits.)

To remain consistent with this model, it would not do to add merely `^` for `Bool`. Either we would have to add `&` and `|` also (i.e., overturn the decision that `Bool` doesn’t represent a single bit, a choice that has been deliberately avoided), or we would want to create `^^` just for `Bool`. But if we had `^^` behave consistently with `&&` and `||`, then it would just be more or less another spelling for `!=`.

As you show, it’s possible to use `!=` for this operation. Swift deliberately allows users to make their own extensions, so you can always use it for yourself, but I don’t think a `^` or `^^` would be a good fit for addition to the standard library for the reasons above.

---

<div class="post-metadata">

**Author:** ![GreatApe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/greatape/32/2640_2.png) [@GreatApe](https://forums.swift.org/u/GreatApe)\
**Post date:** [October 21, 2020, 8:01pm UTC](https://forums.swift.org/t/add-xor-exclusive-or-operator-support-for-bool-type/41346/4 "2020-10-21T20:01:46Z")

</div>

It should definitely be called `^^` and it should absolutely be the same as `!=`. The issue isn’t that you can’t do it, it’s that `!=` is a very confusing to see for people who aren’t used to it.

If the idea is to minimise the number of boolean operators, we don’t need both AND and OR. Come to think of it, we could just make do with NAND, no need even for NOT...

---

<div class="post-metadata">

**Author:** ![scanon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/scanon/32/2445_2.png) [@scanon](https://forums.swift.org/u/scanon)\
**Post date:** [October 21, 2020, 11:09pm UTC](https://forums.swift.org/t/add-xor-exclusive-or-operator-support-for-bool-type/41346/5 "2020-10-21T23:09:30Z")

</div>

The thing about `^^` is that it appears to imply that the operation can short circuit (for people with some C-family experience), but xor never short circuits; you always have to evaluate both arguments.

@Jonathan-Hoch Can you give a non-synthetic example of how you want to use this operator? The only real issue--for me--with `!=` is that it doesn't have associativity defined, so you can't chain them without parentheses. But chaining more than two or three booleans together like this is relatively rare, so it would be helpful to see the use cases that are motivating your request.

---

<div class="post-metadata">

**Author:** ![Jonathan-Hoch](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@Jonathan-Hoch](https://forums.swift.org/u/Jonathan-Hoch)\
**Post date:** [October 22, 2020, 10:06am UTC](https://forums.swift.org/t/add-xor-exclusive-or-operator-support-for-bool-type/41346/6 "2020-10-22T10:06:28Z")

</div>

@xwu. I understand your points.

1. Bitwise operators precedence are rationalised and differ from other languages
2. Bool does not represent a single bit.

In a few projectes I came across needing Bool to act as a bit and used an extension.

```swift
extension Bool {
	static func ^(lhs: Bool, rhs: Bool) -> Bool {
		return lhs != rhs
	}
}

```

I was surprised it wasn't "built in" as I was use to it in other languages.

On the one hand it makes sense for Swift to have Bool be only a logical true or false value as there are no "primitive types" in Swift. However, in other languages Bool-boolean is a primitive type and does support Bitwise operations. - This takes me back to the basics of CS. Boolean to me is the most basic bit 1-0 flipper.

To overturn Swift’s design that Bool does not represent a bit - Is "way beyond my pay grade". 🙂

I leave the descision for you guys here. Thanks.

---

<div class="post-metadata">

**Author:** ![scanon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/scanon/32/2445_2.png) [@scanon](https://forums.swift.org/u/scanon)\
**Post date:** [October 22, 2020, 2:38pm UTC](https://forums.swift.org/t/add-xor-exclusive-or-operator-support-for-bool-type/41346/8 "2020-10-22T14:38:15Z")

</div>

> [@AlexanderM](#):
>
> ```swift
> let switchesByState = Dictionary(grouping: switches, by: \.self)
> isOn = switchesByState[true] == switchesByState[false]
> 
> ```

This isn't quite right; to get xor, you only want to check the parity (true if there are an odd number of trues, false if even). If you already have them in a collection you can just use `reduce(false, !=)`; the trick is when you don't have a collection.

---

<div class="post-metadata">

**Author:** ![GreatApe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/greatape/32/2640_2.png) [@GreatApe](https://forums.swift.org/u/GreatApe)\
**Post date:** [October 22, 2020, 5:05pm UTC](https://forums.swift.org/t/add-xor-exclusive-or-operator-support-for-bool-type/41346/9 "2020-10-22T17:05:57Z")

</div>

So the repeating of the operator indicates, in C, that there is short circuiting? How important is this C convention for modern Swift though?

And as you say, XOR can never short circuit, so even people familiar with the convention wouldn't expect `^^` to short circuit, meaning it wouldn't cause any bugs, right?

If that is correct, then I would say that it's worth more to give modern Swift users an XOR that looks like an XOR, than to adhere to a C convention.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [October 22, 2020, 5:37pm UTC](https://forums.swift.org/t/add-xor-exclusive-or-operator-support-for-bool-type/41346/10 "2020-10-22T17:37:23Z")

</div>

> [@GreatApe](#):
>
> So the repeating of the operator indicates, in C, that there is short circuiting? How important is this C convention for modern Swift though?
> 
> And as you say, XOR can never short circuit, so even people familiar with the convention wouldn't expect `^^` to short circuit, meaning it wouldn't cause any bugs, right?

Not just in C, but in C _family_ languages, `&&` and `||` short circuit and `&` and `|` do not. Swift is, in that respect at least, a member of the C _family_ and the same behavior applies.

If `&` and `|` were defined for Swift, users would be justified in expecting that they do not short circuit. It would follow that since a logical XOR cannot short circuit, it would not be spelled `^^`. However, it also cannot be spelled `^` because that operator is already defined with the expected precedence relative to `&` and `|` and not to `&&` and `||`.

> [@GreatApe](#):
>
> If that is correct, then I would say that it's worth more to give modern Swift users an XOR that looks like an XOR, than to adhere to a C convention

I’m not sure to what you refer here by “looks like a XOR” other than a spelling that isn’t `!=`. To be clear, it is not as though users don’t know about or misunderstand `!=` on `Bool`, and it both looks and behaves the same way in many languages (which is to say, it looks like `!=` and behaves as a logical XOR in many languages). There is no evidence of user misunderstanding as to its behavior, and if it were not present in Swift it would be added to the language with the exact semantics it has.

It’s pretty clear to me that having two logical XOR operators would be problematic. Either another operator behaves identically to `!=` in every respect for `Bool` values, which would then be confusing and unprecedented, or it does not because it would have different associativity and/or precedence, which would also be confusing and unprecedented.

If there is a concrete use case that isn’t well served by Swift’s current operators, we should look at what the use case is and devise another solution. Otherwise, I think it’s fair to guide users to deploy Swift’s existing operators efficaciously.

---

<div class="post-metadata">

**Author:** ![scanon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/scanon/32/2445_2.png) [@scanon](https://forums.swift.org/u/scanon)\
**Post date:** [October 22, 2020, 5:58pm UTC](https://forums.swift.org/t/add-xor-exclusive-or-operator-support-for-bool-type/41346/11 "2020-10-22T17:58:25Z")

</div>

FWIW, in my own code¹, I would _probably_ write this as:

```swift
isOn = [switch1, switch2, switch3, switch4].reduce(false, !=)

```

I personally find this clearer, especially if the switches are actually expressions. YMMV.

¹ at least outside of a performance-critical context; in such a context I would have to see how well the optimizer does with the expression (and maybe avoid it anyway because of debug build performance concerns).

---

<div class="post-metadata">

**Author:** ![AlexanderM](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alexanderm/32/9781_2.png) [@AlexanderM](https://forums.swift.org/u/AlexanderM)\
**Post date:** [October 22, 2020, 7:19pm UTC](https://forums.swift.org/t/add-xor-exclusive-or-operator-support-for-bool-type/41346/12 "2020-10-22T19:19:09Z")

</div>

Oh I totally misunderstood it.

`isOn` should be `true` if there's an `odd` number of `true`s, right? (Conversely, true if there is not an even number of `true`s)

Couldn't that be expressed as just a counting operation?

```swift
isOn = !switches
    .lazy
    .filter(\.self)
    .count
    .isMultiple(of: 2)

```

This is where a `count(where:)` would come in really useful, to remove the need for `.lazy.filter()`. An `isOdd` and `isEven` function would be cool too. If you add extensions for those, you get this down to:

```swift
isOn = switches.count(where: \.self).isOdd

```

---

<div class="post-metadata">

**Author:** ![GreatApe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/greatape/32/2640_2.png) [@GreatApe](https://forums.swift.org/u/GreatApe)\
**Post date:** [October 22, 2020, 8:24pm UTC](https://forums.swift.org/t/add-xor-exclusive-or-operator-support-for-bool-type/41346/14 "2020-10-22T20:24:31Z")

</div>

> [@xwu](#):
>
> I’m not sure to what you refer here by “looks like a XOR” other than a spelling that isn’t `!=` .

Yes I mean that `!=` doesn't look like XOR. I mean it _isn't_ an XOR operator, it is a very general operator that is available for tons of types and means "not equal". If you look in the docs it's not even explained, and it is placed separately from `&&` and `||` which _are_ explained. The only mention of `!=` is in the documentation for `==` where it says:

```
/// Equality is the inverse of inequality. For any values `a` and `b`,
/// `a == b` implies that `a != b` is `false`.

```

So I don't think it is fair to say we already have XOR. We just have an unrelated operator that happens to have the same _truth table_ as XOR.

It might be a well known workaround among C-developers, but shouldn't Swift aim to be accessible for new developers too? At the very least the docs and the official Swift book should reflect that `!=` is viewed as XOR I think.

Maybe I'm the only one, but I certainly didn't know of the `!=` trick when I came to iOS development. I did come to use `!=` sometimes when it fit the behaviour I wanted, and only several years later I realised that it was actually the same as, and an acknowledged synonym of XOR.

Maybe this is because I don't have a C background, but I think such developers will be increasingly common.

But I am not sure that it's a strong argument to say that you can get by without it. You can get by without `Bool.toggle()` and `Int.isMultiple(of:)` too, and a lot of other convenient methods recently introduced.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [October 22, 2020, 8:36pm UTC](https://forums.swift.org/t/add-xor-exclusive-or-operator-support-for-bool-type/41346/15 "2020-10-22T20:36:30Z")

</div>

> [@GreatApe](#):
>
> The only mention of `!=` is in the documentation for `==` where it says:

It's very reasonable to file a bug to document the `!=` more completely. For `Bool`, it should absolutely point out that it is how you spell an exclusive disjunction.

> [@GreatApe](#):
>
> So I don't think it is fair to say we already have XOR. We just have an unrelated operator that happens to have the same _truth table_ as XOR.
> 
> It might be a well known workaround among C-developers, but shouldn't Swift aim to be accessible for new developers too? At the very least the docs and the official Swift book should reflect that `!=` is viewed as XOR I think.
> 
> Maybe I'm the only one, but I certainly didn't know of the `!=` trick when I came to iOS development. I did come to use `!=` sometimes when it fit the behaviour I wanted, and only several years later I realised that it was actually the same as, and an acknowledged synonym of XOR.
> 
> Maybe this is because I don't have a C background, but I think such developers will be increasingly common.

It's not a trick or a workaround, and it has nothing to do with C. This is just Boolean algebra.

> [@GreatApe](#):
>
> But I am not sure that it's a strong argument to say that you can get by without it. You can get by without `Bool.toggle()` and `Int.isMultiple(of:)` too, and a lot of other convenient methods recently introduced.

There is a distinction between convenience methods (which have a set of criteria to meet for inclusion into the standard library) and presenting duplicate APIs for the same operation.

---

<div class="post-metadata">

**Author:** ![GreatApe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/greatape/32/2640_2.png) [@GreatApe](https://forums.swift.org/u/GreatApe)\
**Post date:** [October 22, 2020, 8:49pm UTC](https://forums.swift.org/t/add-xor-exclusive-or-operator-support-for-bool-type/41346/16 "2020-10-22T20:49:31Z")

</div>

> [@xwu](#):
>
> It's not a trick or a workaround, and it has nothing to do with C. This is just Boolean algebra.

It's undoubtedly a workaround, since `!=` exist independently of boolean algebra, it is not limited to `Bool`. I hope you can agree that it is merely a lucky accident that it has the same truth table? Just like `==` can be used as XNOR. Who would argue that Swift has an XNOR operator?

The question is only if it's so commonly accepted that you can expect people to know it.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [October 22, 2020, 8:51pm UTC](https://forums.swift.org/t/add-xor-exclusive-or-operator-support-for-bool-type/41346/17 "2020-10-22T20:51:04Z")

</div>

> [@GreatApe](#):
>
> It's undoubtedly a workaround, since `!=` exist independently of boolean algebra, it is not limited to `Bool` . I hope you can agree that it is merely a lucky accident that it has the same truth table? Just like `==` can be used as XNOR. Who would argue that Swift has an XNOR operator?
> 
> The question is only if it's so commonly accepted that you can expect people to know it.

No! It's not a lucky accident. It's _math_.

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [October 22, 2020, 8:54pm UTC](https://forums.swift.org/t/add-xor-exclusive-or-operator-support-for-bool-type/41346/18 "2020-10-22T20:54:00Z")

</div>

> [@GreatApe](#):
>
> But I am not sure that it's a strong argument to say that you can get by without it. You can get by without `Bool.toggle()` and `Int.isMultiple(of:)` too, and a lot of other convenient methods recently introduced.

If we're quoting them, we should at least compare their metric to the current (`^`) discussion.

For `isMultiple(of:)`:

> [@\[Accepted with Modifications\] SE-0225: Adding isMultiple to BinaryInteger](https://forums.swift.org/t/accepted-with-modifications-se-0225-adding-ismultiple-to-binaryinteger/15689/1):
>
> Almost all library features can be composed from existing features with enough effort. To be considered for addition to the library, a proposed feature must satisfy two conditions: it must provide functionality that is useful to a substantial population of Swift programmers, and it must provide substantial advantages over the alternative ways of accomplishing that functionality. Potential advantages include:
> 
> - It may be complex, challenging, or error-prone for users to implement themselves.
> - It may have substantial performance advantages over a user implementation, either because it has access to library internals or just because the library implementation will likely be more carefully tuned.
> - It may be substantially easier to work with because it composes better with other language or library features.
> - It may be substantially more "fluent": that is, more natural to discover, use, and read in code. The implementation may involve composing primitives in a subtle or tricky way, or the primitives may be unfamiliar to many programmers. This is a more subjective criterion than the others, and people may reasonably differ about how to apply it.

For `toggle`:

> [@Accepted: SE-199 – Add \`toggle\` to \`Bool\`](https://forums.swift.org/t/accepted-se-199-add-toggle-to-bool/10681/1):
>
> The core team sees the following motivations for its addition:
> 
> - **commonality:** the team felt that the need to toggle a nested boolean is a common one across multiple different problem domains
> - **readability:** the addition of this command can significantly improve readability, especially as was seen in the example of toggling a boolean in a type held as a value in a dictionary
> - **consistency:** while reproducing the method in user code is fairly trivial, it is good to have a consistent way of doing this across all swift codebases, rather than having multiple variants
> - **correctness:** there is a correctness risk when the same long path needs to appear on both sides (especially when subscripts are involved), which this method would avoid
> - **performance:** this didn’t come up during the review, but a mutating member operation will be more efficient (requiring only one subscript lookup) once we have the co-routine-based accessors, planned for a future release

---

<div class="post-metadata">

**Author:** ![GreatApe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/greatape/32/2640_2.png) [@GreatApe](https://forums.swift.org/u/GreatApe)\
**Post date:** [October 22, 2020, 8:54pm UTC](https://forums.swift.org/t/add-xor-exclusive-or-operator-support-for-bool-type/41346/19 "2020-10-22T20:54:09Z")

</div>

If it's not an accident, it would mean that `!=` was somehow designed to be used as XOR, and later generalised to work for all equatable types.

Of course it's not an accident that it works every time without fail, I'm talking about the design of the language.

---

<div class="post-metadata">

**Author:** ![GreatApe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/greatape/32/2640_2.png) [@GreatApe](https://forums.swift.org/u/GreatApe)\
**Post date:** [October 22, 2020, 8:56pm UTC](https://forums.swift.org/t/add-xor-exclusive-or-operator-support-for-bool-type/41346/20 "2020-10-22T20:56:45Z")

</div>

I'd say we have at least readability, consistency and correctness. But I'm not necessarily saying we have to introduce it, I just don't think all the arguments against it are very strong.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [October 22, 2020, 8:59pm UTC](https://forums.swift.org/t/add-xor-exclusive-or-operator-support-for-bool-type/41346/21 "2020-10-22T20:59:27Z")

</div>

> [@GreatApe](#):
>
> If it's not an accident, it would mean that `!=` was somehow designed to be used as XOR, and later generalised to work for all equatable types.
> 
> Of course it's not an accident that it works every time without fail, I'm talking about the design of the language.

Again, it's not a matter of programming language design. If a language has a Boolean type and a notion of equivalence, then this is how it works, because _math_. It's like asking if `1 + 1 == 2` is by design. Anyway, I think we've sufficiently explored this here.

---

<div class="post-metadata">

**Author:** ![GreatApe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/greatape/32/2640_2.png) [@GreatApe](https://forums.swift.org/u/GreatApe)\
**Post date:** [October 22, 2020, 9:00pm UTC](https://forums.swift.org/t/add-xor-exclusive-or-operator-support-for-bool-type/41346/22 "2020-10-22T21:00:45Z")

</div>

I feel that you are misunderstanding me on purpose. I'm not saying it's an accident in Swift, I mean in general.

[Next page](https://forums.swift.org/t/add-xor-exclusive-or-operator-support-for-bool-type/41346.md?page=2)
