title: how to decode integer bitmask to original input characters
body: <p>I am reading data from an application that stores optional input character flags as a packed integer (using binary packing). For sake of this discussion, use this example: <code>A=1, B=2, C=4, D=8, E=16</code> :<br>
  When the user inputs: <code>'AC'</code>, the stored value is <code>5 (=1+4)</code><br>
  When the user inputs: <code>'ABCD'</code>, the stored value is <code>15 (=1+2+4+8)</code><br>
  I'd like to recover the original inputs from the integer value (<code>5 =&gt; 'AC'</code> and <code>15 =&gt; 'ABCD'</code>). Case and order are not important.</p>
  
  <p>I'm new at this, so read up on bitmasks and bitwise operations and wrote some working code. Basically I convert the integer to a binary value formatted as a string of 0/1s. Then I check each string position (bit) for True/False (1/0) . When True, I add the matching character to another string (from an ordered string of the input options).  </p>
  
  <p>My intuition tells me there's a simpler way to do this; using the binary representation as a "mask" on the check string in a single operation. I found a couple of posts that touch on bitmasking, but don't answer my question:<br>
  <a href="https://stackoverflow.com/questions/53724160">Checking specific bits of a bitmask</a><br>
  <a href="https://stackoverflow.com/questions/56018322">How to take bit number 3 from 8 bits</a> </p>
  
  <p>Is it possible to simplify the code below?<br>
  It works for all values of i_flag = 1 thru 31.<br>
  [I know I need error checking for invalid i_flag values (<code>=0</code> and <code>&gt;(n_char**2)-1</code>)<br>
  I will add that once I have the conversion logic set.]</p>
  
  <pre><code>chk_str = 'EDCBA'
  i_flag = 1
  str_flag=''
  b_flag = ('{:0'+str(len(chk_str))+'b}').format(i_flag)
  for pos in range(len(b_flag)) :
     if int(b_flag[pos]):
         str_flag += chk_str[pos]
  
  print ('for int=', i_flag, ',flags are:',str_flag)      
  </code></pre>