Java Number Min Value minSignedIntForBitSize(final int bitSize)

Here you can find the source of minSignedIntForBitSize(final int bitSize)

Description

min Signed Int For Bit Size

License

Open Source License

Parameter

Parameter Description
bitSize A number of bits in [1,32].

Exception

Parameter Description
IllegalArgumentException if the specified number of bits is out of range.

Return

The min signed int value that can be stored over the specified number of bits.

Declaration

public static int minSignedIntForBitSize(final int bitSize) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*www  .  ja  v a2s . com*/
     * @param bitSize
     *            A number of bits in [1,32].
     * @return The min signed int value that can be stored over the specified number of bits.
     * @throws IllegalArgumentException
     *             if the specified number of bits is out of range.
     */
    public static int minSignedIntForBitSize(final int bitSize) {
        checkBitSizeForSignedInt(bitSize);
        return minSignedIntForBitSize_noCheck(bitSize);
    }

    /**
     * @return True if does not throw.
     * @throws IllegalArgumentException
     *             if a signed int value can't be read over the specified number of bits, i.e. if it is not in [1,32].
     */
    public static boolean checkBitSizeForSignedInt(final int bitSize) {
        if (!isValidBitSizeForSignedInt(bitSize)) {
            throw new IllegalArgumentException(
                    "bit size [" + bitSize + "] must be in [1,32] for signed int values");
        }
        return true;
    }

    private static int minSignedIntForBitSize_noCheck(final int bitSize) {
        // i.e. (-1<<(bitSize-1))
        return Integer.MIN_VALUE >> 32 - bitSize;
    }

    /**
     * @return True if a signed int value can be read over the specified number of bits, i.e. if it is in [1,32], false
     *         otherwise.
     */
    public static boolean isValidBitSizeForSignedInt(final int bitSize) {
        return bitSize > 0 && bitSize <= 32;
    }
}

Related

  1. minPos(int a, int b)
  2. minPositive(int a, int b)
  3. minPower2(int n)
  4. minRunLength(int n)
  5. minShareLifetime(long shareLifetime1, long shareLifetime2)
  6. minSignedIntForBitSize_noCheck(final int bitSize)
  7. minSize(String str, int min, boolean nullCheck)
  8. minSPSForBandCode(String bandCode)
  9. minToS(long s)