def to_binary(n: int, width: int) -> str:
    """Formats ``n`` as exactly ``width`` binary digits, including when ``width`` is 0"""
    n = operator.index(n)
    width = operator.index(width)
    if n not in range(1 << width):
        raise ValueError(f"{n} does not fit in {width} bits")
    if width == 0:
        return ""
    return f"{n:0{width}b}"
Formats ``n`` as exactly ``width`` binary digits, including when ``width`` is 0
amaranth/_utils.py