Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

RE: [AVR-Chat] Exclusive or

2004-06-20 by Dave Hylands

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/

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.