Calculate min/max values for (un)signed integral number of up to 64 bits This question is a follow up of Calculate min/max values for signed or unsigned integral number with any valid number of bits (up to 64)


I was answering the question Given two int values, return the one closer to 10, and I indicated that something like the following snippet of code could be a different approach for solving the problem.

In the original question, it turned out I had made a mistake, but since there were already some answers, I've decided to post a follow up question with the fixes.

Can this be done in a more efficient or more elegant way?

public class Bits
{
    public static void MinMaxI(ulong _bits, out long _min, out long _max)
    {
        // ? unsigned range with at least one value bit
        if (_bits &lt; 2 || _bits &gt; 64)
        {
            throw new System.ArgumentOutOfRangeException("_bits", _bits,
             String.Format("2 &lt;= _bits &lt;= 64"));
        }

        _min = ~(long)0 &lt;&lt; (int)(_bits - 1);
        _max = -1 - _min;
    }


    public static long MinI(ulong _bits)
    {
        // ? unsigned range with at least one value bit
        if (_bits &lt; 2 || _bits &gt; 64)
        {
            throw new System.ArgumentOutOfRangeException("_bits", _bits,
             String.Format("2 &lt;= _bits &lt;= 64"));
        }

        return ~(long)0 &lt;&lt; (int)(_bits - 1);
    }


    public static long MaxI(ulong _bits)
    {
        // ? unsigned range with at least one value bit
        if (_bits &lt; 2 || _bits &gt; 64)
        {
            throw new System.ArgumentOutOfRangeException("_bits", _bits,
             String.Format("2 &lt;= _bits &lt;= 64"));
        }

        return -1 - (~(long)0 &lt;&lt; (int)(_bits - 1));
    }


    public static ulong MaxN(ulong _bits)
    {
        // ? unsigned range with at least one value bit
        if (_bits &lt; 1 || _bits &gt; 64)
        {
            throw new System.ArgumentOutOfRangeException("_bits", _bits,
             String.Format("1 &lt;= _bits &lt;= 64"));
        }

        return _bits == 64 ? ~(ulong)0 : ~(~(ulong)0 &lt;&lt; (int)_bits);
    }
}
Your argument validation could be de-duplicated.

Here, take a look:

MinMaxI:
    if (_bits &lt; 2 || _bits &gt; 64)
    {
        throw new System.ArgumentOutOfRangeException("_bits", _bits,
         String.Format("2 &lt;= _bits &lt;= 64"));
    }

MinI:
    if (_bits &lt; 2 || _bits &gt; 64)
    {
        throw new System.ArgumentOutOfRangeException("_bits", _bits,
         String.Format("2 &lt;= _bits &lt;= 64"));
    }

MaxI:
    if (_bits &lt; 2 || _bits &gt; 64)
    {
        throw new System.ArgumentOutOfRangeException("_bits", _bits,
         String.Format("2 &lt;= _bits &lt;= 64"));
    }

MaxN:
    if (_bits &lt; 1 || _bits &gt; 64)
    {
        throw new System.ArgumentOutOfRangeException("_bits", _bits,
         String.Format("1 &lt;= _bits &lt;= 64"));
    }


They're practically the same!

I'd make a method like this (I'm not really familiar with C# syntax, so let me know if I made a mistake)

private static void validateBitsArgument(ulong _bits, ulong min, ulong max)
{
    if (_bits &lt; min || _bits &gt; max)
    {
        throw new System.ArgumentOutOfRangeException("_bits", _bits,
         String.Format("{0} &lt;= _bits &lt;= {1}", min, max));
    }
}


And then include it like this:

public class Bits
{
    public static void MinMaxI(ulong _bits, out long _min, out long _max)
    {
        validateBitsArgument(_bits, 2, 64);

        _min = ~(long)0 &lt;&lt; (int)(_bits - 1);
        _max = -1 - _min;
    }


    public static long MinI(ulong _bits)
    {
        validateBitsArgument(_bits, 2, 64);

        return ~(long)0 &lt;&lt; (int)(_bits - 1);
    }


    public static long MaxI(ulong _bits)
    {
        validateBitsArgument(_bits, 2, 64);

        return -1 - (~(long)0 &lt;&lt; (int)(_bits - 1));
    }


    public static ulong MaxN(ulong _bits)
    {
        validateBitsArgument(_bits, 1, 64);

        return _bits == 64 ? ~(ulong)0 : ~(~(ulong)0 &lt;&lt; (int)_bits);
    }

    private static void validateBitsArgument(ulong _bits, ulong min, ulong max)
    {
        if (_bits &lt; min || _bits &gt; max)
        {
            throw new System.ArgumentOutOfRangeException("_bits", _bits,
         String.Format("{0} &lt;= _bits &lt;= {1}", min, max));
        }
    }
}


And voila, shorter code.