def inv(n: int, n_bits: int) -> int:
    """Compute the bitwise inverse.

    Args:
        n: An integer.
        n_bits: The bit-width of the integers used.

    Returns:
        The binary inverse of the input.
    """
    # We should only invert the bits that are within the bit-width of the
    # integers we use. We set this mask to set the other bits to zero.
    bit_mask = (1 << n_bits) - 1  # e.g. 0b111 for n_bits = 3

    return ~n & bit_mask