Proposal: Explicitly require "Data.Bits.bit (-1) == 0" property

ARJANEN Loïc Jean David arjanen.loic at gmail.com
Sun Feb 16 14:10:42 UTC 2014


Hello,

I'll have to come down against that proposal because, at least on amd64 for 64 
bits-sized types (Int, Int64, Word & Word64), it doesn't works.
That's probably because bit n is usually implemented as 1 shifL n and that on 
this architechture, the arithmetic left-shift instruction takes only the shift 
count's 6 low-order bits into account. So we have bit (-1) = shiftL 1 (-1) = 
shiftL 1 63 = 2 ^ 63.
I suspect the same may happen for 32 bits-sized types  on x86 where only the 5 
low order-bits of the shift count are considered.

Regards,
ARJANEN Loïc

Le dimanche 16 février 2014, 11:14:12 Herbert Valerio Riedel a écrit :
> Hello *,
> 
> Right now, there seems to be no "defined" way to create a zero
> 'Bits'-value (w/o requiring also a 'Num' instance), that has all bits
> cleared without involving at least two operations from the 'Bits' class
> (e.g. `clearBit (bit 0) 0` or `let x = bit 0 in xor x x`).
> 
> OTOH, introducing a new method 'class Bits a where bitZero :: a' seems
> overkill to me.
> 
> However, "bit (-1)"[1] seems to result in just such a zero-value for all
> 'Bits' instances from base, so I'd hereby propose to simply document
> this as an expected property of 'bit', as well as the recommended way to
> introduce a zero-value (for when 'Num' is not available).
> 
> Discussion period: 2 weeks
> 
>  [1]: ...or more generally 'bit n == 0' for n<0, as it's usually
>       implemented as 'bit n = 1 shift n'


More information about the Libraries mailing list