title: Finding set bits of an integer
body: <p>Okay, got a question. I have assembled a bit mask for options. Basically my page has a list box that allows multiple selections that stores them in a list of integers (their ID value). There are 14 total choices (so ID val 1-15). The reason I am assembling this into a bit mask is because I don't want to hard code in a number in case I want to add options to the database table (where the listbox populates from). Also, I do not want to be sending in 14 parameters to my SQL stored procedure (thus hardcoding in the number 14). I can send in this integer and deconstruction it (later step). </p>
  
  <p>However, right now I need to find out which bits are set in my integer for another reason. Basically I have a property. The get assembles the bit mask from a list of integers (gotten from user selection) and returns an integer of that binary decimal value. Here is my assembled code for the building of the bitmask.</p>
  
  <pre><code>//optsNum is my integer list. This is the list containing the ID nums of the selections.
  //so if the user selects the first, second, and fourth option, the list contains 1,2,4 (count 3)
  //typeCount is an integer of the amount of options in the list box
  int total = 0;
  for (int c = 0; c &lt; optsNum.Count(); ++c)
  {
      for (int i = 0; i &lt;= typeCount; i++)
      {
          if ((i + 1) == optsNum[c})
              total += (1 &lt;&lt; i);
      }
  }
  return total;
  </code></pre>
  
  <p>So if the first, second, and fourth are set, my integer is 11. This works, I tested for all selections and it is returning the correct integer/decimal value. </p>
  
  <p>Right now I need help making my set method. This needs to take the decimal/integer that I have, find out which bits are set and place those back into the list. So if I have 11 as my value, I need to put into a list of integers 1,2,4. Can anybody help me?</p>