...
| Code Block |
|---|
assert 42i.class == Integer //lowercase i more readable assert 123L.class == Long //uppercase L more readable assert 456g.class == BigInteger assert 0xFFi.class == Integer |
Fixed-Size Integers
The fixed-size integers, Integer and Long, each have size limits but are more efficient in calculations.
...
| Code Block |
|---|
assert 45i.compareTo( 47L ) < 0 assert (45 as byte).compareTo( 43 as short ) > 0 assert 45.compareTo( 45 ) == 0 |
Calculations with Fixed-Size Integers
We can perform addition, subtraction, multiplication, exponents, modulos, and negations on Integers and Longs, using both an operator syntax and a method syntax:
...
| Code Block |
|---|
def a = 4, b = 4, c = 5 assert a == b && a.equals(b) assert a != c && ! a.equals(c) assert (4 <=> 7) == -1 && 4.compareTo(7) == -1 assert (4 <=> 4) == 0 && 4.compareTo(4) == 0 assert (4 <=> 2) == 1 && 4.compareTo(2) == 1 |
Bit-Manipulation on Fixed-Sized Integers
We can examine and manipulate the individual bits on the fixed-sized integers.
...
| Code Block |
|---|
assert Integer.toString( 123456, 2 ) == '11110001001000000'
assert Integer.toString( Integer.reverse( 123456 ), 2 ) ==
'10010001111000000000000000' //reverse bits
assert Integer.reverseBytes( 0x157ACE42 ) == 0x42CE7A15 //also works for bytes
|
Boolean, Conditional, and Assignment Operators with Fixed-Sized Integers
The boolean, conditional, and assignment operators are of even lower precedence than the bitwise operators.
...
| Code Block |
|---|
def a = 7 a += 2 //short for a = a + 2 assert a == 9 a += (a = 3) //expands to a = a + (a = 3) before any part is evaluated assert a == 12 |
BigIntegers
The BigInteger has arbitrary precision, growing as large as necessary to accommodate the results of an operation.
...
| Code Block |
|---|
assert new BigInteger( 20, new Random() ).toString( 2 ).size() == 20
//20 is max bit length, must be >= 0
assert new BigInteger( 20, new Random() ) >= 0
|
Arithmetic with BigIntegers
We can perform the usual arithmetic operations + - * using either methods or operations:
...
| Code Block |
|---|
100.times{
def primes= [17g, 19g, 23g, 29g, 31g]
//bitlength is 5, so primes from 16 to 31 incl
assert new BigInteger( 5, 50, new Random() ) in primes
//5 is bit-length, 50 is certainty (must be integer)
}
def pp= BigInteger.probablePrime( 20, new Random() )
//if we don't want to specify certainty
//20 is bit-length; there's <1.0e-30 chance the number isn't prime
def pn= pp.nextProbablePrime()
//this is probably next prime, but definitely no primes skipped over
( (pp+1)..<pn ).each{
assert ! it.isProbablePrime(50)
//we can test for primality to specific certainty (here, 50).
//True if probably prime, false if definitely composite
}
assert 10g.nextProbablePrime() == 11
assert 0g.nextProbablePrime() == 2
|
Bit-Manipulation on BigIntegers
All operations behave as if BigIntegers were represented in two's-complement notation.
Bit operations operate on a single bit of the two's-complement representation of their operand/s. The infinite word size ensures that there are infinitely many virtual sign bits preceding each BigInteger. None of the single-bit operations can produce a BigInteger with a different sign from the BigInteger being operated on, as they affect only a single bit.
...