def max_o(x, n, prevent=False):
    """
    Ensures that x fits on n bits (not bytes).

    - If prevent is True then an exception is raised
    - Otherwise just set all bits to 1

    Parameters
    ----------
    x
        Number to test
    n
        Number of bits available
    prevent
        If true then an exception will rise in case of overflow

    Returns
    -------
    A value which fits within the bit number constraint

    Raises
    ------
    ValueError
        If the value overflows and prevent is true
    """

    if x >= 1 << n:
        if prevent:
            raise ValueError
        else:
            return (1 << n) - 1

    return x