# Advantages of masking shift operators?

**URL:** <https://forums.swift.org/t/advantages-of-masking-shift-operators/24419>\
**Category:** Using Swift\
**Created:** [May 11, 2019, 2:57pm UTC](https://forums.swift.org/t/advantages-of-masking-shift-operators/24419 "2019-05-11T14:57:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@Martin](https://forums.swift.org/u/Martin)\
**Post date:** [May 11, 2019, 2:57pm UTC](https://forums.swift.org/t/advantages-of-masking-shift-operators/24419/1 "2019-05-11T14:57:11Z")

</div>

I am trying to understand the possible advantages of the “masking shift operators” (`&<<` and `&>>`) over the “normal shift operators” (`<<` and `>>`). In [SR-6749](https://bugs.swift.org/browse/SR-6749) it is said that

> The goal of the operator, however, is never to have this wrapping behavior happen — it's what you use when you have static knowledge that your shift amount is \<= the bitWidth. By masking, you and the compiler can agree that there's no branch needed, so you get slightly faster code without zero branching and zero risk of undefined behavior (which a negative or too large shift would be).

and I can see that they are used a lot in the Swift standard library, for example in [UTF8.swift](https://github.com/apple/swift/blob/master/stdlib/public/core/UTF8.swift#L94):

```
  let bits = source._biasedBits &- 0x01010101
  var value = (bits & 0b0_11_1111 __0000_0000__ 0000_0000__0000_0000) &>> 24
  value |= (bits & 0b0 ____________11_1111__ 0000_0000__0000_0000) &>> 10
  value |= (bits & 0b0 _______________________11_1111__ 0000_0000) &<< 4
  value |= (bits & 0b0 ________________________________ 0000_0111) &<< 18

```

I have two questions:

1. What is the “risk of undefined behavior” when using the normal shift operators?

2. Why is it advantageous to use the masking shift operator if the shift amount is a _literal constant_ (less than the bit width)?

Thanks for any insight,  
Martin

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [May 13, 2019, 4:19pm UTC](https://forums.swift.org/t/advantages-of-masking-shift-operators/24419/2 "2019-05-13T16:19:22Z")

</div>

Neither kind of shift has undefined behavior; the two operators just take different approaches to handling out-of-range shift amounts:

- `<<` and `>>` treat shift amounts greater than the bit width as shifting out all the bits, i.e. producing 0 or -1. They also handle negative shift amounts. As you note, this is called out in the docs.

- `&<<` and `&>>` "mask" their shift amount to be within the valid range for the argument; i.e. "64" on a 64-bit value is treated as a shift of 0, and "33" on a 16-bit value is treated as a shift of 1. Negative numbers also get this treatment, so "-1" on a 16-bit value becomes 15. The use of the term "mask" here is a bit of a misnomer since a bitmask operation only works for integer types whose bit-width is a power of two, but I think we decided it was better to have something slightly incorrect than invent a new notation family.

(The reference to undefined behavior comes from C, where shift amounts outside of `0..<BIT_WIDTH` are considered undefined.)

The former, `<<`, is what we think someone would usually expect, but it means testing the value beforehand. So the latter, `&<<` is also available to do something slightly faster. With constant values, there "shouldn't" be any performance difference in which one you use, but in practice the compiler has an easier time reasoning about `&>>` than `>>`.

@scanon, confirm/refute?

---

<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:** [May 13, 2019, 4:32pm UTC](https://forums.swift.org/t/advantages-of-masking-shift-operators/24419/3 "2019-05-13T16:32:25Z")

</div>

Everything Jordan says is correct.

- All shifts in Swift are always fully defined.
- The "masking" shifts shift by `rhs` mod `lhs.bitWidth`. That happens to correspond to mask for all power-of-two sized integer types, but if there were, say, an `Int7` type, the "masking" shifts would mod the count into the range 0 ..\< 7.
- In practice, for in-range constants there should not be any difference.
- I expect @Michael_Ilseman used `&>>` and `&<<` here out of an abundance of caution.

---

<div class="post-metadata">

**Author:** ![Michael\_Ilseman](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/michael_ilseman/32/44640_2.png) [@Michael\_Ilseman](https://forums.swift.org/u/Michael_Ilseman)\
**Post date:** [May 13, 2019, 6:17pm UTC](https://forums.swift.org/t/advantages-of-masking-shift-operators/24419/4 "2019-05-13T18:17:52Z")

</div>

> [@scanon](#):
>
> - I expect @Michael_Ilseman used `&>>` and `&<<` here out of an abundance of caution.

That snippet is @dabrahams code. I also tend to put them on everything, even for constants, to reduce last-minute surprises and reduce cognitive load for the performance skeptic.
