CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Steve Hebert's Development Blog

Steve's Blog - From .Net to dotMath and everything in between.

Bit Counting Code Test - In one line!

My hat goes off to Adrian Florea for his one line solution to the bit counting test:

System.Convert.ToString(value, 2).Replace("0", string.Empty).Length

I hadn't considered this approach, I've always iterated through the byte testing the on bits.  That's a cool way to look at the problem and a nice use of the framework!

 



Comments

Basil said:

Try this:

static byte[] bits = new byte[256] { 0, 1, 1, 2, ... };

byte getBitCount(byte value)
{
return bits[value];
}
# November 3, 2004 7:51 PM
Check out Devlicio.us!

Our Sponsors

Free Tech Publications