Parsing of list of bits in python
<p>I have a list of 64 bits.</p>

<pre><code>['11111111', '11111111', '10001111', '11111110', '11011011', '11111111', '01011011', '10000111']
</code></pre>

<p>Here each and every bit represents a feature. If a bit is 1 then that particular feature is supported else not.</p>

<p>Is there any way were i can check the bit and just return its associated feature if it is 1.</p>

<p><code>Input : 1111111</code></p>

<p><code>Output: If all bits are one then I need to print all eight features masked in it.</code></p>

<p>I guess you could do something like:</p>

<pre><code>bits = ['11111111', '11111111', '10001111', '11111110', '11011011', '11111111', '01011011', '10000111']

# to produce features mapping dynamically:
# features = {i:"feature " + str(i) for i in range(0,64)}
features = {0: 'feature 0', 1: 'feature 1', 2: 'feature 2', 3: 'feature 3', 4: 'feature 4', 5: 'feature 5', 6: 'feature 6', 7: 'feature 7', 8: 'feature 8', 9: 'feature 9', 10: 'feature 10', 11: 'feature 11', 12: 'feature 12', 13: 'feature 13', 14: 'feature 14', 15: 'feature 15', 16: 'feature 16', 17: 'feature 17', 18: 'feature 18', 19: 'feature 19', 20: 'feature 20', 21: 'feature 21', 22: 'feature 22', 23: 'feature 23', 24: 'feature 24', 25: 'feature 25', 26: 'feature 26', 27: 'feature 27', 28: 'feature 28', 29: 'feature 29', 30: 'feature 30', 31: 'feature 31', 32: 'feature 32', 33: 'feature 33', 34: 'feature 34', 35: 'feature 35', 36: 'feature 36', 37: 'feature 37', 38: 'feature 38', 39: 'feature 39', 40: 'feature 40', 41: 'feature 41', 42: 'feature 42', 43: 'feature 43', 44: 'feature 44', 45: 'feature 45', 46: 'feature 46', 47: 'feature 47', 48: 'feature 48', 49: 'feature 49', 50: 'feature 50', 51: 'feature 51', 52: 'feature 52', 53: 'feature 53', 54: 'feature 54', 55: 'feature 55', 56: 'feature 56', 57: 'feature 57', 58: 'feature 58', 59: 'feature 59', 60: 'feature 60', 61: 'feature 61', 62: 'feature 62', 63: 'feature 63'}

for ind, bit in enumerate(reversed("".join(bits))):
    if bit == '1':
        print "Feature " + features[ind] + " turned on"
</code></pre>

<p>Where using:</p>

<ul>
<li><code>reversed</code> - because bits are read from LSB to MSB so we need to iterate from right to left.</li>
<li><code>enumerate</code> - because we want to get the associated index to check in our mapping what is the feature name/attributes.</li>
<li><code>"".join(bits)</code> - because we want to be able to join all the items on the list to one iterable object we can go through.</li>
</ul>

python python-2.7