# [swift-evolution] [Discussion] Breaking precedence

Xiaodi Wu xiaodi.wu at gmail.com
Mon Aug 1 23:34:29 CDT 2016

```Right. I asked about this issue some months ago on this list, and Chris was
very patient to confirm that what you described was indeed the thinking
behind the rationalized precedence of bitwise operators in Swift.

On Mon, Aug 1, 2016 at 23:18 Félix Cloutier <swift-evolution at swift.org>
wrote:

> I disagree. The binary operators have properties that are comparable to
> arithmetic operators, and their precedence is easy to define as such. & has
> multiplication-like properties (0*0=0, 0*1=0, 1*0=0, 1*1=1); | has
> addition-like properties (0+0=0, 0+1=1, 1+0=1, 1+1=2); ^ has
> subtraction-like properties (0-0=0, 0-1=-1, 1-0=1, 1-1=0), and their
> precedences are set accordingly (& is multiplicative, | and ^ are additive).
>
> The same applies to && and ||. Bit shifts are exponentiative.
>
> Binary operators get especially confusing in some languages because their
> precedence is lower than comparison operators. For instance, in C, `a & b
> == c` gets parsed as `a & (b == c)`. In Swift, the precedence of binary
> operators is above that of comparison operators, so we don't have that
> problem.
>
> Félix
>
> Le 1 août 2016 à 20:48:21, Rob Mayoff via swift-evolution <
> swift-evolution at swift.org> a écrit :
>
> 1 | 2 ^ 3   // or this?
>
>
>
> No. Both of those are bitwise operations. They are often used together.
> They
> have a refined relative precedence in Swift that makes sense.
>
>
> I have no idea what the relative precedence of those operators is in
> Swift, C, or any other language, and thinking about it now, no
> relative precedence seems sensible or obvious to me. Those operators
> (and bitwise-&) are excellent examples of operators that should
> require parentheses.
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160802/83a7a266/attachment.html>
```