Java: Convert from BigInteger to short

Use Number.shortValue:

BigInteger bigInt = ...
short s = bigInt.shortValue()

Note that this simply discards all but the lowest 16 bits. If the BigInteger is outside of the range −32,768…32,767 the conversion my have unexpected results.

BigInteger shortValue()
1,000 1,000
32,767 32,767
32,768 −32,768
32,769 −32,767
65,536 0
100,000 −31,072
−100,000 31,072

Comments

Be the first to comment!