Subject: Re: 2s complement
To: Bill Studenmund <wrstuden@netbsd.org>
From: Chapman Flack <nblists@anastigmatix.net>
List: tech-kern
Date: 01/06/2006 22:02:23
Bill Studenmund wrote:
> Because I don't see how x^(x-1) will give you the first bit set. :-)

I forgot the x&(...) - sorry to anyone who spent any time head
scratching. It gives the /least/ bit set, which is the first if
you count from that end. Of course bit_ffs gives the /index/ of
the bit, which Bill rightly points out is O(lg2 n) more work--
if the machine hasn't got an instruction for it. O(8) vs. O(3)
isn't much difference, but when there's an opportunity to use
bitmasks wider than bytes, 32 vs 5 or 64 vs 6 starts to get
noticeable.

btw, speaking of machine instructions, is there anything like
a machine/bitstring.h (not named exactly that, I checked:))
or some other magic to use things like BFS on machines that
have it?

-Chap