Iterate the string bits
for i in range ( bits ) : NEW_LINE INDENT if i < setBits : NEW_LINE INDENT ans |= 1 NEW_LINE DEDENT if i != setBits - 1 : NEW_LINE INDENT ans <<= 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE