<p>So for e.g. 0110 has bits 1 and 2 set, 1000 has bit 3 set 1111 has bits 0,1,2,3 set</p>

[{'answer_id': 68978, 'author': 'Allan Wind', 'author_id': 9706, 'author_profile': 'https://Stackoverflow.com/users/9706', 'pm_score': 2, 'selected': False, 'text': '<p>I would shift it down and test the least significant bit in a loop.  It might be faster testing with 32 bit masks (or whatever length your unsigned int is).</p>\n\n<p>/Allan</p>\n'}, {'answer_id': 69007, 'author': 'Mladen Janković', 'author_id': 6300, 'author_profile': 'https://Stackoverflow.com/users/6300', 'pm_score': 2, 'selected': False, 'text': '<pre><code>for( int i = 0; variable ; ++i, variable &gt;&gt;= 1 ) {\n  if( variable &amp; 1 )\n    // store bit index - i\n}\n</code></pre>\n'}, {'answer_id': 69011, 'author': 'Ash', 'author_id': 5023, 'author_profile': 'https://Stackoverflow.com/users/5023', 'pm_score': 0, 'selected': False, 'text': '<p>Depends on what you mean by fastest.  </p>\n\n<p>If you mean "simple to code", in .NET you can use the BitArray class and refer to each bit as a boolean true/false.</p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/system.collections.bitarray.aspx" rel="nofollow noreferrer">BitArray Class</a></p>\n'}, {'answer_id': 69018, 'author': '1800 INFORMATION', 'author_id': 3146, 'author_profile': 'https://Stackoverflow.com/users/3146', 'pm_score': 3, 'selected': False, 'text': '<p>If there are really only 4 bits, then the fastest method would certainly involve a lookup table. There are only 16 different possibilities after all.</p>\n'}, {'answer_id': 69059, 'author': 'Brian R. Bondy', 'author_id': 3153, 'author_profile': 'https://Stackoverflow.com/users/3153', 'pm_score': 0, 'selected': False, 'text': "<p>@Allan Wind...</p>\n\n<p>The extra bit shifts are not needed.  It is more efficient to not do a bit shift, as comparing the least significant bit is just as efficient as comparing the 2nd least significant bit, and so on.  Doing a bit shift as well is just doubling the bit operations needed. </p>\n\n<pre><code>firstbit = (x &amp; 0x00000001) \nsecondbit = (x &amp; 0x00000002) \nthirdbit = (x &amp; 0x00000004)   //&lt;-- I'm not saying to store these values, just giving an example. \n...\n</code></pre>\n\n<p>All operations on an x86 system anyway are done with 32-bit registers, so a single bit compare would be just as efficient as a 32-bit compare.</p>\n\n<p>Not to mention the overhead of having the loop itself. </p>\n\n<p>The problem can be done in a constant number of lines of code and whether the code is run on an x86 or an x64, the way I describe is more efficient. </p>\n"}, {'answer_id': 69114, 'author': 'Philibert Perusse', 'author_id': 7984, 'author_profile': 'https://Stackoverflow.com/users/7984', 'pm_score': 3, 'selected': False, 'text': '<p>Best reference on the Internet for all those <strong>bit hacks</strong> - <a href="http://graphics.stanford.edu/~seander/bithacks.html" rel="nofollow noreferrer">bit twiddling hacks</a></p>\n'}, {'answer_id': 69935, 'author': 'freespace', 'author_id': 8297, 'author_profile': 'https://Stackoverflow.com/users/8297', 'pm_score': 0, 'selected': False, 'text': '<p>You can take the hybrid approach of iterating through the bytes of the int, use a lookup table to determine the indexes of the set bits in each byte (broken up into nibbles). Then you would need to add an offset to the indexes to reflect its position in the integer.</p>\n\n<p>i.e. Suppose you started with the MSB of a 32 bit int. The upper nibble indexes I will call upper_idxs, and the lower nibble indexes I will call lower_idxs. Then you need to add 24 to each element of lower_idxs, and add 28 to each element of upper_idxs. The next byte would be similarly processed, except the offsets would be 16 and 20 respectively, since that byte is 8 bits "down".</p>\n\n<p>To me this approach seems reasonable, but I would be happy to proven wrong :-)</p>\n'}, {'answer_id': 72286, 'author': 'Davy Landman', 'author_id': 11098, 'author_profile': 'https://Stackoverflow.com/users/11098', 'pm_score': 1, 'selected': False, 'text': '<p>If it was .NET and you\'d have to use it a lot I would like a nice fluent interface.</p>\n\n<p>I would create the following class (not totally happy with the name BitTools).</p>\n\n<pre><code>[Flags]\npublic enum Int32Bits\n{\n    // Lookup table but nicer\n    None = 0,\n    Bit1 = 1,        Bit2  = 1 &lt;&lt; 1,  Bit3  = 1 &lt;&lt; 2,  Bit4  = 1 &lt;&lt; 3,  Bit5  = 1 &lt;&lt; 4,  Bit6  = 1 &lt;&lt; 5,  Bit7  = 1 &lt;&lt; 6,  Bit8  = 1 &lt;&lt; 7,\n    Bit9  = 1 &lt;&lt; 8,  Bit10 = 1 &lt;&lt; 9,  Bit11 = 1 &lt;&lt; 10, Bit12 = 1 &lt;&lt; 11, Bit13 = 1 &lt;&lt; 12, Bit14 = 1 &lt;&lt; 13, Bit15 = 1 &lt;&lt; 14, Bit16 = 1 &lt;&lt; 15,\n    Bit17 = 1 &lt;&lt; 16, Bit18 = 1 &lt;&lt; 17, Bit19 = 1 &lt;&lt; 18, Bit20 = 1 &lt;&lt; 19, Bit21 = 1 &lt;&lt; 20, Bit22 = 1 &lt;&lt; 21, Bit23 = 1 &lt;&lt; 22, Bit24 = 1 &lt;&lt; 23,\n    Bit25 = 1 &lt;&lt; 24, Bit26 = 1 &lt;&lt; 25, Bit27 = 1 &lt;&lt; 26, Bit28 = 1 &lt;&lt; 27, Bit29 = 1 &lt;&lt; 28, Bit30 = 1 &lt;&lt; 29, Bit31 = 1 &lt;&lt; 30, Bit32 = 1 &lt;&lt; 31,\n}\n\npublic static class BitTools\n{\n    public static Boolean IsSet(Int32 value, Int32Bits bitToCheck)\n    {\n        return ((Int32Bits)value &amp; bitToCheck) == bitToCheck;\n    }\n\n    public static Boolean IsSet(UInt32 value, Int32Bits bitToCheck)\n    {\n        return ((Int32Bits)value &amp; bitToCheck) == bitToCheck;\n    }\n\n    public static Boolean IsBitSet(this Int32 value, Int32Bits bitToCheck)\n    {\n        return ((Int32Bits)value &amp; bitToCheck) == bitToCheck;\n    }\n    public static Boolean IsBitSet(this UInt32 value, Int32Bits bitToCheck)\n    {\n        return ((Int32Bits)value &amp; bitToCheck) == bitToCheck;\n    }\n}\n</code></pre>\n\n<p>And you could use it the following ways:</p>\n\n<pre><code>static void Main(string[] args)\n{\n    UInt32 testValue =  5557; //1010110110101;\n\n    if (BitTools.IsSet(testValue, Int32Bits.Bit1))\n    {\n        Console.WriteLine("The first bit is set!");\n    }\n    if (testValue.IsBitSet(Int32Bits.Bit5))\n    {\n        Console.WriteLine("The fifth bit is set!");\n    }\n    if (!testValue.IsBitSet(Int32Bits.Bit2))\n    {\n        Console.WriteLine("The second bit is NOT set!");\n    }\n}\n</code></pre>\n\n<p>For each (U)Int size you could make another Int*Bits enum and the correct overloads of IsSet and IsBitSet.</p>\n\n<p>EDIT: I misread, you\'re talking about unsigned ints, but it\'s the same in this case.</p>\n'}, {'answer_id': 78373, 'author': 'MSN', 'author_id': 6210, 'author_profile': 'https://Stackoverflow.com/users/6210', 'pm_score': 0, 'selected': False, 'text': '<p>Two steps:</p>\n\n<ol>\n<li><p>Extract each set bit with <code>set_bit= x &amp; -x; x&amp;= x - 1;</code></p></li>\n<li><p>Subtract 1 and count bits set.</p></li>\n</ol>\n'}, {'answer_id': 3082922, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>I think it will help</p>\n\n<pre><code>import java.util.*;\npublic class bitSet {\n\n    public static void main(String[]args) {\n        Scanner scnr = new Scanner(System.in);\n        int x = scnr.nextInt();\n        int i = 0;\n        while (i&lt;32) {\n            if ( ((x&gt;&gt;i)&amp;1) == 1) {\n                System.out.println(i);\n            }\n            i++;\n        }\n    }\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/68964', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/8884/']