std.bitmanip - bitshift?

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Dec 15 07:29:34 PST 2014


On Monday, 15 December 2014 at 15:19:25 UTC, Steven Schveighoffer 
wrote:
> On 12/13/14 5:47 AM, "Marc =?UTF-8?B?U2Now7x0eiI=?= 
> <schuetzm at gmx.net>" wrote:
>> On Friday, 12 December 2014 at 19:35:26 UTC, Steven 
>> Schveighoffer wrote:
>>> On 12/12/14 2:17 PM, H. S. Teoh via Digitalmars-d-learn wrote:
>>>> I've started working on an implementation of this... but 
>>>> it's not very
>>>> clear what the correct semantics should be. For example, if 
>>>> my starting
>>>> BitArray b is 1101, say, what should be the result after 
>>>> b>>=1? Should
>>>> it be 0110, 110, or 01101?
>>>
>>> 0110
>>>
>>> In other words, I would assume the same semantics as an 
>>> unsigned int.
>>>
>>> In other other words, it's like each bit moves one to the 
>>> right, and
>>> the bit that has no source gets a 0.
>>
>> There's a dedicated >>> operator for unsigned right shift, the 
>> normal >>
>> operator does a signed right shift, i.e. it copies the 
>> left-most bit:
>> http://dlang.org/expression#ShiftExpression
>>
>> IMO, for consistency, bitarray should behave the same.
>
> But BitArray is not signed, it's an array of bits, none of 
> which are signed or designated as the sign bit. The unsigned 
> shift operator is only for signed integral types, for unsigned 
> values >> is simply a shift of bits. Note that your assertion 
> that >> "copies the left-most bit" is not in the text, and is 
> incorrect. It copies the sign bit, which doesn't exist in an 
> unsigned type.
>
> I think >> and >>> should do the same thing, and be unsigned 
> shifts on BitArray.

Yes, yebblies pointed that out in the PR. I was confused, because 
the documentation doesn't say that `>>` and `>>>` only behave 
differently for signed integers.


More information about the Digitalmars-d-learn mailing list