An exact representation of a positive whole number, negative whole number, or zero. The largest and smallest representable values are platform-dependent:

  • For the JVM runtime, integer values between -263 and 263-1 may be represented without overflow.
  • For the JavaScript runtime, integer values with a magnitude no greater than 253 may be represented without loss of precision.

Overflow or loss of precision occurs silently (with no exception raised).

An integer is considered equal to its float representation, if that exists. That is, for every integer int, either int.float throws an OverflowException, or the expression int.float==int evaluates to true.

An integer is representable as a sequence of bits. Not all of the bits in the representation may be addressed by the methods inherited from Binary:

  • For the JVM runtime, the bits at all indices (0 to 63) are addressable.
  • For the JavaScript runtime, the bits at indices 0 to 31 are addressable.

Literal integers may be written in decimal, hexadecimal, or binary notation:

8660
#21D4
$10000111010100

Underscores may be used to group digits:

8660
#21_D4
$10_0001_1101_0100

no subtypes hierarchy

Constructors
IntegerSource Codeshared Integer(Integer integer)
Attributes
absoluteSee magnitude
byteSource Codeshared Byte byte

A Byte whose signed and unsigned interpretations are congruent modulo 256 to this integer.

Since 1.1.0
characterSource Codeshared Character character

The UTF-32 character with this UCS code point.

Throws
  • OverflowException

    if this integer is not in the range 0..#10FFFF of legal Unicode code points

evenSource Codeshared Boolean even

Determine if this integer is even.

An integer i is even if there exists an integer k such that:

i == 2*k

Thus, i is even if and only if i%2 == 0.

Since 1.1.0
floatSource Codeshared Float float

The number, represented as a Float, if such a representation is possible.

Throws
  • OverflowException

    if the number cannot be represented as a Float without loss of precision, that is, if

    this.magnitude>runtime.maxExactIntegralFloat
    

    (Note that nearestFloat does not produce an exception in this case.)

Since 1.1.0
fractionalPartSource Codeshared actual Integer fractionalPart

The fractional part of this number, after truncation of the integral part. For Integral numeric types, the fractional part is always zero.

hashSource Codeshared actual Integer hash

The hash code of this Integer, which is just the Integer itself, except on the JVM platform where, as with all hash codes, this 64-bit Integer value is truncated to 32 bits by taking the exclusive disjunction of the 32 lowest-order bits with the 32 highest-order bits, that is:

int.hash == int.rightArithmeticShift(32).exclusiveOr(int)
Refines Object.hash
magnitudeSource Codeshared actual Integer magnitude

The magnitude of this number, defined for any instance x as:

  • -x if x<0, or
  • x otherwise,

where 0 is the additive identity. Hence:

  • x.magnitude >= 0 for any x, and
  • x.magnitude == 0 if and only if x==0.
Aliases: absolute
nearestFloatSource Codeshared Float nearestFloat

The nearest Float to this number.

  • For any integer with magnitude smaller than runtime.maxExactIntegralFloat (253), this is a Float with the exact same mathematical value (and the same value as float).
  • For larger integers on the JVM platform, the Floats are less dense than the Integers so there may be loss of precision.

This method never throws an OverflowException.

See also float
Since 1.2.0
negatedSource Codeshared actual Integer negated

The additive inverse of this value.

negativeSource Codeshared actual Boolean negative

Determine if the number is strictly negative, that is, if this<0, where 0 is the additive identity.

notSource Codeshared actual Integer not

The binary complement of this sequence of bits.

Refines Binary.not
positiveSource Codeshared actual Boolean positive

Determine if the number is strictly positive, that is, if this>0, where 0 is the additive identity.

predecessorSource Codeshared actual Integer predecessor

The predecessor of this value.

Refines Enumerable.predecessor ultimately refines Ordinal.predecessor
signSource Codeshared actual Integer sign

The sign of this number:

Must satisfy:

x.magnitude.timesInteger(x.sign) == x
Refines Number.sign
stringSource Codeshared actual String string

A developer-friendly string representing the instance. Concatenates the name of the concrete class of the instance with the hash of the instance. Subclasses are encouraged to refine this implementation to produce a more meaningful representation.

successorSource Codeshared actual Integer successor

The successor of this value.

Refines Enumerable.successor ultimately refines Ordinal.successor
unitSource Codeshared actual Boolean unit

Determine if the number is the multiplicative identity.

wholePartSource Codeshared actual Integer wholePart

The integral value of the number after truncation of the fractional part. For Integral numeric types, the integral value of a number is the number itself.

zeroSource Codeshared actual Boolean zero

Determine if the number is the additive identity.

Inherited Attributes
Attributes inherited from: Object
Attributes inherited from: Binary<Other>
Attributes inherited from: Enumerable<Other>
Attributes inherited from: Integral<Other>
Attributes inherited from: Invertible<Other>
Attributes inherited from: Number<Other>
Attributes inherited from: Ordinal<Other>
Methods
andSource Codeshared actual Integer and(Integer other)

Performs a logical AND operation.

Refines Binary.and
clearSource Codeshared actual Integer clear(Integer index)

If the index is for an addressable bit, an instance with the same addressable bits as this instance, but with that bit cleared. Otherwise an instance with the same addressable bits as this instance.

Refines Binary.clear
compareSource Codeshared actual Comparison compare(Integer other)

Compare this integer with the given integer, returning:

  • smaller, if this < other,
  • larger, if this > other, or
  • equal, if this == other.
Refines Number.compare ultimately refines Comparable.compare
dividedSource Codeshared actual Integer divided(Integer other)

The quotient obtained by dividing this number by the given number. For integral numeric types, this operation rounds toward 0, the additive identity, and results in a remainder.

When the given divisor is exactly 0, the additive identity, the behavior depends on the numeric type:

  • For some numeric types, including Integer, division by 0 results in an exception.
  • For others, including Float, it results in a special value of the type, for example, infinity.
dividesSource Codeshared actual Boolean divides(Integer other)

Determines if this integer is a factor of the given integer, that is, if remainder(other)==0.

equalsSource Codeshared actual Boolean equals(Object that)

Determines if the given object is equal to this Integer, that is, if:

  • the given object is an Integer representing the same whole number.

Or if:

flipSource Codeshared actual Integer flip(Integer index)

If the index is for an addressable bit, an instance with the same addressable bits as this instance, but with that bit flipped. Otherwise an instance with the same addressable bits as this instance.

Refines Binary.flip
formatSource Codeshared static String format(Integer integer, Integer radix = ..., Character? groupingSeparator = null)

The string representation of the given integer in the base given by radix. If the given integer is negative, the string representation will begin with -. Digits consist of decimal digits 0 to 9, together with and lowercase letters a to z for bases greater than 10.

For example:

  • Integer.format(-46) is "-46"
  • Integer.format(9,2) is "1001"
  • Integer.format(10,8) is "12"
  • Integer.format(511,16) is "1ff"
  • Integer.format(512,32) is "g0"
Parameters:
  • integer

    The integer value to format.

  • radix = 10

    The base, between minRadix and maxRadix inclusive.

  • groupingSeparator = null

    If not null, groupingSeparator will be used to separate each group of three digits if radix is 10 (the default), or each group of four digits if radix is 2 or 16.

    groupingSeparator may not be '-', a digit as defined by the Unicode general category Nd, or a letter as defined by the Unicode general categories Lu, Ll, Lt, Lm, and Lo.

Throws
Since 1.3.1
getSource Codeshared actual Boolean get(Integer index)

If the index is for an addressable bit, the value of that bit. Otherwise false.

Refines Binary.get
largerThanSource Codeshared actual Boolean largerThan(Integer other)

Determines if this value is strictly larger than the given value.

largestSource Codeshared static Integer largest(Integer x, Integer y)

The larger of the two given integers.

Since 1.3.2
leftLogicalShiftSource Codeshared actual Integer leftLogicalShift(Integer shift)

If the given shift is in the range of addressable bits given by

0..runtime.integerAddressableSize-1

shift the addressable bits to the left by shift positions, using zero extension to fill in the least significant bits. Otherwise shift the addressable bits to the left by

shift.and(runtime.integerAddressableSize-1)

positions, using zero extension.

Aliases: leftShift
leftShiftSee leftLogicalShift()
maxSource Codeshared static Integer|Absent max<Absent>(Iterable<Integer,Absent> integers)
given Absent satisfies Null

The largest integer in the given stream, or null if the stream is empty.

Since 1.3.2
minSource Codeshared static Integer|Absent min<Absent>(Iterable<Integer,Absent> integers)
given Absent satisfies Null

The smallest integer in the given stream, or null if the stream is empty.

Since 1.3.2
minusSource Codeshared actual Integer minus(Integer other)

The difference between this number and the given number. Must produce the value x + -y.

moduloSource Codeshared actual Integer modulo(Integer modulus)

The modulo, after dividing this number by the given number. This differs from Integral.remainder() in that the result is always positive or 0.

Since 1.2.0
neighbourSource Codeshared actual Integer neighbour(Integer offset)

The indirect successor or predecessor at the given offset, where:

  • x.neighbour(0) == x,
  • x.neighbour(i+1) == x.neighbour(i).successor, and
  • x.neighbour(i-1) == x.neighbour(i).predecessor.
notLargerThanSource Codeshared actual Boolean notLargerThan(Integer other)

Determines if this value is smaller than or equal to the given value.

notSmallerThanSource Codeshared actual Boolean notSmallerThan(Integer other)

Determines if this value is larger than or equal to the given value.

offsetSource Codeshared actual Integer offset(Integer other)

Compute the offset from the given value, where:

  • x.offset(x) == 0, and
  • x.successor.offset(x) == 1 if x!=x.successor.
offsetSignSource Codeshared actual Integer offsetSign(Integer other)

The sign of the offset from the given value.

orSource Codeshared actual Integer or(Integer other)

Performs a logical inclusive OR operation.

Refines Binary.or
parseSource Codeshared static Integer|ParseException parse(String string, Integer radix = ...)

The Integer value of the given string representation of an integer value in the base given by radix, or null if the string does not represent an integer in that base, or if the mathematical integer it represents is too large in magnitude to be represented by an instance of the class Integer.

The syntax accepted by this function depends upon the given base:

  • For base 10, the accepted syntax is the same as the syntax for an Integer literal in the Ceylon language except that it may optionally begin with a sign character (+ or -) and may not contain grouping underscore characters. That is, an optional sign character, followed by a string of decimal digits, followed by an optional SI magnitude: k, M, G, T, or P.
  • For other bases, the accepted syntax is an optional sign character, followed by a string of digits of the given base.

The given radix specifies the base of the string representation. The list of available digits starts from 0 to 9, followed by a to z. When parsing in a specific base, the first radix digits from the available digits list is used. This function is not case sensitive; a and A both correspond to the digit a whose decimal value is 10.

Integer: Base10 | BaseN Base10: Sign? Base10Digits Magnitude BaseN: Sign? BaseNDigits Sign: '+' | '-' Magnitude: 'k' | 'M' | 'G' | 'T' | 'P' Base10Digits: ('0'..'9')+ BaseNDigits: ('0'..'9'|'a'..'z'|'A'..'Z')+

Parameters:
  • string

    The string representation to parse.

  • radix = 10

    The base, between minRadix and maxRadix inclusive.

Throws
Since 1.3.1
plusSource Codeshared actual Integer plus(Integer other)

The result of adding the given value to this value. This operation should never perform any kind of mutation upon either the receiving value or the argument value.

For any two instances x and y of a type that implements Summable, x.plus(y) may be written as:

x + y
plusIntegerSource Codeshared actual Integer plusInteger(Integer integer)

The result of adding this number to the given Integer.

powerSource Codeshared actual Integer power(Integer other)

The result of raising this number to the given non-negative integer power, where 0^0 evaluates to 1.

Throws
powerOfIntegerSource Codeshared actual Integer powerOfInteger(Integer integer)

The result of raising this number to the given non-negative integer power, where 0^0 evaluates to 1.

Throws
productSource Codeshared static Integer product({Integer*} integers)

The product of all the integers in the given stream, or 1 if the stream is empty.

Since 1.3.2
remainderSource Codeshared actual Integer remainder(Integer other)

The remainder, after dividing this number by the given number. The sign of the remainder depends upon the sign of this number, and of the argument divisor:

  • if this dividend is positive, the remainder has the opposite sign as the divisor, or is 0,
  • if this dividend is negative, the remainder has the same sign as the divisor, or is 0, or
  • if this dividend is zero, the remainder is always 0.

Thus, in order to satisfy the identity x == (x/y)*y + x%y, division for an integral numeric type must round toward 0, the additive identity.

rightArithmeticShiftSource Codeshared actual Integer rightArithmeticShift(Integer shift)

If the given shift is in the range of addressable bits given by

0..runtime.integerAddressableSize-1

shift the addressable bits to the right by shift positions, using sign extension to fill in the most significant bits. Otherwise shift the addressable bits to the right by

shift.and(runtime.integerAddressableSize-1)

positions, using sign extension.

rightLogicalShiftSource Codeshared actual Integer rightLogicalShift(Integer shift)

If the given shift is in the range of addressable bits given by

0..runtime.integerAddressableSize-1

shift the addressable bits to the right by shift positions, using zero extension to fill in the most significant bits. Otherwise shift the addressable bits to the right by

shift.and(runtime.integerAddressableSize-1)

positions, using zero extension.

Aliases: rightShift
rightShiftSee rightLogicalShift()
setSource Codeshared actual Integer set(Integer index, Boolean bit)

If the index is for an addressable bit, an instance with the same addressable bits as this instance, but with that bit set to bit. Otherwise an instance with the same addressable bits as this instance.

Refines Binary.set
smallerThanSource Codeshared actual Boolean smallerThan(Integer other)

Determines if this value is strictly smaller than the given value.

smallestSource Codeshared static Integer smallest(Integer x, Integer y)

The smaller of the two given integers.

Since 1.3.2
sumSource Codeshared static Integer sum({Integer*} integers)

The sum of all the integers in the given stream, or 0 if the stream is empty.

Since 1.3.2
timesSource Codeshared actual Integer times(Integer other)

The product of this number and the given number.

timesIntegerSource Codeshared actual Integer timesInteger(Integer integer)

The result of multiplying this number by the given Integer.

xorSource Codeshared actual Integer xor(Integer other)

Performs a logical exclusive OR operation.

Refines Binary.xor
Inherited Methods
Methods inherited from: Object
Methods inherited from: Binary<Other>
Methods inherited from: Comparable<Other>
Methods inherited from: Enumerable<Other>
Methods inherited from: Exponentiable<This,Other>
Methods inherited from: Integral<Other>
Methods inherited from: Invertible<Other>
Methods inherited from: Number<Other>
Methods inherited from: Numeric<Other>
Methods inherited from: Summable<Other>