<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="">I don't support picking a single RHS type.</div><div class=""><br class=""></div>Again I'll mention that the <a href="https://github.com/apple/swift/blob/master/test/Prototypes/Integers.swift.gyb#L1082" class="">prototype</a> I'm already working on to replace the integer protocols and types already supports <i class="">any</i> integer type appearing on the RHS of a shift operation, where the LHS is any fixed-width integer type. It also supports negative shifts and overshifts without trapping or causing undefined behavior (as suggested by Steve Canon there's also a new masking shift operator that masks the RHS before shifting, for those who can't afford the branch).<div class=""><br class=""><div class=""><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 19, 2015, at 7:43 PM, Jordan Rose 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 class="" style="font-family: AvenirNext-Regular; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">+1 to dropping the existing signatures and providing a consistent right-hand side.</div><div class="" style="font-family: AvenirNext-Regular; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div><div class="" style="font-family: AvenirNext-Regular; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">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="" style="font-family: AvenirNext-Regular; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div><div class="" style="font-family: AvenirNext-Regular; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">(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="" style="font-family: AvenirNext-Regular; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div><div class="" style="font-family: AvenirNext-Regular; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">Jordan</div><br class="" style="font-family: AvenirNext-Regular; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><div style="font-family: AvenirNext-Regular; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><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 class="" style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;">IntegerType</span><span class="" style="font-family: Menlo; font-size: 11px;">.</span><br class=""><div class=""><br class=""></div><div class="">See <span class="" style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;">IntegerArithmeticType, </span><span class="" style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;">SignedNumberType</span><span class="" style="font-family: Menlo; font-size: 11px;"> and </span><span class="" style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;">BitwiseOperationsType</span><span class="" style="font-family: Menlo; font-size: 11px;">.</span></div><div class=""><span class="" style="font-family: Menlo; font-size: 11px;"><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 class="" style="margin: 0px 0px 0px 40px; border: none; padding: 0px;"><div class=""><div class=""><span class="" style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px;">protocol </span><span class="" style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;">IntegerBitShiftType {</span></div></div><div class=""><div class=""><span class="" style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;"> </span><font color="#bb2ca2" face="Menlo" class=""><span class="" style="font-size: 11px;">var</span></font><span class="" style="font-family: Menlo; font-size: 11px;"><span class="Apple-converted-space"> </span>sizeInBits:<span class="Apple-converted-space"> </span></span><span class="" style="font-family: Menlo; font-size: 11px; color: rgb(112, 61, 170);">UIntMax</span></div></div><div class=""><div class=""><span class="" style="font-family: Menlo; font-size: 11px; color: rgb(112, 61, 170);"><br class=""></span></div></div><div class=""><div class=""><span class="" style="font-family: Menlo; font-size: 11px; color: rgb(112, 61, 170);"> </span><span class="" style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px;">@warn_unused_result</span></div></div><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span class="Apple-converted-space"> </span><span class="" style="color: rgb(187, 44, 162);">func</span><span class="Apple-converted-space"> </span><<(lhs:<span class="Apple-converted-space"> </span><span class="" style="color: rgb(112, 61, 170);">Self</span>, rhs: <span class="" style="color: rgb(112, 61, 170);">UIntMax</span>) -><span class="Apple-converted-space"> </span><span class="" style="color: rgb(112, 61, 170);">Self</span></div></div><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><br class=""></div></div><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);"><span class=""> <span class="Apple-converted-space"> </span></span>@warn_unused_result</div></div><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span class="Apple-converted-space"> </span><span class="" style="color: rgb(187, 44, 162);">func</span><span class="Apple-converted-space"> </span>>>(lhs:<span class="Apple-converted-space"> </span><span class="" style="color: rgb(112, 61, 170);">Self</span>, rhs: <span class="" style="color: rgb(112, 61, 170);">UIntMax</span>) -><span class="Apple-converted-space"> </span><span class="" style="color: rgb(112, 61, 170);">Self</span></div></div><div class=""><div class=""><span class="" style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;">}</span></div></div></blockquote><div class=""><div class=""><div class=""><br class=""></div></div><div class="">If <span class="" style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;">IntegerType</span> conformed to that protocol I don't think there's anything I want that <span class="" style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;">Int</span> can do and <span class="" style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;">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 class="" style="font-family: Menlo; font-size: 11px; color: rgb(187, 44, 162);">init</span><span class="" style="font-family: Menlo; font-size: 11px;">(</span><span class="" style="font-family: Menlo; font-size: 11px; color: rgb(187, 44, 162);">_</span><span class="" style="font-family: Menlo; font-size: 11px;">:<span class="Apple-converted-space"> </span></span><span class="" style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;">IntMax</span><span class="" style="font-family: Menlo; font-size: 11px;">)</span> or <span class="" style="font-family: Menlo; font-size: 11px; color: rgb(187, 44, 162);">init</span><span class="" style="font-family: Menlo; font-size: 11px;">(</span><span class="" style="font-family: Menlo; font-size: 11px; color: rgb(187, 44, 162);">_</span><span class="" style="font-family: Menlo; font-size: 11px;">:<span class="Apple-converted-space"> </span></span><span class="" style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;">UIntMax</span><span class="" style="font-family: Menlo; font-size: 11px;">),</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 class="" style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;">IntegerType</span> must conform to <span class="" style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;">_MaxBuiltinIntegerType.</span> It's type is much wider than<span class="Apple-converted-space"> </span><font color="#703daa" face="Menlo" class=""><span class="" style="font-size: 11px;">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 class="" style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;">_MaxBuiltinIntegerType</span> is 4096 bits, but it is still much smaller than what you want from a<span class="Apple-converted-space"> </span><span class="" style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;">BigInt</span> type<font color="#703daa" face="Menlo" class=""><span class="" style="font-size: 11px;">. </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 class="" style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px;">protocol </span><span class="" style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;">IntegerBitShiftType {</span></div><div class=""><span class="" style="font-family: Menlo; font-size: 11px; color: rgb(112, 61, 170);"> </span><span class="" style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px;">@warn_unused_result</span><br class=""></div><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span class="" style="color: rgb(187, 44, 162);">func</span> << <T: <span class="" style="color: rgb(112, 61, 170);">UnsignedIntegerType</span>>(lhs: <span class="" style="color: rgb(112, 61, 170);">Self</span>, rhs: <span class="" style="color: rgb(112, 61, 170);">T</span>) -> <span class="" style="color: rgb(112, 61, 170);">Self</span></div></div><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><br class=""></div></div><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);"><span class=""> </span>@warn_unused_result</div></div><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span class="" style="color: rgb(187, 44, 162);">func</span> >> <T: <span class="" style="color: rgb(112, 61, 170);">UnsignedIntegerType</span>>(lhs: <span class="" style="color: rgb(112, 61, 170);">Self</span>, rhs: <span class="" style="color: rgb(112, 61, 170);">T</span>) -> <span class="" style="color: rgb(112, 61, 170);">Self</span></div></div><div class=""><span class="" style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;">}</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 class="Apple-converted-space"> </span><span dir="ltr" class=""><<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>></span><span class="Apple-converted-space"> </span>wrote:<br class=""><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: 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="">><span class="Apple-converted-space"> </span><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">><span class="Apple-converted-space"> </span><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" class="" style="height: 1px !important; width: 1px !important; border-width: 0px !important; margin: 0px !important; padding: 0px !important;"><span class="Apple-converted-space"> </span>_______________________________________________<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" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class="" style="font-family: AvenirNext-Regular; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=JfMPa-2F7wwZPzsZ3QKA8NjtONIYX4SjbWuUxtpfsTY2hQStYWU-2B10Bh4FcVUSeOcA01-2FGr36bIpQqhUVtStPTrmYfdSTxP-2B2n4C6X-2FnXGxA5YetYtRi9fn3dkLltz2MWAQk970ZmG5AbQiIpNXurfv8FDq7hVLzZrmI7Dfp2aGAePEZRqpFwq5SlB-2BYnJDe0odNwQwioDdQhjX0Izt6lGo5vGpuURJgwgopYIGWdUCnI-3D" alt="" width="1" height="1" border="0" style="font-family: AvenirNext-Regular; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; height: 1px !important; width: 1px !important; border-width: 0px !important; margin: 0px !important; padding: 0px !important;" class=""><span style="font-family: AvenirNext-Regular; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class=""><span class="Apple-converted-space"> </span>_______________________________________________</span><br style="font-family: AvenirNext-Regular; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: AvenirNext-Regular; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">swift-evolution mailing list</span><br style="font-family: AvenirNext-Regular; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="mailto:swift-evolution@swift.org" style="font-family: AvenirNext-Regular; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">swift-evolution@swift.org</a><br style="font-family: AvenirNext-Regular; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" style="font-family: AvenirNext-Regular; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></div></blockquote></div><br class=""><div class="">
-Dave<div class=""><br class=""></div><br class="Apple-interchange-newline">
</div>
<br class=""></div></div></div></body></html>