Hi Paul,
> > If the expression ( x & ( x - 1 )) yields zero, then x is a
> > power of two.
>
> To be completely pedantic, that isn't quite true when x==0 as
> zero is not a power of two.
Yep - that is I agree with you.
> What you could also point out is that x & (x-1) removes the
> rightmost 1 bit of x and can easily be used to accelerate a
> "bit count" subroutine:
>
> unsigned bitcnt(unsigned x)
> {
> unsigned n;
> if (x == 0)
> return 0;
> n = 1;
> while ((x &= x-1) != 0)
> ++n;
> return n;
> }
Very cool. I never thought of that particular application.
--
Dave Hylands
Vancouver, BC, Canada
http://www.DaveHylands.com/Message
RE: [AVR-Chat] Exclusive or
2004-06-20 by Dave Hylands
Attachments
- No local attachments were found for this message.