<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=""><div class="">+1 to dropping the existing signatures and providing a consistent right-hand side.</div><div class=""><br class=""></div><div class="">I'm a little concerned about not allowing Int as the shift type. Even though we disallow negative shifts, we generally encourage using 'Int' as the "vocabulary" type for integers, which means any shift by a non-constant amount might require a conversion. I do see that BigInts may be shifted by more than 2^64, however, so hardcoding Int wouldn't be right either.</div><div class=""><br class=""></div><div class="">(We do want to make sure the implementation is efficient when both argument types are a machine-representable integers, though, which may not be possible with all UnsignedIntegerTypes.)</div><div class=""><br class=""></div><div class="">Jordan</div><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 19, 2015, at 5:54 , Andrew Bennett via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">+1<div class=""><br class=""></div><div class="">Something worth considering with this proposal: Should it be IntMax rather than Int? (or UIntMax, does a negative count make sense?).</div><div class=""><br class=""></div><div class="">Although it's probably never going to be enough if you want to calculate the next largest known prime number in swift, the last was 2^57885161 − 1.<br class=""></div><div class=""><br class=""></div><div class=""><div class=""><b class="">Also if it's in scope:</b></div><div class=""><br class=""></div><div class="">Left and right shift are the only operators on integers that aren't generalised by a protocol. Every other operator is somehow part of&nbsp;<span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px" class="">IntegerType</span><span style="font-family:Menlo;font-size:11px" class="">.</span><br class=""><div class=""><br class=""></div><div class="">See&nbsp;<span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px" class="">IntegerArithmeticType,&nbsp;</span><span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px" class="">SignedNumberType</span><span style="font-family:Menlo;font-size:11px" class="">&nbsp;and&nbsp;</span><span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px" class="">BitwiseOperationsType</span><span style="font-family:Menlo;font-size:11px" class="">.</span></div><div class=""><span style="font-family:Menlo;font-size:11px" class=""><br class=""></span></div>It would be nice to&nbsp;create a new protocol for something that can shift. The new protocol could be:</div></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class=""><div class=""><div class=""><span style="color:rgb(187,44,162);font-family:Menlo;font-size:11px" class="">protocol&nbsp;</span><span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px" class="">IntegerBitShiftType {</span></div></div><div class=""><div class=""><span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px" class="">&nbsp; &nbsp;&nbsp;</span><font color="#bb2ca2" face="Menlo" class=""><span style="font-size:11px" class="">var</span></font><span style="font-family:Menlo;font-size:11px" class=""> sizeInBits: </span><span style="font-family:Menlo;font-size:11px;color:rgb(112,61,170)" class="">UIntMax</span></div></div><div class=""><div class=""><span style="font-family:Menlo;font-size:11px;color:rgb(112,61,170)" class=""><br class=""></span></div></div><div class=""><div class=""><span style="font-family:Menlo;font-size:11px;color:rgb(112,61,170)" class="">&nbsp; &nbsp;&nbsp;</span><span style="color:rgb(187,44,162);font-family:Menlo;font-size:11px" class="">@warn_unused_result</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="color:rgb(187,44,162)" class="">func</span> &lt;&lt;(lhs: <span style="color:rgb(112,61,170)" class="">Self</span>, rhs:&nbsp;<span style="color:rgb(112,61,170)" class="">UIntMax</span>) -&gt; <span style="color:rgb(112,61,170)" class="">Self</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class=""><span style="" class="">&nbsp; &nbsp; </span>@warn_unused_result</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="color:rgb(187,44,162)" class="">func</span> &gt;&gt;(lhs: <span style="color:rgb(112,61,170)" class="">Self</span>, rhs:&nbsp;<span style="color:rgb(112,61,170)" class="">UIntMax</span>) -&gt; <span style="color:rgb(112,61,170)" class="">Self</span></div></div><div class=""><div class=""><span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px" class="">}</span></div></div></blockquote><div class="">



<div class=""><div class=""><br class=""></div></div><div class="">If&nbsp;<span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px" class="">IntegerType</span>&nbsp;conformed to that protocol I don't think there's anything I want that&nbsp;<span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px" class="">Int</span>&nbsp;can do and&nbsp;<span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px" class="">IntegerType</span>&nbsp;can't.</div><div class=""><br class=""></div><div class=""><b class="">A related but&nbsp;slightly off-topic gripe:</b></div><div class="">Integer protocols in Standard Library define&nbsp;<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="">: </span><span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px" class="">IntMax</span><span style="font-family: Menlo; font-size: 11px;" class="">)</span>&nbsp;or&nbsp;<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="">: </span><span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px" class="">UIntMax</span><span style="font-family: Menlo; font-size: 11px;" class="">),</span>&nbsp;they&nbsp;assume that there isn't a wider type. When trying to make a new type conform to IntegerType (ie. a BigInt library) this can be an issue.</div><div class=""><br class=""></div><div class="">It also seems to be necessary that things implementing&nbsp;<span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px" class="">IntegerType</span>&nbsp;must conform to&nbsp;<span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px" class="">_MaxBuiltinIntegerType.</span>&nbsp;It's type is much wider than <font color="#703daa" face="Menlo" class=""><span style="font-size:11px" class="">IntMax</span></font>&nbsp;(64x for me) and its interface is empty which makes it hard to conform.</div><div class=""><br class=""></div><div class="">I think currently&nbsp;<span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px" class="">_MaxBuiltinIntegerType</span>&nbsp;is 4096 bits, but it is still much smaller than what you want from a <span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px" class="">BigInt</span>&nbsp;type<font color="#703daa" face="Menlo" class=""><span style="font-size:11px" class="">.&nbsp;</span></font>While I have suggested using IntMax in this implementation I think that IntMax is inherently flawed in its usage. However it is the best available at the moment in my opinion.</div><div class=""><br class=""></div><div class="">Something like this may work in a more general case:</div><div class=""><br class=""></div><div class=""><div class=""><span style="color:rgb(187,44,162);font-family:Menlo;font-size:11px" class="">protocol&nbsp;</span><span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px" class="">IntegerBitShiftType {</span></div><div class=""><span style="font-family:Menlo;font-size:11px;color:rgb(112,61,170)" class="">&nbsp; &nbsp;&nbsp;</span><span style="color:rgb(187,44,162);font-family:Menlo;font-size:11px" class="">@warn_unused_result</span><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;<span style="color:rgb(187,44,162)" class="">func</span>&nbsp;&lt;&lt; &lt;T:&nbsp;<span style="color:rgb(112,61,170)" class="">UnsignedIntegerType</span>&gt;(lhs:&nbsp;<span style="color:rgb(112,61,170)" class="">Self</span>, rhs:&nbsp;<span style="color:rgb(112,61,170)" class="">T</span>) -&gt;&nbsp;<span style="color:rgb(112,61,170)" class="">Self</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class=""><span style="" class="">&nbsp; &nbsp;&nbsp;</span>@warn_unused_result</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;<span style="color:rgb(187,44,162)" class="">func</span>&nbsp;&gt;&gt; &lt;T:&nbsp;<span style="color:rgb(112,61,170)" class="">UnsignedIntegerType</span>&gt;(lhs:&nbsp;<span style="color:rgb(112,61,170)" class="">Self</span>, rhs:&nbsp;<span style="color:rgb(112,61,170)" class="">T</span>) -&gt;&nbsp;<span style="color:rgb(112,61,170)" class="">Self</span></div></div><div class=""><span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px" class="">}</span></div></div><div class=""><br class=""></div></div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Sat, Dec 19, 2015 at 3:02 AM, Greg Titus via swift-evolution <span dir="ltr" class="">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">+1. In fact, I would go even farther than asking for an additional version that uses Int on the rhs, I think all of the existing definitions are arguably wrong and they should all just be changed to use Int. I just took a brief scan of all uses of &gt;&gt; and &lt;&lt; in the stdlib: most use constants on the rhs, and so would be unaffected. It looks like all the rest either use Int on the lhs or require explicit casting or calling numericCast() to get the rhs to match.<br class="">
<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; - Greg<br class="">
<div class="HOEnZb"><div class="h5"><br class="">
&gt; On Dec 18, 2015, at 3:55 AM, Jeremy Pereira via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class="">
&gt;<br class="">
&gt; These are the definitions of the right shift operators<br class="">
&gt;<br class="">
&gt;&nbsp; &nbsp; &nbsp; &nbsp;public func &gt;&gt;(lhs: Int8, rhs: Int8) -&gt; Int8<br class="">
&gt;<br class="">
&gt;&nbsp; &nbsp; &nbsp; &nbsp;public func &gt;&gt;(lhs: Int, rhs: Int) -&gt; Int<br class="">
&gt;<br class="">
&gt;&nbsp; &nbsp; &nbsp; &nbsp;public func &gt;&gt;(lhs: UInt, rhs: UInt) -&gt; UInt<br class="">
&gt;<br class="">
&gt;&nbsp; &nbsp; &nbsp; &nbsp;public func &gt;&gt;(lhs: Int64, rhs: Int64) -&gt; Int64<br class="">
&gt;<br class="">
&gt;&nbsp; &nbsp; &nbsp; &nbsp;public func &gt;&gt;(lhs: UInt64, rhs: UInt64) -&gt; UInt64<br class="">
&gt;<br class="">
&gt;&nbsp; &nbsp; &nbsp; &nbsp;public func &gt;&gt;(lhs: UInt8, rhs: UInt8) -&gt; UInt8<br class="">
&gt;<br class="">
&gt;&nbsp; &nbsp; &nbsp; &nbsp;public func &gt;&gt;(lhs: UInt16, rhs: UInt16) -&gt; UInt16<br class="">
&gt;<br class="">
&gt;&nbsp; &nbsp; &nbsp; &nbsp;public func &gt;&gt;(lhs: Int16, rhs: Int16) -&gt; Int16<br class="">
&gt;<br class="">
&gt;&nbsp; &nbsp; &nbsp; &nbsp;public func &gt;&gt;(lhs: Int32, rhs: Int32) -&gt; Int32<br class="">
&gt;<br class="">
&gt;&nbsp; &nbsp; &nbsp; &nbsp;public func &gt;&gt;(lhs: UInt32, rhs: UInt32) -&gt; UInt32<br class="">
&gt;<br class="">
&gt;<br class="">
&gt; Note that both left and right hand side are of the same type. In my opinion, rhs, which represents the number of bits to shift, should always be an Int e.g.<br class="">
&gt;<br class="">
&gt;&nbsp; &nbsp; &nbsp; &nbsp;public func &gt;&gt;(lhs: UInt64, rhs: Int) -&gt; UInt64<br class="">
&gt;<br class="">
&gt; The two operands are fundamentally different, the left hand one is conceptually an array of bits and the right hand one is conceptually a count.<br class="">
&gt;<br class="">
&gt; The current definitions mean that I almost always have to do a cast on the right operand with shift operations. e.g. the following snippet that converts a UInt64 into an array of boolean values.<br class="">
&gt;<br class="">
&gt;&nbsp; &nbsp; let aNumber: UInt64 = 0x123456<br class="">
&gt;&nbsp; &nbsp; var numberAsBits: [Bool] = [];<br class="">
&gt;&nbsp; &nbsp; for i in 0 ..&lt; 64<br class="">
&gt;&nbsp; &nbsp; {<br class="">
&gt;&nbsp; &nbsp; &nbsp; &nbsp; numberAsBits.append((aNumber &gt;&gt; i) &amp; 1 != 0); // Error because i needs to be cast to a UInt64<br class="">
&gt;&nbsp; &nbsp; }<br class="">
&gt;<br class="">
&gt; I would like additional versions of the shift operator where rhs is an Int please.<br class="">
&gt;<br class="">
&gt; Needless to say, the same applies to the left shift operators.<br class="">
&gt;<br class="">
&gt; _______________________________________________<br class="">
&gt; swift-evolution mailing list<br class="">
&gt; <a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">
&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
<br class="">
_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
</div></div></blockquote></div><br class=""></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=ZEz4qHYnXhPr3bBPu-2FxP4tN3HfWKL-2FtJpqkQ0gkOVSDkjrhCV4yrH4IoKDyZfHxv3ug1u7zFC58w-2FLvuXpY5-2B6ECP7hQsjUp3dJIcbh0e0rLcDC6EiTpEZ8Ui7ZdMeLOq6rR1vrN4-2Fxk7rFoAx-2FYvDzF7fZ8-2FuCvrqLsPT7xPC3E9BIcms6An6OGO1OtOrIR1Qu2rtTsPynWIdC0-2BTceo3Cb-2BPAwmX5IS5ls61s5UBg-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;" class="">
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>