import math


def _bit_length(n):
    """Return the number of bits necessary to store the number in binary."""
    try:
        return n.bit_length()
    except AttributeError:  # pragma: no cover (Python 2.6 only)
        return int(math.log(n, 2)) + 1