<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">So I recently discovered that the shift operators no longer appear to be defined anywhere, i.e- they seem to only be implemented by convention rather than being required by a protocol, which doesn’t seem ideal. The problem with this is that I wanted to implement some bitwise operations but there’s no longer an obvious place to do this, and I ended up having to implement my own concept of a BitwiseIntegerType, as you can see in the example below:<br class=""><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">protocol</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> BitwiseIntegerType : </span>BitwiseOperationsType<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">, </span>IntegerType<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div><span style="font-family: Menlo; font-size: 11px;" class="">&nbsp; &nbsp;&nbsp;</span><span style="font-family: Menlo; font-size: 11px; color: rgb(187, 44, 162);" class="">init</span><span style="font-family: Menlo; font-size: 11px;" class="">(</span><span style="font-family: Menlo; font-size: 11px; color: rgb(187, 44, 162);" class="">_</span><span style="font-family: Menlo; font-size: 11px;" class="">&nbsp;value:</span><span style="font-family: Menlo; font-size: 11px; color: rgb(112, 61, 170);" class="">Int</span><span style="font-family: Menlo; font-size: 11px;" class="">)</span><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span> value:<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UInt</span>)</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> &lt;&lt; (lhs:<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Self</span>, rhs:<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Self</span>) -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Self</span></div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> &gt;&gt; (lhs:<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Self</span>, rhs:<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Self</span>) -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Self</span></div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> &lt;&lt;= (<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">inout</span> lhs:<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Self</span>, rhs:<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Self</span>)</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> &gt;&gt;= (<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">inout</span> lhs:<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Self</span>, rhs:<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Self</span>)</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>BitwiseIntegerType<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">static</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> allOnes:<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Self</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">~</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">Self</span>.allZeros }</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">static</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> numberOfBits:<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UInt</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UInt</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">sizeof</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">Self</span>) * <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">8</span>) }</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">static</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> mostSignificantBit:<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Self</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">Self</span>.allOnes <span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">&lt;&lt;</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">Self</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">Self</span>.numberOfBits - <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span>) }</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> leadingZeros:<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UInt</span> {</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">==</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">Self</span>.allZeros { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">Self</span>.numberOfBits }</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> value = <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span></div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> width = <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">Self</span>.numberOfBits</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> mask = <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">Self</span>.allOnes</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> zeros:<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UInt</span> = <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span></div><div style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">while</span> (value <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">&amp;</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">Self</span>.mostSignificantBit) <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">==</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">Self</span>.allZeros {</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> (value <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">&amp;</span> mask) <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">==</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">Self</span>.allZeros {</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zeros += width</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value <span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">&lt;&lt;=</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">Self</span>(width)</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">else</span> {</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width /= <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">2</span></div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mask <span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">&lt;&lt;=</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">Self</span>(width)</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> zeros</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> : </span>BitwiseIntegerType<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {}</span></div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int8</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> : </span>BitwiseIntegerType<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {}</span></div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int16</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> : </span>BitwiseIntegerType<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {}</span></div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int32</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> : </span>BitwiseIntegerType<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {}</span></div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int64</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> : </span>BitwiseIntegerType<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {}</span></div><div style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UInt</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> : </span>BitwiseIntegerType<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {}</span></div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UInt8</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> : </span>BitwiseIntegerType<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {}</span></div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UInt16</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> : </span>BitwiseIntegerType<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {}</span></div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UInt32</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> : </span>BitwiseIntegerType<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {}</span></div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UInt64</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> : </span>BitwiseIntegerType<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {}</span></div><div style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int64</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.mostSignificantBit&nbsp; &nbsp; </span>// -9223372036854775808</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1234567</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span>leadingZeros<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #008400" class="">// 43</span></div></div><div style="margin: 0px; font-size: 11px; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #008400" class=""><br class=""></span></div>I think that the best solution would be to add the shift operators to BitwiseOperationsType, while declaring a BitwiseIntegerType similar to what I’ve done above that groups BitwiseOperationsType with IntegerType to create a distinction between binary-based integers and integers that could be based on some other mechanism in future, as well as to declare the required initializers from Int and UInt types. This gives more flexibility in defining higher level protocol extensions that rely on the full range of bitwise operations, without having to move any of it further up (iirc some of these operators used to be in IntegerType).<div class=""><br class=""></div><div class="">Either way, the shift operators are currently declared by convention, which I don’t think is right, as they should surely be declared as a requirement somewhere.</div><div class=""><br class=""></div><div class="">Also, ignore the actual implementation of leadingZeros, it may not be the most efficient method, it’s just a useful illustration of something that can be done with the protocol declarations, I’ve also omitted warnings and such to keep things simple.</div><div class=""><br class=""></div><div class="">Just wondering what others think? One other issue I’m unsure about is that the required Int and Uint initialiizers should probably be of the truncatingBitPattern type for bitwise operations, but I wasn’t sure how to handle adding that to the types that don’t currently have these initializers (i.e- the 64-bit types that don’t need them since they can’t currently be initialized from anything bigger).</div></body></html>