title: How can I reverse a bitwise operation to mark check boxes after page refresh
body: <p>I have a php function that has an array of all of these interest selections for users to choose from. I populate that data like so in a table:
      
  
  <pre><code>foreach($interests as $key=&gt;$value){
  ?&gt;
  &lt;input type="checkbox" name="targetInterests" id="targetInterests"  value="&lt;?php echo $key;?&gt;" class="addeditplacefields"&gt;
  &lt;?php echo $value;?&gt;
  &lt;?php   
  }
  ?&gt;
  </code></pre>
  
  <p>I am grabbing the values of the checked checkboxes as 1 for true and -1 for false, creating an array of the 1's and -1 and using a bitshift to store the values of those checked checkboxes in our mysql db like so:</p>
  
  <pre><code>var chkStr = "";
  $(":checkbox").each(function(){
      chkStr += this.checked ? "1," : "-1,";
  });
  var chkStr = chkStr.substr(0, chkStr.length - 1);
  // Turn checkbox string into an iterable array
  var target =  JSON.parse("[" + chkStr + "]");
  
  x = 0;
  for (i=0; i&lt;46; i++){
      var val = target[i];
      if (val == 1){
          x |= (1 &lt;&lt; i);
      }
  return x;
  </code></pre>
  
  <p>Bitwise operations are a new concept for me and I am struggling to find a neat solution that will repopulate those checked boxes after a user hits save and the page refreshes or when a user returns to the page to see what they have checked. Any help or suggestions would be much appreciated.</p>