Java Code Examples for java.math.BigInteger#shortValue()

The following examples show how to use java.math.BigInteger#shortValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OperatorUtils.java    From hasor with Apache License 2.0 6 votes vote down vote up
private static Number newReal(int realType, BigInteger value) {
    switch (realType) {
    case BOOL:
        return BigInteger.ZERO.compareTo(value) == 0 ? 0 : 1;
    case BYTE:
        return value.byteValue();
    case SHORT:
        return value.shortValue();
    case CHAR:
    case INT:
        return value.intValue();
    case LONG:
        return value.longValue();
    default:
        return value;
    }
}
 
Example 2
Source File: CastUInt64ToUInt8Test.java    From zserio with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void uint8ValueUsingUInt64Value()
{
    final BigInteger uint64Value = new BigInteger("FFFFFFFFFFFFFFFE", 16);
    final CastUInt64ToUInt8Expression castUInt64ToUInt8Expression =
            new CastUInt64ToUInt8Expression(uint64Value, false);
    final short expectedUInt8Value = uint64Value.shortValue();
    assertEquals(expectedUInt8Value, castUInt64ToUInt8Expression.funcUint8Value());
}
 
Example 3
Source File: StringUtil.java    From mumu with Apache License 2.0 5 votes vote down vote up
public static String baseString(BigInteger num, int base) {
	String str = "", digit = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	if (num.shortValue() == 0) {
		return "";
	} else {
		BigInteger valueOf = BigInteger.valueOf(base);
		str = baseString(num.divide(valueOf), base);
		return str + digit.charAt(num.mod(valueOf).shortValue());
	}
}
 
Example 4
Source File: TlvDecoder.java    From SI with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Decodes a byte array into an integer value.
 */
public static Number decodeInteger(byte[] value) throws TlvException {
    BigInteger bi = new BigInteger(value);
    if (value.length == 1) {
        return bi.byteValue();
    } else if (value.length <= 2) {
        return bi.shortValue();
    } else if (value.length <= 4) {
        return bi.intValue();
    } else if (value.length <= 8) {
        return bi.longValue();
    } else {
        throw new TlvException("Invalid length for an integer value: " + value.length);
    }
}
 
Example 5
Source File: TlvDecoder.java    From SI with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Decodes a byte array into an integer value.
 */
public static Number decodeInteger(byte[] value) throws TlvException {
    BigInteger bi = new BigInteger(value);
    if (value.length == 1) {
        return bi.byteValue();
    } else if (value.length <= 2) {
        return bi.shortValue();
    } else if (value.length <= 4) {
        return bi.intValue();
    } else if (value.length <= 8) {
        return bi.longValue();
    } else {
        throw new TlvException("Invalid length for an integer value: " + value.length);
    }
}
 
Example 6
Source File: Ip6.java    From batfish with Apache License 2.0 5 votes vote down vote up
private static String asIpv6AddressString(BigInteger ipv6AddressAsBigInteger) {
  BigInteger remainder = ipv6AddressAsBigInteger;
  String[] pieces = new String[8];
  for (int i = 0; i < 8; i++) {
    int mask = (int) remainder.shortValue() & 0xFFFF;
    pieces[7 - i] = String.format("%x", mask);
    remainder = remainder.shiftRight(16);
  }
  return StringUtils.join(pieces, ":");
}
 
Example 7
Source File: DefaultFunctions.java    From jackcess with Apache License 2.0 4 votes vote down vote up
@Override
protected Value eval1(EvalContext ctx, Value param1) {

  // strip all whitespace from string
  String str = ValueSupport.WHITESPACE_PAT.matcher(param1.getAsString(ctx))
    .replaceAll("");

  if(str.length() == 0) {
    return ValueSupport.ZERO_D_VAL;
  }

  Matcher m = null;

  if(str.charAt(0) == ValueSupport.NUMBER_BASE_PREFIX) {

    // see if we can parse as a radix format
    BigInteger bi = null;
    if((m = ValueSupport.HEX_PAT.matcher(str)).find()) {
      bi = ValueSupport.parseIntegerString(m.group(), 16);
    } else if((m = ValueSupport.OCTAL_PAT.matcher(str)).find()) {
      bi = ValueSupport.parseIntegerString(m.group(), 8);
    }

    if(bi != null) {
      // this function works differently than normal string to number
      // conversion.  it seems to coerce these values to a short/long int
      // depending on the size of the number (which creates
      // positive/negative values dependent on the value length)
      int iVal = ((bi.bitLength() <= 16) ? bi.shortValue() : bi.intValue());
      return ValueSupport.toValue((double)iVal);
    }

  } else {

    // parse as normal "decimal" number.
    if((m = ValueSupport.NUMBER_PAT.matcher(str)).find()) {
      BigDecimal bd = new BigDecimal(m.group());
      return ValueSupport.toValue(bd.doubleValue());
    }
  }

  return ValueSupport.ZERO_D_VAL;
}
 
Example 8
Source File: Main.java    From Java-Coding-Problems with MIT License 3 votes vote down vote up
public static void main(String[] args) {

        BigInteger nr = BigInteger.valueOf(Long.MAX_VALUE);
               
        long nrLong = nr.longValue();        
        System.out.println(nr + " as long is: " + nrLong);
        
        int nrInt = nr.intValue();
        System.out.println(nr + " as int is: " + nrInt);
        
        short nrShort = nr.shortValue();
        System.out.println(nr + " as short is: " + nrShort);
        
        byte nrByte = nr.byteValue();                                
        System.out.println(nr + " as byte is: " + nrByte);
                
        long nrExactLong = nr.longValueExact(); // ok       
        System.out.println(nr + " as exact long is: " + nrExactLong);
        
        int nrExactInt = nr.intValueExact(); // ArithmeticException
        System.out.println(nr + " as exact int is: " + nrExactInt);
        
        short nrExactShort = nr.shortValueExact(); // ArithmeticException
        System.out.println(nr + " as exact short is: " + nrExactShort);
        
        byte nrExactByte = nr.byteValueExact(); // ArithmeticException                        
        System.out.println(nr + " as exact byte is: " + nrExactByte);
    }
 
Example 9
Source File: UShort.java    From CPE552-Java with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Throw exception if value out of range (long version)
 *
 * @param value Value to check
 * @return value if it is in range
 * @throws ArithmeticException if value is out of range
 */
public static short checkSigned(BigInteger value) throws ArithmeticException {
    if (value.compareTo(BigInteger.ZERO) < 0 || value.intValue() > MAX_VALUE) {
        throw new ArithmeticException("Value is out of range : " + value);
    }
    return value.shortValue();
}
 
Example 10
Source File: ULong.java    From CPE552-Java with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Throw exception if value out of range (long version)
 *
 * @param value Value to check
 * @return value if it is in range
 * @throws ArithmeticException if value is out of range
 */
public static short checkSigned(BigInteger value) throws ArithmeticException {
    if (value.compareTo(BigInteger.ZERO) < 0 || value.intValue() > MAX_VALUE) {
        throw new ArithmeticException("Value is out of range : " + value);
    }
    return value.shortValue();
}
 
Example 11
Source File: UShort.java    From glm with MIT License 3 votes vote down vote up
/**
 * Throw exception if value out of range (long version)
 *
 * @param value Value to check
 * @return value if it is in range
 * @throws ArithmeticException if value is out of range
 */
public static short checkSigned(BigInteger value) throws ArithmeticException {
    if (value.compareTo(BigInteger.ZERO) < 0 || value.intValue() > MAX_VALUE) {
        throw new ArithmeticException("Value is out of range : " + value);
    }
    return value.shortValue();
}
 
Example 12
Source File: ULong.java    From glm with MIT License 3 votes vote down vote up
/**
 * Throw exception if value out of range (long version)
 *
 * @param value Value to check
 * @return value if it is in range
 * @throws ArithmeticException if value is out of range
 */
public static short checkSigned(BigInteger value) throws ArithmeticException {
    if (value.compareTo(BigInteger.ZERO) < 0 || value.intValue() > MAX_VALUE) {
        throw new ArithmeticException("Value is out of range : " + value);
    }
    return value.shortValue();
}
 
Example 13
Source File: UShort.java    From CPE552-Java with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Create an <code>unsigned short</code>
 *
 * @param value
 */
public UShort(BigInteger value) {
    this.value = value.shortValue();
}
 
Example 14
Source File: UShort.java    From glm with MIT License 2 votes vote down vote up
/**
 * Create an <code>unsigned short</code>
 *
 * @param value
 */
public UShort(BigInteger value) {
    this.value = value.shortValue();
}