def bitstobytes(bits):
 ret = []
 nextbyte = 0
 nextbit = 7
 for bit in bits:
  if bit:
   nextbyte = (nextbyte | (1 << nextbit))
  if nextbit:
   nextbit = (nextbit - 1)
  else:
   ret.append(nextbyte)
   nextbit = 7
   nextbyte = 0
 if (nextbit < 7):
  ret.append(nextbyte)
 return ret

def bitstobytes ( bits ) : ret = [ ] nextbyte = 0 nextbit = 7 for bit in bits : if bit : nextbyte = ( nextbyte | ( 1 << nextbit ) ) if nextbit : nextbit = ( nextbit - 1 ) else : ret . append ( nextbyte ) nextbit = 7 nextbyte = 0 if ( nextbit < 7 ) : ret . append ( nextbyte ) return ret
interprets an indexable list of booleans as bits .