On Thu, Jan 16, 2014 at 1:12 PM, Jens Nockert <j...@nockert.se> wrote:
>
>  On 2014/01/16, at 17:40, Jason Orendorff <jason.orendo...@gmail.com>
> wrote:
> > Or maybe: flip the function around so that it returns the number of
> > bits in the binary expansion of the value: Math.bitlen(15) === 4. This
> > is just (32 - CLZ), so it effectively computes the same thing as clz.
> > The advantage is that it extends naturally to integers of any size.
>
> What is Math.bitlen(-1) then? Isn’t this just the same problem as before,
> except it happens for negative numbers instead of positive?


FWIW: Common Lisp has rigorously transparent (that is, you cannot observe
the machine word size) bigints and quite a few binary operations defined on
them, so it's where I personally would look for precedent on such
questions. It doesn't have clz or bitlen per se, but it has these two
functions which contain positions on the issue:


integer-length
http://www.lispworks.com/documentation/HyperSpec/Body/f_intege.htm
 Returns the number of bits needed to represent 'integer' in binary
two's-complement format.
[Comment: This is equivalent to bitlen + 1 in order to count the sign bit,
and is well-defined for negative numbers.]

logcount
http://www.lispworks.com/documentation/HyperSpec/Body/f_logcou.htm
Computes and returns the number of bits in the two's-complement binary
representation of 'integer' that are `on' or `set'. If 'integer' is
negative, the 0 bits are counted; otherwise, the 1 bits are counted.


(If I had guessed without actually reading the docs, though, I would have
had logcount rejecting negative numbers.)
_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to