def encode_binary(x, width):
    """Convert integer x to binary with at least width digits."""
    assert isinstance(x, int)
    xb = bin(x)[2:]
    if width == 0:
        assert x == 0
        return ''
    else:
        assert len(xb) <= width
        pad = width  - len(xb)
        return '0' * pad + xb