<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Another of my favorite tricks - reducing the number of bits in a number by 1</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx</link><description>One of the classic (and thus no longer asked) Microsoft interview questions is "How quickly can you count the bits in a 16 (or 32) bit integer?". You get a varied number of responses to this one, from brute force to lookup tables. One of my favorite tricks</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Another of my favorite tricks - reducing the number of bits in a number by 1</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#480770</link><pubDate>Thu, 13 Oct 2005 23:44:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:480770</guid><dc:creator>Anon, et cetera</dc:creator><description>How useful do you feel interview questions like the one mentioned above are? Why?&lt;br&gt;</description></item><item><title>re: Another of my favorite tricks - reducing the number of bits in a number by 1</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#480771</link><pubDate>Thu, 13 Oct 2005 23:44:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:480771</guid><dc:creator>Nicholas Allen</dc:creator><description>Interesting observation.  So I decided to think about why x &amp;amp; (x - 1) has one fewer bit set if x != 0.  Here's why:&lt;br&gt;&lt;br&gt;x != 0 so x has at least one bit set.&lt;br&gt;&lt;br&gt;From right-to-left, x has some number of 0 bits at the end (0-31), a 1 bit, and the rest of the bits.  Call the rest of the bits a.&lt;br&gt;&lt;br&gt;So, x looks like a10...0.&lt;br&gt;&lt;br&gt;x - 1 is going to flip all the 0 bits on the right until it can borrow the rightmost 1 bit.&lt;br&gt;&lt;br&gt;So, x - 1 looks like a01...1.&lt;br&gt;&lt;br&gt;a10...0 &amp;amp; a01...1 = a00...0, which is the same as x except we killed the rightmost 1 bit.  Thus, x &amp;amp; (x - 1) has one fewer set bit than x.</description></item><item><title>re: Another of my favorite tricks - reducing the number of bits in a number by 1</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#480783</link><pubDate>Fri, 14 Oct 2005 00:05:15 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:480783</guid><dc:creator>Aaron</dc:creator><description>Bitwise operations are fun.&lt;br&gt;&lt;br&gt;What you're doing, (x &amp;amp; (x - 1)), removes the lowest bit.&lt;br&gt;&lt;br&gt;My favorite one is (x &amp;amp; -x) which isolates the bottom bit.&lt;br&gt;&lt;br&gt;My most useful operation with this?  ((x &amp;amp; -x) == x)... returns true if x is a power of 2 (or equal to 0) -- very useful for quick assertions that the world is sane.&lt;br&gt;</description></item><item><title>re: Another of my favorite tricks - reducing the number of bits in a number by 1</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#480794</link><pubDate>Fri, 14 Oct 2005 00:29:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:480794</guid><dc:creator>LarryOsterman</dc:creator><description>Anon, I used to use this one exclusively for several years.&lt;br&gt;&lt;br&gt;The reason I like it is because it's a remarkably open-ended question - usually the interviewee starts with an interative loop that counts the bits taking 32 iterations.  Then you ask them to make it faster, which usually adds an early out.&lt;br&gt;&lt;br&gt;Then you ask them to make it still faster.  Now things get interesting.  A good candidate will pull into their bag of tricks and come up with a lookup table.  You then ask them how big the lookup table, and hopefully they'll realize that they're looking at a 4G lookup table and they can then figure out how to reduce the size of the table.&lt;br&gt;&lt;br&gt;Nicholas, thanks!</description></item><item><title>re: Another of my favorite tricks - reducing the number of bits in a number by 1</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#480811</link><pubDate>Fri, 14 Oct 2005 00:50:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:480811</guid><dc:creator>tzagotta</dc:creator><description>Interesting post.  Thanks Larry.</description></item><item><title>re: Another of my favorite tricks - reducing the number of bits in a number by 1</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#480822</link><pubDate>Fri, 14 Oct 2005 01:19:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:480822</guid><dc:creator>nar</dc:creator><description>A better interview question would be: Why does x = x &amp;amp; (x - 1) reduce the number of bits by one? Test it on yourself. :)&lt;br&gt;</description></item><item><title>re: Another of my favorite tricks - reducing the number of bits in a number by 1</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#480908</link><pubDate>Fri, 14 Oct 2005 05:32:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:480908</guid><dc:creator>Norman Diamond</dc:creator><description>Regarding the first trick:&lt;br&gt;&lt;br&gt;&amp;gt; It's a silly trick, but clever.&lt;br&gt;&lt;br&gt;Yes and yes.  But if the numeric values are distributed uniformly, then each bit will be as likely to be a 1 as a 0, so that loop will still iterate 16 times on average.  Of course there are reasons why the numeric values usually aren't distributed uniformly and usually less than half the bits are 1s, but your loop might still iterate more times than you were thinking.  A lookup table on bytes will still beat it.&lt;br&gt;&lt;br&gt;I wouldn't have thought of the trick that your colleagues came up with though.  Its cleverness outranks its silliness and that looks like amazing code.</description></item><item><title>re: Another of my favorite tricks - reducing the number of bits in a number by 1</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#480918</link><pubDate>Fri, 14 Oct 2005 06:16:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:480918</guid><dc:creator>vcv</dc:creator><description>Wouldn't this also work?&lt;br&gt;&lt;br&gt;{&lt;br&gt;	bitcount = 0;&lt;br&gt;	while (value != 0) &lt;br&gt;	{&lt;br&gt;		value &amp;gt;&amp;gt;= 1;&lt;br&gt;		bitcount++;&lt;br&gt;	}&lt;br&gt;	return bitcount;&lt;br&gt;}</description></item><item><title>re: Another of my favorite tricks - reducing the number of bits in a number by 1</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#480947</link><pubDate>Fri, 14 Oct 2005 08:54:04 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:480947</guid><dc:creator>Dominic Cronin</dc:creator><description>OK - I'll be the dummy :-)&lt;br&gt;&lt;br&gt;Why on earth would you want to count the number of bits in a number? Is this something you would do in a real situation, or just in Microsoft interviews? &lt;br&gt;&lt;br&gt;</description></item><item><title>re: Another of my favorite tricks - reducing the number of bits in a number by 1</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#481011</link><pubDate>Fri, 14 Oct 2005 15:13:09 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:481011</guid><dc:creator>denis</dc:creator><description>vcv: that gets the highest bit set.</description></item><item><title>re: Another of my favorite tricks - reducing the number of bits in a number by 1</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#481024</link><pubDate>Fri, 14 Oct 2005 15:55:09 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:481024</guid><dc:creator>Carlos</dc:creator><description>vcv wrote &amp;quot;Wouldn't this also work?&amp;quot;&lt;br&gt;&lt;br&gt;No.  It returns the position of the most significant 1 bit.&lt;br&gt;&lt;br&gt;The best solution to Larry's question &amp;quot;How quickly can you count the bits in a 16 (or 32) bit integer?&amp;quot; is:&lt;br&gt;&lt;br&gt;int CountBits(int value)&lt;br&gt;{&lt;br&gt;  return sizeof(value) * 8;&lt;br&gt;}&lt;br&gt;</description></item><item><title>re: Another of my favorite tricks - reducing the number of bits in a number by 1</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#481089</link><pubDate>Fri, 14 Oct 2005 18:16:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:481089</guid><dc:creator>Jonas Grumby</dc:creator><description>Does the &amp;quot;fastest known version&amp;quot;, beat all lookup-based solutions?  4GB might be excessive overhead just for bit counting, but byte-based tables/switches could be reasonable, and presumably faster (or am I wrong about that?).</description></item><item><title>re: Another of my favorite tricks - reducing the number of bits in a number by 1</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#481094</link><pubDate>Fri, 14 Oct 2005 18:31:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:481094</guid><dc:creator>BlackTigerX</dc:creator><description>&amp;gt;&amp;gt;Wouldn't this also work? &lt;br&gt;&lt;br&gt;wouldn't that return always the same value?</description></item><item><title>re: Another of my favorite tricks - reducing the number of bits in a number by 1</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#481098</link><pubDate>Fri, 14 Oct 2005 18:38:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:481098</guid><dc:creator>Anonymous</dc:creator><description>To the original question, I think a sharp interview candidate could have quickly answered immediately 16 (or 32).  Because you didn't ask him how to count how many bits *are set* in the integer.  Simply count the bits.  (grin).  &lt;br&gt;&lt;br&gt;&amp;quot;How quickly can you count the bits in a 16 (or 32) bit integer?&amp;quot;</description></item><item><title>re: Another of my favorite tricks - reducing the number of bits in a number by 1</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#481116</link><pubDate>Fri, 14 Oct 2005 19:35:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:481116</guid><dc:creator>LarryOsterman</dc:creator><description>Jonas, on most machines, I'm pretty sure that the bit twiddling version will be faster (but I've not tested it).  This is because the bit twidling version does its work entirely in registers, while a lookup table has to hit memory (to look values up in the table).  In general, going to memory is going to be slower than accessing data in registers.&lt;br&gt;&lt;br&gt;It takes about 15 instructions to do the shift and adds (even for a 32bit number), even if you unroll the loop and look up in an 8bit table, you're going to have 4 memory accesses to a 256 byte large table (which means you're likely to blow the L1 cache hitting the table).&lt;br&gt;&lt;br&gt;</description></item><item><title>How's this?</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#481117</link><pubDate>Fri, 14 Oct 2005 19:37:49 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:481117</guid><dc:creator>Maurits</dc:creator><description>Rough draft&lt;br&gt;Assumes Win32 (32-bit ints), fails on 64-bit machines&lt;br&gt;&lt;br&gt;I use leading underscores to preserve indenting (sorry, but this blog software eats leading spaces)&lt;br&gt;&lt;br&gt;// bits.h&lt;br&gt;int bitsinchar(unsigned char);&lt;br&gt;int bitsinint(unsigned int);&lt;br&gt;&lt;br&gt;// bits.c&lt;br&gt;int bitsinint(unsigned int i)&lt;br&gt;{&lt;br&gt;__// count char-by-char... assumes four-byte int&lt;br&gt;__// if this is a 64-bit int then extend the pattern accordingly&lt;br&gt;__return&lt;br&gt;____bitsinchar((char)(i &amp;amp; 0xff)) +&lt;br&gt;____bitsinchar((char)(i &amp;gt;&amp;gt; 8 &amp;amp; 0xff)) +&lt;br&gt;____bitsinchar((char)(i &amp;gt;&amp;gt; 16 &amp;amp; 0xff)) +&lt;br&gt;____bitsinchar((char)(i &amp;gt;&amp;gt; 24 &amp;amp; 0xff))&lt;br&gt;__;		&lt;br&gt;}&lt;br&gt;&lt;br&gt;// c must be an eight-bit char... no wchar's here&lt;br&gt;int bitsinchar(unsigned char c)&lt;br&gt;{&lt;br&gt;__// 256-entry lookup table&lt;br&gt;__select (c)&lt;br&gt;__{&lt;br&gt;____// eight bits&lt;br&gt;____case 0xff:&lt;br&gt;______return 8;&lt;br&gt;&lt;br&gt;____// seven bits&lt;br&gt;____case 0xfe:&lt;br&gt;____case 0xfd:&lt;br&gt;____// ... eight cases...&lt;br&gt;____case 0x7f:&lt;br&gt;______return 7;&lt;br&gt;&lt;br&gt;____// six bits... (eight-choose-two-cases)&lt;br&gt;&lt;br&gt;____// five bits... (eight-choose-three-cases)&lt;br&gt;&lt;br&gt;____// four bits... (eight-choose-four-cases)&lt;br&gt;&lt;br&gt;____// three bits... (eight-choose-three-cases)&lt;br&gt;&lt;br&gt;____// two bits... (eight-choose-two-cases)&lt;br&gt;&lt;br&gt;____// one bit...&lt;br&gt;____case 0x80:&lt;br&gt;____case 0x40:&lt;br&gt;____// more cases...&lt;br&gt;____case 0x02:&lt;br&gt;____case 0x01:&lt;br&gt;______return 1;&lt;br&gt;&lt;br&gt;____// zero bits&lt;br&gt;____case 0: return 0;&lt;br&gt;&lt;br&gt;&lt;br&gt;__}&lt;br&gt;&lt;br&gt;}&lt;br&gt;</description></item><item><title>OK, you answered my question... :)</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#481123</link><pubDate>Fri, 14 Oct 2005 19:55:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:481123</guid><dc:creator>Maurits</dc:creator><description>You answered my question while I was typing it out: :)&lt;br&gt;&amp;gt; even if you unroll the loop and look up in an 8bit table, you're going to have 4 memory accesses to a 256 byte large table (which means you're likely to blow the L1 cache hitting the table)</description></item><item><title>How about an 8-entry lookup table?</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#481128</link><pubDate>Fri, 14 Oct 2005 20:03:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:481128</guid><dc:creator>Maurits</dc:creator><description>typedef char nybble;&lt;br&gt;&lt;br&gt;int bitsinnybble(nybble n)&lt;br&gt;{&lt;br&gt;// 8-entry lookup table&lt;br&gt;__select (n)&lt;br&gt;__{&lt;br&gt;____// four bits&lt;br&gt;____case 0xf:&lt;br&gt;______return 4;&lt;br&gt;&lt;br&gt;____// three bits&lt;br&gt;____case 0xe:&lt;br&gt;____case 0xd:&lt;br&gt;____case 0xb:&lt;br&gt;____case 0x7:&lt;br&gt;______return 3;&lt;br&gt;&lt;br&gt;____// two bits&lt;br&gt;____case 0xc:&lt;br&gt;____case 0xa:&lt;br&gt;____case 0x9:&lt;br&gt;____case 0x6:&lt;br&gt;____case 0x5:&lt;br&gt;____case 0x3:&lt;br&gt;______return 3;&lt;br&gt;&lt;br&gt;____// one bit&lt;br&gt;____case 0x8:&lt;br&gt;____case 0x4:&lt;br&gt;____case 0x2:&lt;br&gt;____case 0x1:&lt;br&gt;______return 1;&lt;br&gt;&lt;br&gt;____// zero bits&lt;br&gt;____case 0:&lt;br&gt;______return 0;&lt;br&gt;}&lt;br&gt;&lt;br&gt;int bitsinchar(unsigned char c)&lt;br&gt;{&lt;br&gt;__return&lt;br&gt;____bitsinnybble((nybble)(c &amp;amp; 0xf)) +&lt;br&gt;____bitsinnybble((nybble)(c &amp;gt;&amp;gt; 4 &amp;amp; 0xf))&lt;br&gt;__;&lt;br&gt;}&lt;br&gt;</description></item><item><title>stupid bug fix</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#481150</link><pubDate>Fri, 14 Oct 2005 20:36:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:481150</guid><dc:creator>Maurits</dc:creator><description>Er, I mean, a 16-entry lookup table (not 8-entry)&lt;br&gt;Also, the two-bit cases should return 2, not 3</description></item><item><title>re: Another of my favorite tricks - reducing the number of bits in a number by 1</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#481155</link><pubDate>Fri, 14 Oct 2005 20:53:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:481155</guid><dc:creator>SomeBody</dc:creator><description>If you're going to count the bits in numerous bytes at a time, the lookup table will get cached and thus be extremely fast (assuming a small lookup table, not the 4GB one).  &lt;br&gt;&lt;br&gt;Even with counting a single byte, the lookup table version could be faster than the loop version depending on the characteristics of the data.  There are multiple instructions in the loop and the loop will get executed as many times as there are bits.  For larger numbers of bits, the loop version will have to execute more instructions than the memory access penalty of the lookup table -- memory access isn't *that* slow.&lt;br&gt;&lt;br&gt;I've heard this same discussion in the past and at one time benchmarked the loop version, the parallel version, and the lookup table version over a large set of integers (Pentium 4, VC7).  The lookup table version blew the others away.&lt;br&gt;</description></item><item><title>re: Another of my favorite tricks - reducing the number of bits in a number by 1</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#481226</link><pubDate>Fri, 14 Oct 2005 23:19:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:481226</guid><dc:creator>Mark Wan</dc:creator><description>I am interested to know the intent of asking questions like this in an interview situation.   If the intent is to make the candidate sweat and see how she reacts that is probably fine.  If the intent is to see how well they problem solve it might not work.  Prior knowledge of similar problems or problem patterns might affect the performance of a candidate.  I try to stay away from specific questions as a gauge for talent when I conduct interviews.&lt;br&gt;&lt;br&gt;Also fastest solution does not always mean fewest lines of code.  Sometimes fastest solution means tweaking the implementation to a point that it could not be well understood.  Well, as an  application developer I do not encourage such practices until there is a critical need for such implementation.  To me maintainability and code readability are more important than raw performance numbers.&lt;br&gt;&lt;br&gt;Mark.</description></item><item><title>re: Another of my favorite tricks - reducing the number of bits in a number by 1</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#481277</link><pubDate>Sat, 15 Oct 2005 02:20:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:481277</guid><dc:creator>Roozbeh Ghaffari</dc:creator><description>Didn't get a chance to test, but this should do the trick:&lt;br&gt;&lt;br&gt;input/output: eax&lt;br&gt;&lt;br&gt;		mov	ebx,55555555h&lt;br&gt;		mov	edx,eax&lt;br&gt;		and	edx,ebx&lt;br&gt;		shr	eax,1&lt;br&gt;		and	eax,ebx&lt;br&gt;		add	eax,edx&lt;br&gt;&lt;br&gt;		mov	ebx,33333333h&lt;br&gt;		mov	edx,eax&lt;br&gt;		and	edx,ebx&lt;br&gt;		shr	eax,2&lt;br&gt;		and	eax,ebx&lt;br&gt;		add	eax,edx&lt;br&gt;&lt;br&gt;		mov	ebx,0f0f0f0fh&lt;br&gt;		mov	edx,eax&lt;br&gt;		and	edx,ebx&lt;br&gt;		shr	eax,4&lt;br&gt;		and	eax,ebx&lt;br&gt;		add	eax,edx&lt;br&gt;&lt;br&gt;		mov	ebx,00ff00ffh&lt;br&gt;		mov	edx,eax&lt;br&gt;		and	edx,ebx&lt;br&gt;		shr	eax,8&lt;br&gt;		and	eax,ebx&lt;br&gt;		add	eax,edx&lt;br&gt;&lt;br&gt;		mov	ebx,0000ffffh&lt;br&gt;		mov	edx,eax&lt;br&gt;		and	edx,ebx&lt;br&gt;		shr	eax,16&lt;br&gt;		and	eax,ebx&lt;br&gt;		add	eax,edx&lt;br&gt;&lt;br&gt;anyone got a faster implementation?&lt;br&gt;</description></item><item><title>re: Another of my favorite tricks - reducing the number of bits in a number by 1</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#481291</link><pubDate>Sat, 15 Oct 2005 03:26:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:481291</guid><dc:creator>Roozbeh Ghaffari</dc:creator><description>Woohoo! Check this out:&lt;br&gt;&lt;br&gt;in/out: eax&lt;br&gt;&lt;br&gt;		mov edx,eax&lt;br&gt;		shr edx,1&lt;br&gt;		and edx,55555555h&lt;br&gt;		sub eax,edx&lt;br&gt;&lt;br&gt;		mov ebx,33333333h&lt;br&gt;		mov edx,eax&lt;br&gt;		shr edx,2&lt;br&gt;		and edx,ebx&lt;br&gt;		and eax,ebx&lt;br&gt;		add eax,edx&lt;br&gt;&lt;br&gt;		mov edx,eax&lt;br&gt;		shr edx,4&lt;br&gt;		add eax,edx&lt;br&gt;		and eax,0f0f0f0fh&lt;br&gt;&lt;br&gt;		mov ebx,01010101h&lt;br&gt;		mul ebx&lt;br&gt;		shr eax,24&lt;br&gt;</description></item><item><title>re: Another of my favorite tricks - reducing the number of bits in a number by 1</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#481313</link><pubDate>Sat, 15 Oct 2005 05:33:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:481313</guid><dc:creator>LarryOsterman</dc:creator><description>Somebody, Roozbeh put a version of the parallel algorithm in the comments.  It's possible that a lookup algorithm would be faster, but...&lt;br&gt;&lt;br&gt;When we did the numbers (about 10 years ago, on 386 hardware), the parallel algorithm blew the lookup algorithm away completely.&lt;br&gt;&lt;br&gt;With more modern processors, your results might be different.&lt;br&gt;</description></item><item><title>re: Another of my favorite tricks - reducing the number of bits in a number by 1</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#481330</link><pubDate>Sat, 15 Oct 2005 07:01:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:481330</guid><dc:creator>jeuge</dc:creator><description>Check the MIT HAKMEM count &lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/jeuge/archive/2005/06/08/HAKMEM_Bit_Count.aspx"&gt;http://blogs.msdn.com/jeuge/archive/2005/06/08/HAKMEM_Bit_Count.aspx&lt;/a&gt; </description></item><item><title>re: Another of my favorite tricks - reducing the number of bits in a number by 1</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#481409</link><pubDate>Sat, 15 Oct 2005 17:44:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:481409</guid><dc:creator>LarryOsterman</dc:creator><description>Jeu, my eyes are bleeding, and I'm just now picking my jaw off the floor.&lt;br&gt;&lt;br&gt;That was amazingly brilliant.  I defy ANYONE to write a table lookup version that's shorter than that.&lt;br&gt;</description></item><item><title>re: Another of my favorite tricks - reducing the number of bits in a number by 1</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#481570</link><pubDate>Sun, 16 Oct 2005 16:49:09 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:481570</guid><dc:creator>Ken Hirsch</dc:creator><description>There's a book, &amp;lt;a href=&amp;quot;&lt;a rel="nofollow" target="_new" href="http://www.amazon.com/gp/product/0201914654/&amp;quot;&amp;gt;Hacker"&gt;http://www.amazon.com/gp/product/0201914654/&amp;quot;&amp;gt;Hacker&lt;/a&gt;'s Delight by Henry Warren&amp;lt;/a&amp;gt;, that has tons of these tricks.&lt;br&gt;</description></item><item><title>re: Another of my favorite tricks - reducing the number of bits in a number by 1</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#481699</link><pubDate>Mon, 17 Oct 2005 06:50:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:481699</guid><dc:creator>Norman Diamond</dc:creator><description>Saturday, October 15, 2005 10:44 AM by LarryOsterman&lt;br&gt;&lt;br&gt;&amp;gt; Jeu, my eyes are bleeding, and I'm just now&lt;br&gt;&amp;gt; picking my jaw off the floor.&lt;br&gt;&lt;br&gt;You're going to drop your jaw again.  Look at the algorithm of your colleagues that you described (though didn't count) in your initial posting here.  4 of the operators are shifts.  Shifts can be slow, but how slow?  Let's compare.&lt;br&gt;&lt;br&gt;Look at the MIT HAKMEM translation into C as posted by Jeu George.  1 of the operators is an % operator, taking a remainder.  A remainder as in the result of a DIVIDE.  With a divider that is not a power of 2.  Divides are slow.  Slower than 4 shifts?  I think yes, usually.</description></item><item><title>re: Another of my favorite tricks - reducing the number of bits in a number by 1</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#481829</link><pubDate>Mon, 17 Oct 2005 17:54:35 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:481829</guid><dc:creator>michael</dc:creator><description>I generally prefer interview questions that prove some basic understanding of math as well as sound programming skills.&lt;br&gt;&lt;br&gt;My favorite today is the not so complex ...&lt;br&gt;&amp;quot;Write a function for calculating the prime factors of any given integer.&amp;quot;&lt;br&gt;&lt;br&gt;Not too complex, but it shows how the person thinks and approaches code very easily.&lt;br&gt;&lt;br&gt;- Michael</description></item><item><title>re: Another of my favorite tricks - reducing the number of bits in a number by 1</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#481927</link><pubDate>Mon, 17 Oct 2005 21:57:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:481927</guid><dc:creator>Roozbeh Ghaffari</dc:creator><description>That was amazing Jeu! In assembly it's even shorter than the one that uses multiplication. I'm not sure about today's machines, but in the old days I remember DIV being much slower than MUL. That might compensate for the few extra instructions, but who cares! ;)&lt;br&gt;</description></item><item><title>256-entry lookup table is the fastest</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#482059</link><pubDate>Tue, 18 Oct 2005 03:28:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:482059</guid><dc:creator>Maurits</dc:creator><description>According to this 2002 comparison of several bitcounting algorithms...&lt;br&gt;&lt;a rel="nofollow" target="_new" href="http://www-db.stanford.edu/~manku/bitcount/bitcount.html"&gt;http://www-db.stanford.edu/~manku/bitcount/bitcount.html&lt;/a&gt;&lt;br&gt;&lt;br&gt;A 256-entry lookup table is the fastest.</description></item><item><title>Oops, I misread...</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#482060</link><pubDate>Tue, 18 Oct 2005 03:30:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:482060</guid><dc:creator>Maurits</dc:creator><description>Actually the results of the comparison are that a  65Kb lookup table is the fastest.&lt;br&gt;&lt;br&gt;I presume that the initialization of the lookup table is outweighed by the number of bits counted, though.</description></item><item><title>re: Another of my favorite tricks - reducing the number of bits in a number by 1</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#486261</link><pubDate>Fri, 28 Oct 2005 19:10:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:486261</guid><dc:creator>Wally Bass</dc:creator><description>Counting &amp;quot;1&amp;quot; bits is probably not all that common, but checking whether a number is a power of 2 does occur with some frequency (in verifying a valid &amp;quot;sectors per cluster&amp;quot; value for example). Checking whether a number is a power of 2 is the same as checking whether it has exactly 1 &amp;quot;1&amp;quot; bit.&lt;br&gt;&lt;br&gt;Is there a better way to check that a number has exactly 1 &amp;quot;1&amp;quot; bit than using an approach&lt;br&gt;derived from your algorithm?</description></item><item><title>re: Another of my favorite tricks - reducing the number of bits in a number by 1</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#504891</link><pubDate>Sat, 17 Dec 2005 02:53:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:504891</guid><dc:creator>David Walker</dc:creator><description>When you say &amp;quot;I'm not sure exactly why it works, and it only works for 2s complement numbers, but it DOES work.&amp;quot;&lt;br&gt;&lt;br&gt;Umm, you mean it only works when negative numbers are stored as the twos complement of the same positive number?  It sounds like you're talkking about a set of &amp;quot;2s complement numbers&amp;quot; that maybe not all numbers fall into.&lt;br&gt;&lt;br&gt;Also, somewhere in a related note, someone said that a lookup table would work, except if you're adding the bits that are &amp;quot;on&amp;quot; in a 32-bit fullword, you'd need a huge lookup table.  I would simply think of the 32-bit word as eight 4-bit chunks, and use a 16-entry lookup table for each of the 4-bit chunks.  Eight lookups and eight additions, plus some looping overhead.&lt;br&gt;&lt;br&gt;In fact, I solved the same problem that way in production IBM mainframe code many years ago.  (This was for something that was infrequently used... In fact, it was used once per customer to count something, and then a flag was set to never call the routine again.)&lt;br&gt;&lt;br&gt;David W.</description></item><item><title>re: Another of my favorite tricks - reducing the number of bits in a number by 1</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#504893</link><pubDate>Sat, 17 Dec 2005 02:59:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:504893</guid><dc:creator>David Walker</dc:creator><description>Edit -- it wasn't someone somewhere, it was you, Larry, in a comment in this blog.  Duh.  I thought that's where I read it, then I missed it when I looked, oh well. &lt;br&gt;&lt;br&gt;Spending scads of time optimizing a function like this might not be a good use of time -- depending on the use of course.  We decided that our 4-bit-at-a-time lookup function was the perfect balance between being simple to write and (importantly) to understand, and being plenty fast.</description></item><item><title> Larry Osterman s WebLog Another of my favorite tricks reducing the | Patio Chairs</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#9688656</link><pubDate>Wed, 03 Jun 2009 05:26:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9688656</guid><dc:creator> Larry Osterman s WebLog Another of my favorite tricks reducing the | Patio Chairs</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://patiochairsite.info/story.php?id=29037"&gt;http://patiochairsite.info/story.php?id=29037&lt;/a&gt;&lt;/p&gt;
</description></item><item><title> Larry Osterman s WebLog Another of my favorite tricks reducing the | patio set</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#9771743</link><pubDate>Thu, 18 Jun 2009 05:16:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9771743</guid><dc:creator> Larry Osterman s WebLog Another of my favorite tricks reducing the | patio set</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://patiosetsite.info/story.php?id=160"&gt;http://patiosetsite.info/story.php?id=160&lt;/a&gt;&lt;/p&gt;
</description></item><item><title> Larry Osterman s WebLog Another of my favorite tricks reducing the | patio umbrella</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#9772556</link><pubDate>Thu, 18 Jun 2009 07:06:23 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9772556</guid><dc:creator> Larry Osterman s WebLog Another of my favorite tricks reducing the | patio umbrella</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://patioumbrellasource.info/story.php?id=1777"&gt;http://patioumbrellasource.info/story.php?id=1777&lt;/a&gt;&lt;/p&gt;
</description></item><item><title> Larry Osterman s WebLog Another of my favorite tricks reducing the | debt solutions</title><link>http://blogs.msdn.com/larryosterman/archive/2005/10/13/480758.aspx#9791293</link><pubDate>Fri, 19 Jun 2009 20:53:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9791293</guid><dc:creator> Larry Osterman s WebLog Another of my favorite tricks reducing the | debt solutions</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://debtsolutionsnow.info/story.php?id=1308"&gt;http://debtsolutionsnow.info/story.php?id=1308&lt;/a&gt;&lt;/p&gt;
</description></item></channel></rss>