# Bitshift operators

**URL:** <https://forums.swift.org/t/bitshift-operators/2978>\
**Category:** Discussion\
**Created:** [June 15, 2016, 7:36am UTC](https://forums.swift.org/t/bitshift-operators/2978 "2016-06-15T07:36:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![beccadax](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/beccadax/32/34217_2.png) [@beccadax](https://forums.swift.org/u/beccadax)\
**Post date:** [June 15, 2016, 7:36am UTC](https://forums.swift.org/t/bitshift-operators/2978/1 "2016-06-15T07:36:49Z")

</div>

While I was watching WWDC videos today, I noticed that there's a bit of annoying boilerplate in OptionSet specifications, and set out to get rid of it. Basically, what I'd like to do is this:

&nbsp;&nbsp;extension OptionSet where RawValue: Integer {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;init(bit bits: RawValue...) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.init(rawValue: bits.reduce(0) { $0 | (1 \<\< $1) })  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;}

&nbsp;&nbsp;struct FooOptions: OptionSet {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let rawValue: Int  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;static let bar = FooOptions(bit: 0)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;static let baz = FooOptions(bit: 1)  
&nbsp;&nbsp;}

However, this was thwarted by the fact that the \<\< operator is not in Integer. Or BitwiseOperations. Or any protocol, in fact.

From what I understand, this is a long-known issue we were planning to address in the Integer redesign, but I'm not sure what the status of that is at this point. I would guess that we're facing one of three situations:

1. An Integer redesign is still planned for Swift 3, but hasn't been reviewed yet.  
2. An Integer redesign has been deferred, but an incremental improvement (like adding bitshifting to BitwiseOperations) would be welcome.  
3. An Integer redesign has been deferred, and an incremental improvement is either not desired or too late at this point.

Can anyone share the status of this? If we're in #2, I'd be happy to put together a quick proposal to patch this particular issue up in time for Swift 3. (I assume that `OptionSet.init(bit:)` would be considered a "new feature" and thus out of scope.)

> **···**
>
> --  
> Brent Royal-Gordon  
> Architechies

---

<div class="post-metadata">

**Author:** ![dabrahams](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dabrahams/32/9815_2.png) [@dabrahams](https://forums.swift.org/u/dabrahams)\
**Post date:** [June 16, 2016, 2:36pm UTC](https://forums.swift.org/t/bitshift-operators/2978/2 "2016-06-16T14:36:54Z")

</div>

A proposal is imminent.

> **···**
>
> on Wed Jun 15 2016, Brent Royal-Gordon \<swift-evolution@swift.org\> wrote:
> 
> > While I was watching WWDC videos today, I noticed that there's a bit  
> > of annoying boilerplate in OptionSet specifications, and set out to  
> > get rid of it. Basically, what I'd like to do is this:
> > 
> > &nbsp;&nbsp;extension OptionSet where RawValue: Integer {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;init(bit bits: RawValue...) {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.init(rawValue: bits.reduce(0) { $0 | (1 \<\< $1) })  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;struct FooOptions: OptionSet {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let rawValue: Int
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;static let bar = FooOptions(bit: 0)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;static let baz = FooOptions(bit: 1)  
> > &nbsp;&nbsp;}
> > 
> > However, this was thwarted by the fact that the \<\< operator is not in  
> > Integer. Or BitwiseOperations. Or any protocol, in fact.
> > 
> > From what I understand, this is a long-known issue we were planning to  
> > address in the Integer redesign, but I'm not sure what the status of  
> > that is at this point.
> 
> --  
> -Dave

---

<div class="post-metadata">

**Author:** ![benrimmington](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/benrimmington/32/695_2.png) [@benrimmington](https://forums.swift.org/u/benrimmington)\
**Post date:** [June 16, 2016, 11:25am UTC](https://forums.swift.org/t/bitshift-operators/2978/3 "2016-06-16T11:25:47Z")

</div>

Brent Royal-Gordon wrote:

> While I was watching WWDC videos today, I noticed that there's  
> a bit of annoying boilerplate in OptionSet specifications, and  
> set out to get rid of it. Basically, what I'd like to do is this:
> 
> &nbsp;&nbsp;extension OptionSet where RawValue: Integer {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;init(bit bits: RawValue...) {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.init(rawValue: bits.reduce(0) { $0 | (1 \<\< $1) })  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;}
> 
> &nbsp;&nbsp;struct FooOptions: OptionSet {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let rawValue: Int
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;static let bar = FooOptions(bit: 0)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;static let baz = FooOptions(bit: 1)  
> &nbsp;&nbsp;}
> 
> However, this was thwarted by the fact that the \<\< operator is  
> not in Integer. Or BitwiseOperations. Or any protocol, in fact.

Alternatively, using binary integer literals:

&nbsp;&nbsp;struct FooOptions: OptionSet {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let rawValue: Int

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;static let bar = Self(rawValue: 0b01)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;static let baz = Self(rawValue: 0b10)  
&nbsp;&nbsp;}

Does the revised SE-0068 proposal allow this?

-- Ben

---

<div class="post-metadata">

**Author:** ![Brent\_Royal-Gordon](https://avatars.discourse-cdn.com/v4/letter/b/53a042/32.png) [@Brent\_Royal-Gordon](https://forums.swift.org/u/Brent_Royal-Gordon)\
**Post date:** [June 16, 2016, 10:22pm UTC](https://forums.swift.org/t/bitshift-operators/2978/4 "2016-06-16T22:22:10Z")

</div>

> A proposal is imminent.

Great—thanks for the update.

> **···**
>
> --  
> Brent Royal-Gordon  
> Architechies

---

<div class="post-metadata">

**Author:** ![benrimmington](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/benrimmington/32/695_2.png) [@benrimmington](https://forums.swift.org/u/benrimmington)\
**Post date:** [June 17, 2016, 12:51pm UTC](https://forums.swift.org/t/bitshift-operators/2978/5 "2016-06-17T12:51:27Z")

</div>

\<[Swift.org - API Design Guidelines](https://swift.org/documentation/api-design-guidelines/#type-conversion&gt);

> \*\*In initializers that perform full-width type conversions, omit the  
> first argument label\*\*, e.g. `Int64(someUInt32)`

OptionSet could add `init(_ rawValue: RawValue)` to reduce boilerplate:

&nbsp;&nbsp;struct FooOptions: OptionSet {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let rawValue: Int

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;static let bar = Self(0b01)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;static let baz = Self(0b10)  
&nbsp;&nbsp;}

-- Ben

---

<div class="post-metadata">

**Author:** ![dabrahams](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dabrahams/32/9815_2.png) [@dabrahams](https://forums.swift.org/u/dabrahams)\
**Post date:** [June 18, 2016, 1:25am UTC](https://forums.swift.org/t/bitshift-operators/2978/6 "2016-06-18T01:25:53Z")

</div>

Great idea; please submit a proposal!

> **···**
>
> on Fri Jun 17 2016, Ben Rimmington \<swift-evolution@swift.org\> wrote:
> 
> > \<[Swift.org - API Design Guidelines](https://swift.org/documentation/api-design-guidelines/#type-conversion&gt);
> > 
> > > \*\*In initializers that perform full-width type conversions, omit the  
> > > first argument label\*\*, e.g. `Int64(someUInt32)`
> > 
> > OptionSet could add `init(_ rawValue: RawValue)` to reduce boilerplate:
> > 
> > &nbsp;&nbsp;struct FooOptions: OptionSet {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let rawValue: Int
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;static let bar = Self(0b01)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;static let baz = Self(0b10)  
> > &nbsp;&nbsp;}
> 
> --  
> -Dave

---

<div class="post-metadata">

**Author:** ![benrimmington](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/benrimmington/32/695_2.png) [@benrimmington](https://forums.swift.org/u/benrimmington)\
**Post date:** [June 18, 2016, 8:47pm UTC](https://forums.swift.org/t/bitshift-operators/2978/7 "2016-06-18T20:47:43Z")

</div>

On second thoughts, perhaps the current design is better. OptionSet changes the failable initializer (inherited from RawRepresentable) into a \*nonfailable\* initializer. But if we used `init(_ rawValue: RawValue)` in OptionSet, it would also still have the failable initializer (which could maybe have a default implementation).

The original FooOptions example is missing some required boilerplate: `init(rawValue: Int) { self.rawValue = rawValue }`. In the future, a \*macro\* could be used to hide the RawRepresentable requirements of option sets, making them as easy to read/write as enum cases.

-- Ben

> **···**
>
> > On 18 Jun 2016, at 02:25, Dave Abrahams via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > on Fri Jun 17 2016, Ben Rimmington \<swift-evolution@swift.org\> wrote:
> > 
> > > \<[Swift.org - API Design Guidelines](https://swift.org/documentation/api-design-guidelines/#type-conversion&gt);
> > > 
> > > > \*\*In initializers that perform full-width type conversions, omit the  
> > > > first argument label\*\*, e.g. `Int64(someUInt32)`
> > > 
> > > OptionSet could add `init(_ rawValue: RawValue)` to reduce boilerplate:
> > > 
> > > &nbsp;&nbsp;struct FooOptions: OptionSet {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let rawValue: Int
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;static let bar = Self(0b01)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;static let baz = Self(0b10)  
> > > &nbsp;&nbsp;}
> > 
> > Great idea; please submit a proposal!
> > 
> > --  
> > -Dave
