<div dir="ltr">I saw it mentioned somewhere that the standard library team wants to allow smaller types to be used, like <<(lhs: UInt64, rhs: UInt8). I'm not sure exactly how that would fit in here.<div class="gmail_extra">
<br><div class="gmail_quote">On Mon, Jan 18, 2016 at 4:45 AM, Haravikk via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">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><div><br></div><div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(112,61,170)"><span style="color:#bb2ca2">protocol</span><span style="color:#000000"> BitwiseIntegerType : </span>BitwiseOperationsType<span style="color:#000000">, </span>IntegerType<span style="color:#000000"> {</span></div><span style="font-family:Menlo;font-size:11px"> </span><span style="font-family:Menlo;font-size:11px;color:rgb(187,44,162)">init</span><span style="font-family:Menlo;font-size:11px">(</span><span style="font-family:Menlo;font-size:11px;color:rgb(187,44,162)">_</span><span style="font-family:Menlo;font-size:11px"> value:</span><span style="font-family:Menlo;font-size:11px;color:rgb(112,61,170)">Int</span><span style="font-family:Menlo;font-size:11px">)</span><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">init</span>(<span style="color:#bb2ca2">_</span> value:<span style="color:#703daa">UInt</span>)</div><div style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">func</span> << (lhs:<span style="color:#703daa">Self</span>, rhs:<span style="color:#703daa">Self</span>) -> <span style="color:#703daa">Self</span></div><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">func</span> >> (lhs:<span style="color:#703daa">Self</span>, rhs:<span style="color:#703daa">Self</span>) -> <span style="color:#703daa">Self</span></div><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">func</span> <<= (<span style="color:#bb2ca2">inout</span> lhs:<span style="color:#703daa">Self</span>, rhs:<span style="color:#703daa">Self</span>)</div><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">func</span> >>= (<span style="color:#bb2ca2">inout</span> lhs:<span style="color:#703daa">Self</span>, rhs:<span style="color:#703daa">Self</span>)</div><div style="margin:0px;font-size:11px;font-family:Menlo">}</div><div style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#bb2ca2">extension</span><span style="color:#000000"> </span>BitwiseIntegerType<span style="color:#000000"> {</span></div><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">static</span> <span style="color:#bb2ca2">var</span> allOnes:<span style="color:#703daa">Self</span> { <span style="color:#bb2ca2">return</span> <span style="color:#3d1d81">~</span><span style="color:#bb2ca2">Self</span>.allZeros }</div><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">static</span> <span style="color:#bb2ca2">var</span> numberOfBits:<span style="color:#703daa">UInt</span> { <span style="color:#bb2ca2">return</span> <span style="color:#703daa">UInt</span>(<span style="color:#3d1d81">sizeof</span>(<span style="color:#bb2ca2">Self</span>) * <span style="color:#272ad8">8</span>) }</div><div style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">static</span> <span style="color:#bb2ca2">var</span> mostSignificantBit:<span style="color:#703daa">Self</span> { <span style="color:#bb2ca2">return</span> <span style="color:#bb2ca2">Self</span>.allOnes <span style="color:#31595d"><<</span> <span style="color:#bb2ca2">Self</span>(<span style="color:#bb2ca2">Self</span>.numberOfBits - <span style="color:#272ad8">1</span>) }</div><div style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">var</span> leadingZeros:<span style="color:#703daa">UInt</span> {</div><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">if</span> <span style="color:#bb2ca2">self</span> <span style="color:#3d1d81">==</span> <span style="color:#bb2ca2">Self</span>.allZeros { <span style="color:#bb2ca2">return</span> <span style="color:#bb2ca2">Self</span>.numberOfBits }</div><div style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">var</span> value = <span style="color:#bb2ca2">self</span></div><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">var</span> width = <span style="color:#bb2ca2">Self</span>.numberOfBits</div><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">var</span> mask = <span style="color:#bb2ca2">Self</span>.allOnes</div><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">var</span> zeros:<span style="color:#703daa">UInt</span> = <span style="color:#272ad8">0</span></div><div style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">while</span> (value <span style="color:#3d1d81">&</span> <span style="color:#bb2ca2">Self</span>.mostSignificantBit) <span style="color:#3d1d81">==</span> <span style="color:#bb2ca2">Self</span>.allZeros {</div><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">if</span> (value <span style="color:#3d1d81">&</span> mask) <span style="color:#3d1d81">==</span> <span style="color:#bb2ca2">Self</span>.allZeros {</div><div style="margin:0px;font-size:11px;font-family:Menlo"> zeros += width</div><div style="margin:0px;font-size:11px;font-family:Menlo"> value <span style="color:#31595d"><<=</span> <span style="color:#bb2ca2">Self</span>(width)</div><div style="margin:0px;font-size:11px;font-family:Menlo"> } <span style="color:#bb2ca2">else</span> {</div><div style="margin:0px;font-size:11px;font-family:Menlo"> width /= <span style="color:#272ad8">2</span></div><div style="margin:0px;font-size:11px;font-family:Menlo"> mask <span style="color:#31595d"><<=</span> <span style="color:#bb2ca2">Self</span>(width)</div><div style="margin:0px;font-size:11px;font-family:Menlo"> }</div><div style="margin:0px;font-size:11px;font-family:Menlo"> }</div><div style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">return</span> zeros</div><div style="margin:0px;font-size:11px;font-family:Menlo"> }</div><div style="margin:0px;font-size:11px;font-family:Menlo">}</div><div style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#bb2ca2">extension</span><span style="color:#000000"> </span><span style="color:#703daa">Int</span><span style="color:#000000"> : </span>BitwiseIntegerType<span style="color:#000000"> {}</span></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#bb2ca2">extension</span><span style="color:#000000"> </span><span style="color:#703daa">Int8</span><span style="color:#000000"> : </span>BitwiseIntegerType<span style="color:#000000"> {}</span></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#bb2ca2">extension</span><span style="color:#000000"> </span><span style="color:#703daa">Int16</span><span style="color:#000000"> : </span>BitwiseIntegerType<span style="color:#000000"> {}</span></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#bb2ca2">extension</span><span style="color:#000000"> </span><span style="color:#703daa">Int32</span><span style="color:#000000"> : </span>BitwiseIntegerType<span style="color:#000000"> {}</span></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#bb2ca2">extension</span><span style="color:#000000"> </span><span style="color:#703daa">Int64</span><span style="color:#000000"> : </span>BitwiseIntegerType<span style="color:#000000"> {}</span></div><div style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#bb2ca2">extension</span><span style="color:#000000"> </span><span style="color:#703daa">UInt</span><span style="color:#000000"> : </span>BitwiseIntegerType<span style="color:#000000"> {}</span></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#bb2ca2">extension</span><span style="color:#000000"> </span><span style="color:#703daa">UInt8</span><span style="color:#000000"> : </span>BitwiseIntegerType<span style="color:#000000"> {}</span></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#bb2ca2">extension</span><span style="color:#000000"> </span><span style="color:#703daa">UInt16</span><span style="color:#000000"> : </span>BitwiseIntegerType<span style="color:#000000"> {}</span></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#bb2ca2">extension</span><span style="color:#000000"> </span><span style="color:#703daa">UInt32</span><span style="color:#000000"> : </span>BitwiseIntegerType<span style="color:#000000"> {}</span></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#bb2ca2">extension</span><span style="color:#000000"> </span><span style="color:#703daa">UInt64</span><span style="color:#000000"> : </span>BitwiseIntegerType<span style="color:#000000"> {}</span></div><div style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(0,132,0)"><span style="color:#703daa">Int64</span><span style="color:#000000">.mostSignificantBit </span>// -9223372036854775808</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#272ad8">1234567</span><span style="color:#000000">.</span>leadingZeros<span style="color:#000000"> </span><span style="color:#008400">// 43</span></div></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#008400"><br></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><br></div><div>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><br></div><div>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><br></div><div>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></div><br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div></div>