Class int

extends num implements Comparable
A 64 bit integer.
Ints are always 64 bit two's complement signed values between int.MIN and int.MAX. Overflow is silent.
This is a fully fledged class, not a 'primitive type'.
Ints are immutable objects.

Class summary


MAX -> any
The maximum integer value.
MIN -> any
The minimum integer value.
MIN-8 -> any
The minimum signed 8-bit integer value.
MAX-8 -> any
The maximum signed 8-bit integer value.
MIN-16 -> any
The minimum signed 16-bit integer value.
MAX-16 -> any
The maximum signed 16-bit integer value.
MIN-24 -> any
The minimum signed 24-bit integer value.
MAX-24 -> any
The maximum signed 24-bit integer value.
MIN-32 -> any
The minimum signed 32-bit integer value.
MAX-32 -> any
The maximum signed 32-bit integer values.
MAX-U8 -> any
The maximum unsigned 8-bit integer values.
MAX-U16 -> any
The maximum unsigned 16-bit integer values.
MAX-U24 -> any
The maximum unsigned 24-bit integer values.
MAX-U32 -> any
The maximum unsigned 32-bit integer values.
parse data/Data --radix/int= -> int
Parses the data as an integer.
parse data/Data from/int to/int= --radix/int= -> int
Deprecated. Use parse data --radix with a slice instead.
parse data/Data --radix/int= [--on-error] -> int
Deprecated. Use parse data --radix [--if-error] instead.
parse data/Data --radix/int= [--if-error] -> int
parse data/Data from/int to/int= --radix/int= [--on-error] -> int
Deprecated. Use parse data --radix [--if-error] with a slice instead.

Statics

MAX -> any
The maximum integer value.
The maximum value is equal to:
  • 9223372036854775807
  • 2**63-1 (** is "to the power of")
  • 0x7fff_ffff_ffff_ffff

MAX-16 -> any
The maximum signed 16-bit integer value.

MAX-24 -> any
The maximum signed 24-bit integer value.

MAX-32 -> any
The maximum signed 32-bit integer values.

MAX-8 -> any
The maximum signed 8-bit integer value.

MAX-U16 -> any
The maximum unsigned 16-bit integer values.

MAX-U24 -> any
The maximum unsigned 24-bit integer values.

MAX-U32 -> any
The maximum unsigned 32-bit integer values.

MAX-U8 -> any
The maximum unsigned 8-bit integer values.

MIN -> any
The minimum integer value.
The minimum value is equal to:
  • -9223372036854775808
  • -2**63 (** is "to the power of").
  • 0x8000_0000_0000_0000

MIN-16 -> any
The minimum signed 16-bit integer value.

MIN-24 -> any
The minimum signed 24-bit integer value.

MIN-32 -> any
The minimum signed 32-bit integer value.

MIN-8 -> any
The minimum signed 8-bit integer value.

parse data/Data --radix/int=null -> int
Parses the data as an integer.
The given radix must be in the range 2 and 36 (inclusive).
Use slices to parse only a subset of the data (for example data[..3]).
Errors
The data must be a valid integer. That is, it may have a leading "-" and must otherwise only contain valid characters as specified by the radix.
The number represented by data must be in the 64-bit integer range ([int.MIN, int.MAX]).
The data must not be empty.
Examples

int.parse "22"           // => 22
int.parse "-2"           // => -2
int.parse "007"          // => 7
int.parse "anno 2017"    // Error.

int.parse "22" --radix=16       // => 34
int.parse "a" --radix=16        // => 10
int.parse "A" --radix=16        // => 10

parse data/Data from/int to/int=data.byte-size --radix/int=null -> int
Deprecated. Use parse data --radix with a slice instead.

parse data/Data --radix/int=null [--on-error] -> int
Deprecated. Use parse data --radix [--if-error] instead.

parse data/Data --radix/int=null [--if-error] -> int
If the data can't be parsed correctly, returns the result of calling the if-error block.

parse data/Data from/int to/int=data.byte-size --radix/int=null [--on-error] -> int
Deprecated. Use parse data --radix [--if-error] with a slice instead.

Methods

abs -> int
See super.

compare-to other/num -> int
Compares this number to the other.
Uses the truncated division for the modulo computation. The sign of the result is thus always the same as the one of the divisor (the first operand).
Returns 1 if this number is greater than the other.
Returns 0 if this number is equal to the other.
Returns -1 if this number is less than the other.
Returns -1 if the other is NaN (float.NAN).
Returns 0 if both this number and the other are NaN (float.NAN).
Return 1 if this number is NaN and the other is not NaN.
Contrary to < this comparison handles 0.0 and -0.0, such that 0.0.compare-to -0.0 returns 1.
Examples

2.compare-to 1  // => 1
1.compare-to 1  // => 0
1.compare-to 2  // => -1

(-0.0).compare-to 0.0 // => -1

2.compare-to float.NAN // => -1

float.INFINITY.compare-to 3               // => 1
float.INFINITY.compare-to float.INFINITY  // => 0
3.compare-to float.INFINITY               // => -1

compare-to other/num [--if-equal] -> int
Variant of compare-to other.
Calls if-equal if this number is equal to other.
Examples
In the example, MyTime implements a lexicographical ordering of seconds and nanoseconds using compare-to other [--if-equal] to move on to nanoseconds when the seconds component is equal.

class MyTime:
  seconds/int
  nanoseconds/int

  constructor .seconds .nanoseconds:

  compare-to other/MyTime -> int:
    return seconds.compare-to other.seconds --if-equal=:
      nanoseconds.compare-to other.nanoseconds

Returns the number of initial zeros in the binary representation of the integer.
The integer is treated as an unsigned 64 bit number. Thus it returns 0 if called on a negative integer.
Examples

(0x00FF).count-leading-zeros  // => 56
(0x0025).count-leading-zeros  // => 58
(0).count-leading-zeros       // => 64
int.MIN.count-leading-zeros   // => 0
int.MAX.count-leading-zeros   // => 1

Returns the number of trailing zeros in the binary representation of the integer.
The integer is treated as an unsigned 64 bit number. Thus it returns 1 if called on -2.
Examples

(0b101000).count-trailing-zeros   // => 3
(0b101100).count-trailing-zeros   // => 2
(0b101010).count-trailing-zeros   // => 1
(0b101101).count-trailing-zeros   // => 0
(0).count-trailing-zeros          // => 64
int.MIN.count-trailing-zeros      // => 63
int.MAX.count-trailing-zeros      // => 0

Counts the number of ones in the binary representation of the integer.
Returns false if the number is odd, true if the number is even.
The integer is treated as a 64 bit number. Thus it returns false if called on -1.
Examples

(0b101101).parity  // => true
(0b101100).parity  // => false
(0b101110).parity  // => true
(0b101111).parity  // => false
(0).parity         // => true
(-1).parity        // => true
int.MIN.parity     // => false
int.MAX.parity     // => false

Counts the number of ones in the binary representation of the integer.
Returns true if the number is odd, false if the number is even.
The integer is treated as a 64 bit number. Thus it returns true if called on -1.
Examples

(0b101101).parity  // => false
(0b101100).parity  // => true
(0b101110).parity  // => false
(0b101111).parity  // => true
(0).parity         // => false
(-1).parity        // => false
int.MIN.parity     // => true
int.MAX.parity     // => true

The hash code of this number.

Whether this integer is aligned with n.
The given n must be a power of 2.
Examples

8.is-aligned 2         // => true
4.is-aligned 4         // => true
16384.is-aligned 4096  // => true
0.is-aligned 4096      // => true

2.is-aligned 1024  // => false
3.is-aligned 2     // => false.

2.is-aligned 3     // Error.
  

Whether this integer is a power of two.
An integer is a power of two if there exists a number n such that the integer is equal to 2**n.
(** is "to the power of".)
Examples

1.is-power-of-two     // => true
2.is-power-of-two     // => true
4.is-power-of-two     // => true
1096.is-power-of-two  // => true

0.is-power-of-two     // => false
(-2).is-power-of-two  // => false
1.is-power-of-two     // => false
14.is-power-of-two    // => false

operator - other/num -> any
Subtracts this number from the other.
Overflows if this number and the other are integers and the result is outside the 64-bit integer range ([int.MIN, int.MAX]).
Returns NaN (float.NAN) if either this number of the other is NaN.
Returns infinity (float.INFINITY) if either this number or the other is infinity and the other is a scalar.
Examples

46 - 2          // => 44
1.0 - 3.0       // => -2.0
1 - 1.1         // => -0.10000000000000008882
int.MAX - (-1)  // => -9223372036854775808
int.MIN - 1     // => 9223372036854775807

1 - float.NAN          // => float.NAN
float.NAN - 1          // => float.NAN
float.NAN - float.NAN  // => float.NAN

float.INFINITY - 1               // => float.INFINITY
float.INFINITY - float.INFINITY  // => float.NAN

operator * other/num -> any
Multiplies this number with the other.
Overflows if this number and the other are integers and the result is outside the 64-bit integer range ([int.MIN, int.MAX]).
Returns NaN (float.NAN) if either this number of the other is NaN.
Returns infinity (float.INFINITY) if either this number or the other is infinity and the other is not NaN.
Returns NaN if both this numbers and the other are infinity (float.INFINITY).
Examples

7 * 9         // => 63
-12 * 3       // => -36
2.0 * 3.0     // => 6.0
2 * 1.1       // => 2.2000000000000001776
-1 * int.MAX  // => -9223372036854775807
-1 * int.MIN  // => -9223372036854775808

1 * float.NAN          // => float.NAN
float.NAN * 1          // => float.NAN
float.NAN * float.NAN  // => float.NAN

float.INFINITY * 1                // => float.INFINITY
float.INFINITY * float.INFINITY   // => float.INFINITY
-1 * float.INFINITY               // => -float.INFINITY
float.INFINITY * -float.INFINITY  // => -float.INFINITY

operator / other/num -> any
Divides this number by the other.
Returns NaN (float.NAN) if either this number of the other is NaN.
Returns infinity (float.INFINITY) for division by zero if either this number or the other is a float.
Returns infinity (float.INFINITY) if this number is infinity and the other is a scalar.
Returns 0.0 if either this number is a scalar and the other is infinity (float.INFINITY).
Returns NaN (float.NAN) if both this number and the other are either infinity (float.INFINITY).
Errors
The other must not be 0 when this number is an int.
Examples

46 / 2    // => 23
2.0 / 4.0 // => 0.5
-1 / 3.0  // => -0.33333333333333331483

2 / 0     // Error.
2.0 / 0   // => float.INFINITY
2 / 0.0   // => float.INFINITY
2 / -0.0   // => -float.INFINITY

1 / float.NAN          // => float.NAN
float.NAN / 1          // => float.NAN
float.NAN / float.NAN  // => float.NAN

float.INFINITY / 2               // => float.INFINITY
float.INFINITY / float.INFINITY  // => float.NAN

operator & other/int -> int
Bitwise-ANDs this integer with the other.
Examples

1 & 1        // => 1
1 & 0        // => 0
0 & 1        // => 0
0 & 0        // => 0
293 & 465    // => 257

0b1111 & 0b1110  // => 14 (0b1110)
0b0001 & 0b1110  // => 0
0b0011 & 0b1110  // => 2 (0b10)

operator % other/num -> any
Takes this number modulo the other.
Uses the truncated division for the modulo computation. The sign of the result is thus always the same as the one of the divisor (the first operand).
Returns NaN (float.NAN) if this number or the other is a float and the other is equal to 0.0.
Returns NaN (float.NAN) if either this number or the other is a NaN.
Errors
The other must not be 0 when this number is an int.
Examples

5 % 3    // => 2
-5 % 3   // => -2
5 % -3   // => 2
-5 % -3  // => -2
6 % 1.5  // => 0.0
5.2 % 3  // => 2.2000000000000001776

5 % 0    // => Error.
2.0 % 0  // => float.NAN
2 % 0.0  // => float.NAN

1 % float.NAN          // => float.NAN
float.NAN % 1          // => float.NAN
float.NAN % float.NAN  // => float.NAN

operator ^ other/int -> int
Bitwise-XORs this integer with the other.
Examples

1 ^ 1        // => 0
1 ^ 0        // => 1
0 ^ 1        // => 1
0 ^ 0        // => 0
293 ^ 465    // => 244

0b1010 ^ 0b0101  // => 15 (0b1111)
0b1010 ^ 0b1010  // => 0 (0b0000)
0b1111 ^ 0b1010  // => 7 (0b0101)

operator + other/num -> any
Sums this number with the other.
Overflows if this number and the other are integers and the result is outside the 64-bit integer range ([int.MIN, int.MAX]).
Returns NaN (float.NAN) if either this number of the other is NaN.
Returns infinity (float.INFINITY) if either this number or the other is infinity and the other is a scalar.
Returns NaN when summing positive and negative infinity (float.INFINITY).
Examples

1 + 1           // => 2
1.0 + 1.0       // => 2.0
1 + 1.1         // => 2.1000000000000000888
int.MAX + 1     // => -9223372036854775808
int.MIN + (-1)  // => 9223372036854775807

1 + float.NAN          // => float.NAN
float.NAN + 1          // => float.NAN
float.NAN + float.NAN  // => float.NAN

float.INFINITY + 1                  // => float.INFINITY
float.INFINITY + -float.INFINITY    // => float.NAN

Whether this number is less than the other.
Returns false if this number or the other is a NaN (float.NAN)
Examples

1 < 1  // => false
1 < 2  // => true
2 < 1  // => false

12.3 < 12.3  // => false
0.0 < 12.3   // => true
1.2 < 0.0    // => false
0.0 < -0.0   // => false
-0.0 < 0.0   // => false

12 < 123.0    // => true
12.34 < 123   // => true
1234 < 123.0  // => false
1.2 < 1       // => false

float.NAN < float.NAN  // => false
1 < float.NAN          // => false
float.NAN < 1.0        // => false

float.MAX-FINITE < float.INFINITY  // => true
float.NAN < float.INFINITY  // => false
float.INFINITY < float.NAN  // => false

operator << number-of-bits/int -> int
Left shifts this integer with number-of-bits.
Examples

0 << 2  // => 0
1 << 2  // => 4
1 << 10  // => 1024
1 << 62  // => 4611686018427387904
1 << 63  // => -9223372036854775808
1 << 64  // => 0

-1 << 2  // => -4
-1 << 9  // => -512
-1 << 63 // => -9223372036854775808
-1 << 0  // => 0

Whether this number is less than or equal to the other.
Returns false if this number or the other is a NaN (float.NAN).
Examples

1 <= 1  // => true
1 <= 2  // => true
2 <= 1  // => false

12.3 <= 12.3  // => true
0.0 <= 12.3   // => true
1.2 <= 0.0    // => false
0.0 <= -0.0   // => true

12 <= 123.0    // => true
12.34 <= 123   // => true
32.0 <= 32     // => true
32 <= 32.0     // => true
1234 <= 123.0  // => false
1.2 <= 1       // => false

float.NAN <= float.NAN  // => false
1 <= float.NAN          // => false
float.NAN <= 1.0        // => false

float.MAX-FINITE <= float.INFINITY  // => true
float.NAN <= float.INFINITY  // => false
float.INFINITY <= float.NAN  // => false

Whether this number is equal to the other.
Returns false if this number or the other is a NaN (float.NAN).
Examples

1 == 1  // => true
1 == 2  // => false
2 == 1  // => false

12.3 == 12.3  // => true
0.0 == 12.3   // => false
1.2 == 0.0    // => false
0.0 == -0.0   // => true

123 == 123.0     // => true
1.0 == 1         // => true

float.NAN == float.NAN  // => false
1 == float.NAN          // => false
float.NAN == 1.0        // => false

float.INFINITY == float.INFINITY  // => true

Whether this number is greater than the other.
Returns false if this number or the other is a NaN (float.NAN).
Examples

1 > 1  // => false
1 > 2  // => false
2 > 1  // => true

12.3 > 12.3  // => false
0.0 > 12.3   // => false
1.2 > 0.0    // => true
-0.0 > 0.0   // => false

12 > 123.0    // => false
12.34 > 123   // => false
32.0 > 32     // => false
32 > 32.0     // => false
1234 > 123.0  // => true
1.2 > 1       // => true

float.NAN > float.NAN  // => false
1 > float.NAN          // => false
float.NAN > 1.0        // => false

float.MAX-FINITE > float.INFINITY  // => false
float.NAN > float.INFINITY  // => false
float.INFINITY > float.NAN  // => false

Whether this number is greater than or equal to the other.
Returns false if this number or the other is a NaN (float.NAN).
Examples

1 >= 1  // => true
1 >= 2  // => false
2 >= 1  // => true

12.3 >= 12.3  // => true
0.0 >= 12.3   // => false
1.2 >= 0.0    // => true
-0.0 >= 0.0   // => true

12 >= 123.0    // => false
12.34 >= 123   // => false
32.0 >= 32     // => true
32 >= 32.0     // => true
1234 >= 123.0  // => true
1.2 >= 1       // => true

float.NAN >= float.NAN  // => false
1 >= float.NAN          // => false
float.NAN >= 1.0        // => false

float.MAX-FINITE >= float.INFINITY  // => false
float.NAN >= float.INFINITY  // => false
float.INFINITY >= float.NAN  // => false

operator >> number-of-bits/int -> int
Right shifts this integer with number-of-bits.
The left-most bit of this integer is inserted to the left of the shifted bits preserving the sign.
Examples

16 >> 0  // => 16
16 >> 1  // => 8
16 >> 4  // => 1
16 >> 5  // => 0

-16 >> 1  // => -8
-16 >> 4  // => -1
-16 >> 5  // => -1

operator >>> number-of-bits/int -> int
Right shifts this integer with number-of-bits, erasing the sign bit.
Examples

16 >>> 0  // => 16
16 >>> 1  // => 8
16 >>> 4  // => 1
16 >>> 5  // => 0

-1 >>> 0    // => -1
-16 >>> 1   // => 9223372036854775800
-1 >>> 1    // => 9223372036854775807 (int.MAX)
-16 >>> 60  // => 15
-16 >>> 64  // => 0

operator | other/int -> int
Bitwise-ORs this integer with the other.
Examples

1 | 1        // => 1
1 | 0        // => 1
0 | 1        // => 1
0 | 0        // => 0
293 | 465    // => 501

0b1100 | 0b0011  // => 15 (0b1111)
0b1010 | 0b0011  // => 11 (0b1011)

Negates this integer bitwise.
Examples

~0  // => -1 (0xffff_ffff_ffff_ffff)
~1  // => -2 (0xffff_ffff_ffff_fffe)

Counts the number of ones in the binary representation of the integer.
Returns 1 if the number is odd, zero if the number is even.
The integer is treated as a 64 bit number. Thus it returns 0 if called on -1.
Examples

(0b101101).parity  // => 0
(0b101100).parity  // => 1
(0b101110).parity  // => 0
(0b101111).parity  // => 1
(0).parity         // => 0
(-1).parity        // => 0
int.MIN.parity     // => 1
int.MAX.parity     // => 1

Returns the number of ones in the binary representation of the integer.
The integer is treated as a 64 bit number. Thus it returns 64 if called on -1.
Examples

(0b100001).population-count  // => 2
(0b101100).population-count  // => 3
(0b101110).population-count  // => 4
(0b101111).population-count  // => 5
(0).population-count         // => 0
(-1).population-count        // => 64
int.MIN.population-count     // => 1
int.MAX.population-count     // => 63

repeat [block] -> none
Calls the given block a number of times corresponding to the value of this integer.
If the number is negative, then the given block is not called.
Examples

count := 0
3.repeat: count++
print count  // >> 3

count = 0
0.repeat: count++
print count  // >> 0

count = 0
(-1).repeat: count++
print count  // >> 0

See super.

sign-extend --bits/int -> int
Sign-extend an n-bit two's complement number to a full (64 bit) signed Toit integer.
Examples

255.sign-extend --bits=8  // => -1
128.sign-extend --bits=8  // => -128
127.sign-extend --bits=8  // => 127

Deprecated. Use to-string --radix instead.

stringify --uint64/True -> string
Deprecated. Use to-string --uint64 instead.

Converts this number to a floating point number.
For very large integers, the conversion may be to the nearest floating point number.
Examples

2.to-float   // => 2.0
2.1.to-float // => 2.1

9223372036854775807.to-float  // => 9223372036854775808.0


to-string --radix/int -> string
Variant of stringify.
Unlike string interpolation with base 2, 8, or 16, negative numbers are rendered in a straight-forward way with a '-' character at the start.
Supports radix 2 to 36.
Examples

0.stringify 2   // => 0
7.stringify 2   // => 111
32.stringify 32 // => 10
42.stringify 16 // => 2a
-1.stringify 8  // => -1
-9.stringify 8  // => -11
35.stringify 36 // => z

to-string --uint64/True -> string
Variant of stringify.
Treats the number as an unsigned 64-bit integer.

Converts this number to a well-defined string.