Kevin Reid (2014-01-16T21:41:45.000Z)
domenic at domenicdenicola.com (2014-01-24T19:54:30.615Z)
On Thu, Jan 16, 2014 at 1:12 PM, Jens Nockert <jens at nockert.se> wrote: > 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.)