[issue19915] int.bit_at(n) - Accessing a single bit in O(1)

anon report at bugs.python.org
Thu Oct 2 13:01:05 CEST 2014


anon added the comment:

@Georg: I don't think it would be as common but do agree it'd be useful.

I think it can be implemented "efficiently" in pure Python currently.

def with_bits(i, value, pos, width=1):
  width = min(width, value.bit_length())
  mask = ((1 << width) - 1)
  v = value & mask
  i = i & ~(mask << pos)
  return i | (v << pos)

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue19915>
_______________________________________


More information about the Python-bugs-list mailing list