<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 <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> 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 <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 <span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px" class="">IntegerArithmeticType, </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=""> and </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 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 </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=""> </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=""> </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=""> <span style="color:rgb(187,44,162)" class="">func</span> <<(lhs: <span style="color:rgb(112,61,170)" class="">Self</span>, rhs: <span style="color:rgb(112,61,170)" class="">UIntMax</span>) -> <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=""> </span>@warn_unused_result</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">func</span> >>(lhs: <span style="color:rgb(112,61,170)" class="">Self</span>, rhs: <span style="color:rgb(112,61,170)" class="">UIntMax</span>) -> <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 <span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px" class="">IntegerType</span> conformed to that protocol I don't think there's anything I want that <span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px" class="">Int</span> can do and <span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px" class="">IntegerType</span> can't.</div><div class=""><br class=""></div><div class=""><b class="">A related but slightly off-topic gripe:</b></div><div class="">Integer protocols in Standard Library define <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> or <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> they 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 <span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px" class="">IntegerType</span> must conform to <span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px" class="">_MaxBuiltinIntegerType.</span> It's type is much wider than <font color="#703daa" face="Menlo" class=""><span style="font-size:11px" class="">IntMax</span></font> (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 <span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px" class="">_MaxBuiltinIntegerType</span> 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> type<font color="#703daa" face="Menlo" class=""><span style="font-size:11px" class="">. </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 </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=""> </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=""> <span style="color:rgb(187,44,162)" class="">func</span> << <T: <span style="color:rgb(112,61,170)" class="">UnsignedIntegerType</span>>(lhs: <span style="color:rgb(112,61,170)" class="">Self</span>, rhs: <span style="color:rgb(112,61,170)" class="">T</span>) -> <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=""> </span>@warn_unused_result</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">func</span> >> <T: <span style="color:rgb(112,61,170)" class="">UnsignedIntegerType</span>>(lhs: <span style="color:rgb(112,61,170)" class="">Self</span>, rhs: <span style="color:rgb(112,61,170)" class="">T</span>) -> <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=""><<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>></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 >> and << 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="">
- Greg<br class="">
<div class="HOEnZb"><div class="h5"><br class="">
> On Dec 18, 2015, at 3:55 AM, Jeremy Pereira via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class="">
><br class="">
> These are the definitions of the right shift operators<br class="">
><br class="">
> public func >>(lhs: Int8, rhs: Int8) -> Int8<br class="">
><br class="">
> public func >>(lhs: Int, rhs: Int) -> Int<br class="">
><br class="">
> public func >>(lhs: UInt, rhs: UInt) -> UInt<br class="">
><br class="">
> public func >>(lhs: Int64, rhs: Int64) -> Int64<br class="">
><br class="">
> public func >>(lhs: UInt64, rhs: UInt64) -> UInt64<br class="">
><br class="">
> public func >>(lhs: UInt8, rhs: UInt8) -> UInt8<br class="">
><br class="">
> public func >>(lhs: UInt16, rhs: UInt16) -> UInt16<br class="">
><br class="">
> public func >>(lhs: Int16, rhs: Int16) -> Int16<br class="">
><br class="">
> public func >>(lhs: Int32, rhs: Int32) -> Int32<br class="">
><br class="">
> public func >>(lhs: UInt32, rhs: UInt32) -> UInt32<br class="">
><br class="">
><br class="">
> 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="">
><br class="">
> public func >>(lhs: UInt64, rhs: Int) -> UInt64<br class="">
><br class="">
> 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="">
><br class="">
> 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="">
><br class="">
> let aNumber: UInt64 = 0x123456<br class="">
> var numberAsBits: [Bool] = [];<br class="">
> for i in 0 ..< 64<br class="">
> {<br class="">
> numberAsBits.append((aNumber >> i) & 1 != 0); // Error because i needs to be cast to a UInt64<br class="">
> }<br class="">
><br class="">
> I would like additional versions of the shift operator where rhs is an Int please.<br class="">
><br class="">
> Needless to say, the same applies to the left shift operators.<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="">
<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>