Appearance
int abstract final
abstract final class int extends numAn integer number.
The default implementation of int is 64-bit two's complement integers with operations that wrap to that range on overflow.
Note: When compiling to JavaScript, integers are restricted to values that can be represented exactly by double-precision floating point values. The available integer values include all integers between -2^53 and 2^53, and some integers with larger magnitude. That includes some integers larger than 2^63. The behavior of the operators and methods in the int class therefore sometimes differs between the Dart VM and Dart code compiled to JavaScript. For example, the bitwise operators truncate their operands to 32-bit integers when compiled to JavaScript.
Classes cannot extend, implement, or mix in int.
See also:
- num the super class for int.
- Built-in number types
- Number representation
Inheritance
Object → num → int
Available Extensions
Constructors
int.fromEnvironment() factory const
Integer value for name in the compilation configuration environment.
The compilation configuration environment is provided by the surrounding tools which are compiling or running the Dart program. The environment is a mapping from a set of string keys to their associated string value. The string value, or lack of a value, associated with a name must be consistent across all calls to String.fromEnvironment, int.fromEnvironment, bool.fromEnvironment and bool.hasEnvironment in a single program. The string values can be directly accessed using String.fromEnvironment.
This constructor looks up the string value for name, then attempts to parse it as an integer, using the same syntax rules as int.parse/int.tryParse. That is, it accepts decimal numerals and hexadecimal numerals with a 0x prefix, and it accepts a leading minus sign. If there is no value associated with name in the compilation configuration environment, or if the associated string value cannot be parsed as an integer, the value of the constructor invocation is the defaultValue integer, which defaults to the integer zero.
The result is effectively the same as that of:
dart
int.tryParse(const String.fromEnvironment(name, defaultValue: ""))
?? defaultValueexcept that the constructor invocation can be a constant value.
Example:
dart
const defaultPort = int.fromEnvironment("defaultPort", defaultValue: 80);In order to check whether a value is there at all, use bool.hasEnvironment. Example:
dart
const int? maybeDeclared = bool.hasEnvironment("defaultPort")
? int.fromEnvironment("defaultPort")
: null;The string value, or lack of a value, associated with a name must be consistent across all calls to String.fromEnvironment, int.fromEnvironment, bool.fromEnvironment and bool.hasEnvironment in a single program.
This constructor is only guaranteed to work when invoked as const. It may work as a non-constant invocation on some platforms which have access to compiler options at run-time, but most ahead-of-time compiled platforms will not have this information.
Implementation
dart
external const factory int.fromEnvironment(
String name, {
int defaultValue = 0,
});Properties
address extension no setter
Pointer<Never> get addressThe memory address of the underlying data.
An expression of the form expression.address denoting this address can only occurr as an entire argument expression in the invocation of a leaf Native external function.
Can only be used on fields of Struct subtypes, fields of Union subtypes, Array elements, or TypedData elements. In other words, the number whose address is being accessed must itself be acccessed through a Struct, Union, Array, or TypedData.
Example:
dart
@Native<Void Function(Pointer<Int8>)>(isLeaf: true)
external void myFunction(Pointer<Int8> pointer);
final class MyStruct extends Struct {
@Int8()
external int x;
@Int8()
external int y;
@Array(10)
external Array<Int8> array;
}
void main() {
final myStruct = Struct.create<MyStruct>();
myFunction(myStruct.y.address);
myFunction(myStruct.array[5].address);
final list = Int8List(10);
myFunction(list[5].address);
}The expression before .address is evaluated like the left-hand-side of an assignment, to something that gives access to the storage behind the expression, which can be used both for reading and writing. The .address then gives a native pointer to that storage.
The .address is evaluated just before calling into native code when invoking a leaf Native external function. This ensures the Dart garbage collector will not move the object that the address points in to.
Available on int, provided by the IntAddress extension
Implementation
dart
external Pointer<Never> get address;bitLength no setter
int get bitLengthReturns the minimum number of bits required to store this integer.
The number of bits excludes the sign bit, which gives the natural length for non-negative (unsigned) values. Negative values are complemented to return the bit position of the first bit that differs from the sign bit.
To find the number of bits needed to store the value as a signed value, add one, i.e. use x.bitLength + 1.
dart
x.bitLength == (-x-1).bitLength;
3.bitLength == 2; // 00000011
2.bitLength == 2; // 00000010
1.bitLength == 1; // 00000001
0.bitLength == 0; // 00000000
(-1).bitLength == 0; // 11111111
(-2).bitLength == 1; // 11111110
(-3).bitLength == 2; // 11111101
(-4).bitLength == 2; // 11111100Implementation
dart
int get bitLength;hashCode no setter inherited
int get hashCodeReturns a hash code for a numerical value.
The hash code is compatible with equality. It returns the same value for an int and a double with the same numerical value, and therefore the same value for the doubles zero and minus zero.
No guarantees are made about the hash code of NaN values.
Inherited from num.
Implementation
dart
int get hashCode;isEven no setter
bool get isEvenReturns true if and only if this integer is even.
Implementation
dart
bool get isEven;isFinite no setter inherited
bool get isFiniteWhether this number is finite.
The only non-finite numbers are NaN values, positive infinity, and negative infinity. All integers are finite.
All numbers satisfy exactly one of isInfinite, isFinite and isNaN.
Inherited from num.
Implementation
dart
bool get isFinite;isInfinite no setter inherited
bool get isInfiniteWhether this number is positive infinity or negative infinity.
Only satisfied by double.infinity and double.negativeInfinity.
All numbers satisfy exactly one of isInfinite, isFinite and isNaN.
Inherited from num.
Implementation
dart
bool get isInfinite;isNaN no setter inherited
bool get isNaNWhether this number is a Not-a-Number value.
Is true if this number is the double.nan value or any other of the possible double NaN values. Is false if this number is an integer, a finite double or an infinite double (double.infinity or double.negativeInfinity).
All numbers satisfy exactly one of isInfinite, isFinite and isNaN.
Inherited from num.
Implementation
dart
bool get isNaN;isNegative no setter inherited
bool get isNegativeWhether this number is negative.
A number is negative if it's smaller than zero, or if it is the double -0.0. This precludes a NaN value like double.nan from being negative.
Inherited from num.
Implementation
dart
bool get isNegative;isOdd no setter
bool get isOddReturns true if and only if this integer is odd.
Implementation
dart
bool get isOdd;runtimeType no setter inherited
Type get runtimeTypeA representation of the runtime type of the object.
Inherited from Object.
Implementation
dart
external Type get runtimeType;sign no setter override
int get signReturns the sign of this integer.
Returns 0 for zero, -1 for values less than zero and +1 for values greater than zero.
Implementation
dart
int get sign;toJS extension no setter
JSNumber get toJSConverts this num to a JSNumber.
Available on num, provided by the NumToJSExtension extension
Implementation
dart
JSNumber get toJS => DoubleToJSNumber(toDouble()).toJS;Methods
abs() override
int abs()Returns the absolute value of this integer.
For any integer value, the result is the same as value < 0 ? -value : value.
Integer overflow may cause the result of -value to stay negative.
Implementation
dart
int abs();ceil() override
int ceil()Returns this.
Implementation
dart
int ceil();ceilToDouble() override
double ceilToDouble()Returns this.toDouble().
Implementation
dart
double ceilToDouble();clamp() inherited
Returns this num clamped to be in the range lowerLimit-upperLimit.
The comparison is done using compareTo and therefore takes -0.0 into account. This also implies that double.nan is treated as the maximal double value.
The arguments lowerLimit and upperLimit must form a valid range where lowerLimit.compareTo(upperLimit) <= 0.
Example:
dart
var result = 10.5.clamp(5, 10.0); // 10.0
result = 0.75.clamp(5, 10.0); // 5
result = (-10).clamp(-5, 5.0); // -5
result = (-0.0).clamp(-5, 5.0); // -0.0Inherited from num.
Implementation
dart
num clamp(num lowerLimit, num upperLimit);compareTo() inherited
Compares this to other.
Returns a negative number if this is less than other, zero if they are equal, and a positive number if this is greater than other.
The ordering represented by this method is a total ordering of num values. All distinct doubles are non-equal, as are all distinct integers, but integers are equal to doubles if they have the same numerical value.
For doubles, the compareTo operation is different from the partial ordering given by operator==, operator< and operator>. For example, IEEE doubles impose that 0.0 == -0.0 and all comparison operations on NaN return false.
This function imposes a complete ordering for doubles. When using compareTo, the following properties hold:
- All NaN values are considered equal, and greater than any numeric value.
- -0.0 is less than 0.0 (and the integer 0), but greater than any non-zero negative value.
- Negative infinity is less than all other values and positive infinity is greater than all non-NaN values.
- All other values are compared using their numeric value.
Examples:
dart
print(1.compareTo(2)); // => -1
print(2.compareTo(1)); // => 1
print(1.compareTo(1)); // => 0
// The following comparisons yield different results than the
// corresponding comparison operators.
print((-0.0).compareTo(0.0)); // => -1
print(double.nan.compareTo(double.nan)); // => 0
print(double.infinity.compareTo(double.nan)); // => -1
// -0.0, and NaN comparison operators have rules imposed by the IEEE
// standard.
print(-0.0 == 0.0); // => true
print(double.nan == double.nan); // => false
print(double.infinity < double.nan); // => false
print(double.nan < double.infinity); // => false
print(double.nan == double.infinity); // => falseInherited from num.
Implementation
dart
int compareTo(num other);floor() override
int floor()Returns this.
Implementation
dart
int floor();floorToDouble() override
double floorToDouble()Returns this.toDouble().
Implementation
dart
double floorToDouble();gcd()
Returns the greatest common divisor of this integer and other.
If either number is non-zero, the result is the numerically greatest integer dividing both this and other.
The greatest common divisor is independent of the order, so x.gcd(y) is always the same as y.gcd(x).
For any integer x, x.gcd(x) is x.abs().
If both this and other is zero, the result is also zero.
Example:
dart
print(4.gcd(2)); // 2
print(8.gcd(4)); // 4
print(10.gcd(12)); // 2
print(10.gcd(0)); // 10
print((-2).gcd(-3)); // 1Implementation
dart
int gcd(int other);modInverse()
Returns the modular multiplicative inverse of this integer modulo modulus.
The modulus must be positive.
It is an error if no modular inverse exists.
Implementation
dart
int modInverse(int modulus);modPow()
Returns this integer to the power of exponent modulo modulus.
The exponent must be non-negative and modulus must be positive.
Implementation
dart
int modPow(int exponent, int modulus);noSuchMethod() inherited
dynamic noSuchMethod(Invocation invocation)Invoked when a nonexistent method or property is accessed.
A dynamic member invocation can attempt to call a member which doesn't exist on the receiving object. Example:
dart
dynamic object = 1;
object.add(42); // Statically allowed, run-time errorThis invalid code will invoke the noSuchMethod method of the integer 1 with an Invocation representing the .add(42) call and arguments (which then throws).
Classes can override noSuchMethod to provide custom behavior for such invalid dynamic invocations.
A class with a non-default noSuchMethod invocation can also omit implementations for members of its interface. Example:
dart
class MockList<T> implements List<T> {
noSuchMethod(Invocation invocation) {
log(invocation);
super.noSuchMethod(invocation); // Will throw.
}
}
void main() {
MockList().add(42);
}This code has no compile-time warnings or errors even though the MockList class has no concrete implementation of any of the List interface methods. Calls to List methods are forwarded to noSuchMethod, so this code will log an invocation similar to Invocation.method(#add, [42]) and then throw.
If a value is returned from noSuchMethod, it becomes the result of the original invocation. If the value is not of a type that can be returned by the original invocation, a type error occurs at the invocation.
The default behavior is to throw a NoSuchMethodError.
Inherited from Object.
Implementation
dart
@pragma("vm:entry-point")
@pragma("wasm:entry-point")
external dynamic noSuchMethod(Invocation invocation);remainder() inherited
The remainder of the truncating division of this by other.
The result r of this operation satisfies: this == (this ~/ other) * other + r. As a consequence, the remainder r has the same sign as the dividend this.
The result is an int, as described by int.remainder, if both this number and other are integers, otherwise the result is a double.
Example:
dart
print(5.remainder(3)); // 2
print(-5.remainder(3)); // -2
print(5.remainder(-3)); // 2
print(-5.remainder(-3)); // -2Inherited from num.
Implementation
dart
num remainder(num other);round() override
int round()Returns this.
Implementation
dart
int round();roundToDouble() override
double roundToDouble()Returns this.toDouble().
Implementation
dart
double roundToDouble();toDouble() inherited
double toDouble()This number as a double.
If an integer number is not precisely representable as a double, an approximation is returned.
Inherited from num.
Implementation
dart
double toDouble();toInt() inherited
int toInt()Truncates this num to an integer and returns the result as an int.
Equivalent to truncate.
Inherited from num.
Implementation
dart
int toInt();toRadixString()
Converts this int to a string representation in the given radix.
In the string representation, lower-case letters are used for digits above '9', with 'a' being 10 and 'z' being 35.
The radix argument must be an integer in the range 2 to 36.
Example:
dart
// Binary (base 2).
print(12.toRadixString(2)); // 1100
print(31.toRadixString(2)); // 11111
print(2021.toRadixString(2)); // 11111100101
print((-12).toRadixString(2)); // -1100
// Octal (base 8).
print(12.toRadixString(8)); // 14
print(31.toRadixString(8)); // 37
print(2021.toRadixString(8)); // 3745
// Hexadecimal (base 16).
print(12.toRadixString(16)); // c
print(31.toRadixString(16)); // 1f
print(2021.toRadixString(16)); // 7e5
// Base 36.
print((35 * 36 + 1).toRadixString(36)); // z1Implementation
dart
String toRadixString(int radix);toSigned()
Returns the least significant width bits of this integer, extending the highest retained bit to the sign. This is the same as truncating the value to fit in width bits using an signed 2-s complement representation. The returned value has the same bit value in all positions higher than width.
dart
// V--sign bit-V
16.toSigned(5) == -16; // 00010000 -> 11110000
239.toSigned(5) == 15; // 11101111 -> 00001111
// ^ ^This operation can be used to simulate arithmetic from low level languages. For example, to increment an 8 bit signed quantity:
dart
q = (q + 1).toSigned(8);q will count from 0 up to 127, wrap to -128 and count back up to 127.
If the input value fits in width bits without truncation, the result is the same as the input. The minimum width needed to avoid truncation of x is x.bitLength + 1, i.e.
dart
x == x.toSigned(x.bitLength + 1);Implementation
dart
int toSigned(int width);toString() override
String toString()Returns a string representation of this integer.
The returned string is parsable by parse. For any int i, it is guaranteed that i == int.parse(i.toString()).
Implementation
dart
String toString();toStringAsExponential() inherited
An exponential string-representation of this number.
Converts this number to a double before computing the string representation.
If fractionDigits is given, then it must be an integer satisfying: 0 <= fractionDigits <= 20. In this case the string contains exactly fractionDigits after the decimal point. Otherwise, without the parameter, the returned string uses the shortest number of digits that accurately represent this number.
If fractionDigits equals 0, then the decimal point is omitted. Examples:
dart
1.toStringAsExponential(); // 1e+0
1.toStringAsExponential(3); // 1.000e+0
123456.toStringAsExponential(); // 1.23456e+5
123456.toStringAsExponential(3); // 1.235e+5
123.toStringAsExponential(0); // 1e+2Inherited from num.
Implementation
dart
String toStringAsExponential([int? fractionDigits]);toStringAsFixed() inherited
A decimal-point string-representation of this number.
Converts this number to a double before computing the string representation, as by toDouble.
If the absolute value of this is greater than or equal to 10^21, then this methods returns an exponential representation computed by this.toStringAsExponential(). Otherwise the result is the closest string representation with exactly fractionDigits digits after the decimal point. If fractionDigits equals 0, then the decimal point is omitted.
The parameter fractionDigits must be an integer satisfying: 0 <= fractionDigits <= 20.
Examples:
dart
1.toStringAsFixed(3); // 1.000
(4321.12345678).toStringAsFixed(3); // 4321.123
(4321.12345678).toStringAsFixed(5); // 4321.12346
123456789012345.toStringAsFixed(3); // 123456789012345.000
10000000000000000.toStringAsFixed(4); // 10000000000000000.0000
5.25.toStringAsFixed(0); // 5Inherited from num.
Implementation
dart
String toStringAsFixed(int fractionDigits);toStringAsPrecision() inherited
A string representation with precision significant digits.
Converts this number to a double and returns a string representation of that value with exactly precision significant digits.
The parameter precision must be an integer satisfying: 1 <= precision <= 21.
Examples:
dart
1.toStringAsPrecision(2); // 1.0
1e15.toStringAsPrecision(3); // 1.00e+15
1234567.toStringAsPrecision(3); // 1.23e+6
1234567.toStringAsPrecision(9); // 1234567.00
12345678901234567890.toStringAsPrecision(20); // 12345678901234567168
12345678901234567890.toStringAsPrecision(14); // 1.2345678901235e+19
0.00000012345.toStringAsPrecision(15); // 1.23450000000000e-7
0.0000012345.toStringAsPrecision(15); // 0.00000123450000000000Inherited from num.
Implementation
dart
String toStringAsPrecision(int precision);toUnsigned()
Returns the least significant width bits of this integer as a non-negative number (i.e. unsigned representation). The returned value has zeros in all bit positions higher than width.
dart
(-1).toUnsigned(5) == 31 // 11111111 -> 00011111This operation can be used to simulate arithmetic from low level languages. For example, to increment an 8 bit quantity:
dart
q = (q + 1).toUnsigned(8);q will count from 0 up to 255 and then wrap around to 0.
If the input fits in width bits without truncation, the result is the same as the input. The minimum width needed to avoid truncation of x is given by x.bitLength, i.e.
dart
x == x.toUnsigned(x.bitLength);Implementation
dart
int toUnsigned(int width);truncate() override
int truncate()Returns this.
Implementation
dart
int truncate();truncateToDouble() override
double truncateToDouble()Returns this.toDouble().
Implementation
dart
double truncateToDouble();Operators
operator %() inherited
Euclidean modulo of this number by other.
Returns the remainder of the Euclidean division. The Euclidean division of two integers a and b yields two integers q and r such that a == b * q + r and 0 <= r < b.abs().
The Euclidean division is only defined for integers, but can be easily extended to work with doubles. In that case, q is still an integer, but r may have a non-integer value that still satisfies 0 <= r < |b|.
The sign of the returned value r is always positive.
See remainder for the remainder of the truncating division.
The result is an int, as described by int.%, if both this number and other are integers, otherwise the result is a double.
Example:
dart
print(5 % 3); // 2
print(-5 % 3); // 1
print(5 % -3); // 2
print(-5 % -3); // 1Inherited from num.
Implementation
dart
num operator %(num other);operator &()
Bit-wise and operator.
Treating both this and other as sufficiently large two's component integers, the result is a number with only the bits set that are set in both this and other
If both operands are negative, the result is negative, otherwise the result is non-negative.
dart
print((2 & 1).toRadixString(2)); // 0010 & 0001 -> 0000
print((3 & 1).toRadixString(2)); // 0011 & 0001 -> 0001
print((10 & 2).toRadixString(2)); // 1010 & 0010 -> 0010Implementation
dart
int operator &(int other);operator *() inherited
Multiplies this number by other.
The result is an int, as described by int.*, if both this number and other are integers, otherwise the result is a double.
Inherited from num.
Implementation
dart
num operator *(num other);operator +() inherited
Adds other to this number.
The result is an int, as described by int.+, if both this number and other is an integer, otherwise the result is a double.
Inherited from num.
Implementation
dart
num operator +(num other);operator -() inherited
Subtracts other from this number.
The result is an int, as described by int.-, if both this number and other is an integer, otherwise the result is a double.
Inherited from num.
Implementation
dart
num operator -(num other);operator /() inherited
Divides this number by other.
Inherited from num.
Implementation
dart
double operator /(num other);operator <() inherited
Whether this number is numerically smaller than other.
Returns true if this number is smaller than other. Returns false if this number is greater than or equal to other or if either value is a NaN value like double.nan.
Inherited from num.
Implementation
dart
bool operator <(num other);operator <<()
Shift the bits of this integer to the left by shiftAmount.
Shifting to the left makes the number larger, effectively multiplying the number by pow(2, shiftAmount).
There is no limit on the size of the result. It may be relevant to limit intermediate values by using the "and" operator with a suitable mask.
It is an error if shiftAmount is negative.
Example:
dart
print((3 << 1).toRadixString(2)); // 0011 -> 0110
print((9 << 2).toRadixString(2)); // 1001 -> 100100
print((10 << 3).toRadixString(2)); // 1010 -> 1010000Implementation
dart
int operator <<(int shiftAmount);operator <=() inherited
Whether this number is numerically smaller than or equal to other.
Returns true if this number is smaller than or equal to other. Returns false if this number is greater than other or if either value is a NaN value like double.nan.
Inherited from num.
Implementation
dart
bool operator <=(num other);operator ==() inherited
Test whether this value is numerically equal to other.
If both operands are doubles, they are equal if they have the same representation, except that:
- zero and minus zero (0.0 and -0.0) are considered equal. They both have the numerical value zero.
- NaN is not equal to anything, including NaN. If either operand is NaN, the result is always false.
If one operand is a double and the other is an int, they are equal if the double has an integer value (finite with no fractional part) and the numbers have the same numerical value.
If both operands are integers, they are equal if they have the same value.
Returns false if other is not a num.
Notice that the behavior for NaN is non-reflexive. This means that equality of double values is not a proper equality relation, as is otherwise required of operator==. Using NaN in, e.g., a HashSet will fail to work. The behavior is the standard IEEE-754 equality of doubles.
If you can avoid NaN values, the remaining doubles do have a proper equality relation, and can be used safely.
Use compareTo for a comparison that distinguishes zero and minus zero, and that considers NaN values as equal.
Inherited from num.
Implementation
dart
bool operator ==(Object other);operator >() inherited
Whether this number is numerically greater than other.
Returns true if this number is greater than other. Returns false if this number is smaller than or equal to other or if either value is a NaN value like double.nan.
Inherited from num.
Implementation
dart
bool operator >(num other);operator >=() inherited
Whether this number is numerically greater than or equal to other.
Returns true if this number is greater than or equal to other. Returns false if this number is smaller than other or if either value is a NaN value like double.nan.
Inherited from num.
Implementation
dart
bool operator >=(num other);operator >>()
Shift the bits of this integer to the right by shiftAmount.
Shifting to the right makes the number smaller and drops the least significant bits, effectively doing an integer division by pow(2, shiftAmount).
It is an error if shiftAmount is negative.
Example:
dart
print((3 >> 1).toRadixString(2)); // 0011 -> 0001
print((9 >> 2).toRadixString(2)); // 1001 -> 0010
print((10 >> 3).toRadixString(2)); // 1010 -> 0001
print((-6 >> 2).toRadixString); // 111...1010 -> 111...1110 == -2
print((-85 >> 3).toRadixString); // 111...10101011 -> 111...11110101 == -11Implementation
dart
int operator >>(int shiftAmount);operator >>>()
Bitwise unsigned right shift by shiftAmount bits.
The least significant shiftAmount bits are dropped, the remaining bits (if any) are shifted down, and zero-bits are shifted in as the new most significant bits.
The shiftAmount must be non-negative.
Example:
dart
print((3 >>> 1).toRadixString(2)); // 0011 -> 0001
print((9 >>> 2).toRadixString(2)); // 1001 -> 0010
print(((-9) >>> 2).toRadixString(2)); // 111...1011 -> 001...1110 (> 0)Implementation
dart
int operator >>>(int shiftAmount);operator ^()
Bit-wise exclusive-or operator.
Treating both this and other as sufficiently large two's component integers, the result is a number with the bits set that are set in one, but not both, of this and other
If the operands have the same sign, the result is non-negative, otherwise the result is negative.
Example:
dart
print((2 ^ 1).toRadixString(2)); // 0010 ^ 0001 -> 0011
print((3 ^ 1).toRadixString(2)); // 0011 ^ 0001 -> 0010
print((10 ^ 2).toRadixString(2)); // 1010 ^ 0010 -> 1000Implementation
dart
int operator ^(int other);operator unary-() override
int operator unary-()Return the negative value of this integer.
The result of negating an integer always has the opposite sign, except for zero, which is its own negation.
Implementation
dart
int operator -();operator unary-() inherited
num operator unary-()The negation of this value.
The negation of a number is a number of the same kind (int or double) representing the negation of the numbers numerical value (the result of subtracting the number from zero), if that value exists.
Negating a double gives a number with the same magnitude as the original value (number.abs() == (-number).abs()), and the opposite sign (-(number.sign) == (-number).sign).
Negating an integer, -number, is equivalent to subtracting it from zero, 0 - number.
(Both properties generally also hold for the other type, but with a few edge case exceptions).
Inherited from num.
Implementation
dart
num operator -();operator |()
Bit-wise or operator.
Treating both this and other as sufficiently large two's component integers, the result is a number with the bits set that are set in either of this and other
If both operands are non-negative, the result is non-negative, otherwise the result is negative.
Example:
dart
print((2 | 1).toRadixString(2)); // 0010 | 0001 -> 0011
print((3 | 1).toRadixString(2)); // 0011 | 0001 -> 0011
print((10 | 2).toRadixString(2)); // 1010 | 0010 -> 1010Implementation
dart
int operator |(int other);operator ~()
int operator ~()The bit-wise negate operator.
Treating this as a sufficiently large two's component integer, the result is a number with the opposite bits set.
This maps any integer x to -x - 1.
Implementation
dart
int operator ~();operator ~/() inherited
Truncating division operator.
Performs truncating division of this number by other. Truncating division is division where a fractional result is converted to an integer by rounding towards zero.
If both operands are ints, then other must not be zero. Then a ~/ b corresponds to a.remainder(b) such that a == (a ~/ b) * b + a.remainder(b).
If either operand is a double, then the other operand is converted to a double before performing the division and truncation of the result. Then a ~/ b is equivalent to (a / b).truncate(). This means that the intermediate result of the double division must be a finite integer (not an infinity or double.nan).
Inherited from num.
Implementation
dart
int operator ~/(num other);Static Methods
parse() override
Parse source as a, possibly signed, integer literal and return its value.
The source must be a non-empty sequence of base-radix digits, optionally prefixed with a minus or plus sign ('-' or '+').
The radix must be in the range 2..36. The digits used are first the decimal digits 0..9, and then the letters 'a'..'z' with values 10 through 35. Also accepts upper-case letters with the same values as the lower-case ones.
If no radix is given then it defaults to 10. In this case, the source digits may also start with 0x, in which case the number is interpreted as a hexadecimal integer literal, When int is implemented by 64-bit signed integers, hexadecimal integer literals may represent values larger than 263, in which case the value is parsed as if it is an unsigned number, and the resulting value is the corresponding signed integer value.
For any int n and valid radix r, it is guaranteed that n == int.parse(n.toRadixString(r), radix: r).
If the source string does not contain a valid integer literal, optionally prefixed by a sign, a FormatException is thrown.
Rather than throwing and immediately catching the FormatException, instead use tryParse to handle a potential parsing error.
Example:
dart
var value = int.tryParse(text);
if (value == null) {
// handle the problem
// ...
}Implementation
dart
external static int parse(String source, {int? radix});tryParse() override
Parse source as a, possibly signed, integer literal.
Like parse except that this function returns null where a similar call to parse would throw a FormatException.
Example:
dart
print(int.tryParse('2021')); // 2021
print(int.tryParse('1f')); // null
// From binary (base 2) value.
print(int.tryParse('1100', radix: 2)); // 12
print(int.tryParse('00011111', radix: 2)); // 31
print(int.tryParse('011111100101', radix: 2)); // 2021
// From octal (base 8) value.
print(int.tryParse('14', radix: 8)); // 12
print(int.tryParse('37', radix: 8)); // 31
print(int.tryParse('3745', radix: 8)); // 2021
// From hexadecimal (base 16) value.
print(int.tryParse('c', radix: 16)); // 12
print(int.tryParse('1f', radix: 16)); // 31
print(int.tryParse('7e5', radix: 16)); // 2021
// From base 35 value.
print(int.tryParse('y1', radix: 35)); // 1191 == 34 * 35 + 1
print(int.tryParse('z1', radix: 35)); // null
// From base 36 value.
print(int.tryParse('y1', radix: 36)); // 1225 == 34 * 36 + 1
print(int.tryParse('z1', radix: 36)); // 1261 == 35 * 36 + 1Implementation
dart
external static int? tryParse(String source, {int? radix});