Re: Dr. Dobbs Dev. Update 1/5 July 94 & Schneier
Date: Sun, 3 Jul 1994 01:24:39 -0700 From: norm@netcom.com (Norman Hardy) At 01:00 1994/07/03 -0400, tim werner wrote:
... I had to write a little loop to check the number of bits set in a word. ... The loop was really a waste when you consider that it could have been done in 1 instruction.
In C, {int j... if(j & (j-1)) not_exactly_one_bit; ...}
I knew if I subscribed to this list I'd eventually pick up something useful. I already asked a couple of people at work if they knew of a trick to see if there was just one bit set, and no one did. Otherwise I would feel extra stupid. :-) tw
In C, {int j... if(j & (j-1)) not_exactly_one_bit; ...}
C code that does bit twiddling should almost always use unsigned rather than signed integers, or you may get some nasty surprises on C implementations that do not use two's complement representation for signed integers. --apb (Alan Barrett)
participants (2)
-
Alan Barrett -
tim werner