int64 Class
An immutable 64-bit signed integer, in the range -2^63, 2^63 - 1.
Arithmetic operations may overflow in order to maintain this range.
Implements
Constructors
Code new int64._bits(int _l, int _m, int _h) #
Constructs an int64 with a given bitwise representation. No validation is performed.
int64._bits(int this._l, int this._m, int this._h);
Code new int64.fromInts(int top, int bottom) #
Constructs an int64 from a pair of 32-bit integers having the value
((top & 0xffffffff) << 32) | (bottom & 0xffffffff).
int64.fromInts(int top, int bottom) {
top &= 0xffffffff;
bottom &= 0xffffffff;
_l = bottom & _MASK;
_m = ((top & 0xfff) << 10) | ((bottom >> _BITS) & 0x3ff);
_h = (top >> 12) & _MASK_2;
}
Code factory int64.fromBytesBigEndian(List<int> bytes) #
factory int64.fromBytesBigEndian(List<int> bytes) {
int top = bytes[0] & 0xff;
top <<= 8;
top |= bytes[1] & 0xff;
top <<= 8;
top |= bytes[2] & 0xff;
top <<= 8;
top |= bytes[3] & 0xff;
int bottom = bytes[4] & 0xff;
bottom <<= 8;
bottom |= bytes[5] & 0xff;
bottom <<= 8;
bottom |= bytes[6] & 0xff;
bottom <<= 8;
bottom |= bytes[7] & 0xff;
return new int64.fromInts(top, bottom);
}
Code factory int64.fromBytes(List<int> bytes) #
factory int64.fromBytes(List<int> bytes) {
int top = bytes[7] & 0xff;
top <<= 8;
top |= bytes[6] & 0xff;
top <<= 8;
top |= bytes[5] & 0xff;
top <<= 8;
top |= bytes[4] & 0xff;
int bottom = bytes[3] & 0xff;
bottom <<= 8;
bottom |= bytes[2] & 0xff;
bottom <<= 8;
bottom |= bytes[1] & 0xff;
bottom <<= 8;
bottom |= bytes[0] & 0xff;
return new int64.fromInts(top, bottom);
}
Code new int64.fromInt(int value) #
Constructs an int64 with a given int value.
int64.fromInt(int value) {
bool negative = false;
if (value < 0) {
negative = true;
value = -value - 1;
}
if (_haveBigInts) {
_l = value & _MASK;
_m = (value >> _BITS) & _MASK;
_h = (value >> _BITS01) & _MASK_2;
} else {
// Avoid using bitwise operations that coerce their input to 32 bits.
_h = value ~/ 17592186044416; // 2^44
value -= _h * 17592186044416;
_m = value ~/ 4194304; // 2^22
value -= _m * 4194304;
_l = value;
}
if (negative) {
_l = ~_l & _MASK;
_m = ~_m & _MASK;
_h = ~_h & _MASK_2;
}
}
Static Methods
Code int64 get MAX_VALUE() #
The maximum positive value attainable by an int64, namely 9,223,372,036,854,775,807.
static int64 get MAX_VALUE() {
if (_MAX_VALUE == null) {
_MAX_VALUE = new int64._bits(_MASK, _MASK, _MASK_2 >> 1);
}
return _MAX_VALUE;
}
Code int64 get MIN_VALUE() #
The minimum positive value attainable by an int64, namely -9,223,372,036,854,775,808.
static int64 get MIN_VALUE() {
if (_MIN_VALUE == null) {
_MIN_VALUE = new int64._bits(0, 0, _SIGN_BIT_VALUE);
}
return _MIN_VALUE;
}
Code int64 get ONE() #
An int64 constant equal to 1.
static int64 get ONE() {
if (_ONE == null) {
_ONE = new int64._bits(1, 0, 0);
}
return _ONE;
}
Code int64 get TWO() #
An int64 constant equal to 2.
static int64 get TWO() {
if (_TWO == null) {
_TWO = new int64._bits(2, 0, 0);
}
return _TWO;
}
Code int64 get ZERO() #
An int64 constant equal to 0.
static int64 get ZERO() {
if (_ZERO == null) {
_ZERO = new int64();
}
return _ZERO;
}
Code int64 parseHex(String s) #
Parses a hexadecimal String and returns an int64.
static int64 parseHex(String s) => parseRadix(s, 16);
Code int64 parseInt(String s) #
Parses a decimal String and returns an int64.
static int64 parseInt(String s) => parseRadix(s, 10);
Code int64 parseRadix(String s, int radix) #
Parses a String in a given
radix between 2 and 16 and returns an
int64.
static int64 parseRadix(String s, int radix) {
if ((radix <= 1) || (radix > 16)) {
throw "Bad radix: $radix";
}
int64 x = ZERO;
int i = 0;
bool negative = false;
if (s[0] == '-') {
negative = true;
i++;
}
for (; i < s.length; i++) {
int c = s.charCodeAt(i);
int digit = int32._decodeHex(c);
if (digit < 0 || digit >= radix) {
throw new Exception("Non-radix char code: $c");
}
x = (x * radix) + digit;
}
return negative ? -x : x;
}
Methods
Code int compareTo(Comparable other) #
int compareTo(Comparable other) {
int64 o = _promote(other);
int signa = _h >> (_BITS2 - 1);
int signb = o._h >> (_BITS2 - 1);
if (signa != signb) {
return signa == 0 ? 1 : -1;
}
if (_h > o._h) {
return 1;
} else if (_h < o._h) {
return -1;
}
if (_m > o._m) {
return 1;
} else if (_m < o._m) {
return -1;
}
if (_l > o._l) {
return 1;
} else if (_l < o._l) {
return -1;
}
return 0;
}
Code int hashCode() #
Returns a hash code based on all the bits of this int64.
int hashCode() {
int bottom = ((_m & 0x3ff) << _BITS) | _l;
int top = (_h << 12) | ((_m >> 10) & 0xfff);
return bottom ^ top;
}
Code bool isEven() #
bool isEven() => (_l & 0x1) == 0;
Code bool isMaxValue() #
bool isMaxValue() => (_h == _MASK_2 >> 1) && _m == _MASK && _l == _MASK;
Code bool isMinValue() #
bool isMinValue() => _h == _SIGN_BIT_VALUE && _m == 0 && _l == 0;
Code bool isNegative() #
bool isNegative() => (_h >> (_BITS2 - 1)) != 0;
Code bool isOdd() #
bool isOdd() => (_l & 0x1) == 1;
Code bool isZero() #
bool isZero() => _h == 0 && _m == 0 && _l == 0;
Code int numberOfLeadingZeros() #
Returns the number of leading zeros in this int64 as an int
between 0 and 64.
int numberOfLeadingZeros() {
int b2 = int32._numberOfLeadingZeros(_h);
if (b2 == 32) {
int b1 = int32._numberOfLeadingZeros(_m);
if (b1 == 32) {
return int32._numberOfLeadingZeros(_l) + 32;
} else {
return b1 + _BITS2 - (32 - _BITS);
}
} else {
return b2 - (32 - _BITS2);
}
}
Code int numberOfTrailingZeros() #
Returns the number of trailing zeros in this int64 as an int
between 0 and 64.
int numberOfTrailingZeros() {
int zeros = int32._numberOfTrailingZeros(_l);
if (zeros < 32) {
return zeros;
}
zeros = int32._numberOfTrailingZeros(_m);
if (zeros < 32) {
return _BITS + zeros;
}
zeros = int32._numberOfTrailingZeros(_h);
if (zeros < 32) {
return _BITS01 + zeros;
}
// All zeros
return 64;
}
Code int64 operator negate() #
int64 operator -() {
// Like 0 - this.
int sum0 = -_l;
int sum1 = -_m + _shiftRight(sum0, _BITS);
int sum2 = -_h + _shiftRight(sum1, _BITS);
return new int64._bits(sum0 & _MASK, sum1 & _MASK, sum2 & _MASK_2);
}
Code int64 operator +(other) #
int64 operator +(other) {
int64 o = _promote(other);
int sum0 = _l + o._l;
int sum1 = _m + o._m + _shiftRight(sum0, _BITS);
int sum2 = _h + o._h + _shiftRight(sum1, _BITS);
int64 result = new int64._bits(sum0 & _MASK, sum1 & _MASK, sum2 & _MASK_2);
return result;
}
Code int64 operator *(other) #
int64 operator *(other) {
int64 o = _promote(other);
// Grab 13-bit chunks.
int a0 = _l & 0x1fff;
int a1 = (_l >> 13) | ((_m & 0xf) << 9);
int a2 = (_m >> 4) & 0x1fff;
int a3 = (_m >> 17) | ((_h & 0xff) << 5);
int a4 = (_h & 0xfff00) >> 8;
int b0 = o._l & 0x1fff;
int b1 = (o._l >> 13) | ((o._m & 0xf) << 9);
int b2 = (o._m >> 4) & 0x1fff;
int b3 = (o._m >> 17) | ((o._h & 0xff) << 5);
int b4 = (o._h & 0xfff00) >> 8;
// Compute partial products.
// Optimization: if b is small, avoid multiplying by parts that are 0.
int p0 = a0 * b0; // << 0
int p1 = a1 * b0; // << 13
int p2 = a2 * b0; // << 26
int p3 = a3 * b0; // << 39
int p4 = a4 * b0; // << 52
if (b1 != 0) {
p1 += a0 * b1;
p2 += a1 * b1;
p3 += a2 * b1;
p4 += a3 * b1;
}
if (b2 != 0) {
p2 += a0 * b2;
p3 += a1 * b2;
p4 += a2 * b2;
}
if (b3 != 0) {
p3 += a0 * b3;
p4 += a1 * b3;
}
if (b4 != 0) {
p4 += a0 * b4;
}
// Accumulate into 22-bit chunks:
// .........................................c10|...................c00|
// |....................|..................xxxx|xxxxxxxxxxxxxxxxxxxxxx| p0
// |....................|......................|......................|
// |....................|...................c11|......c01.............|
// |....................|....xxxxxxxxxxxxxxxxxx|xxxxxxxxx.............| p1
// |....................|......................|......................|
// |.................c22|...............c12....|......................|
// |..........xxxxxxxxxx|xxxxxxxxxxxxxxxxxx....|......................| p2
// |....................|......................|......................|
// |.................c23|..c13.................|......................|
// |xxxxxxxxxxxxxxxxxxxx|xxxxx.................|......................| p3
// |....................|......................|......................|
// |.........c24........|......................|......................|
// |xxxxxxxxxxxx........|......................|......................| p4
int c00 = p0 & 0x3fffff;
int c01 = (p1 & 0x1ff) << 13;
int c0 = c00 + c01;
int c10 = p0 >> 22;
int c11 = p1 >> 9;
int c12 = (p2 & 0x3ffff) << 4;
int c13 = (p3 & 0x1f) << 17;
int c1 = c10 + c11 + c12 + c13;
int c22 = p2 >> 18;
int c23 = p3 >> 5;
int c24 = (p4 & 0xfff) << 8;
int c2 = c22 + c23 + c24;
// Propagate high bits from c0 -> c1, c1 -> c2.
c1 += c0 >> _BITS;
c0 &= _MASK;
c2 += c1 >> _BITS;
c1 &= _MASK;
c2 &= _MASK_2;
return new int64._bits(c0, c1, c2);
}
Code int64 operator %(other) #
int64 operator %(other) {
if (other.isZero()) {
throw new IntegerDivisionByZeroException();
}
if (this.isZero()) {
return ZERO;
}
int64 o = _promote(other).abs();
_divMod(this, o, true);
return _remainder < 0 ? (_remainder + o) : _remainder;
}
Code int64 operator &(other) #
int64 operator &(other) {
int64 o = _promote(other);
int a0 = _l & o._l;
int a1 = _m & o._m;
int a2 = _h & o._h;
return new int64._bits(a0, a1, a2);
}
Code int64 operator |(other) #
int64 operator |(other) {
int64 o = _promote(other);
int a0 = _l | o._l;
int a1 = _m | o._m;
int a2 = _h | o._h;
return new int64._bits(a0, a1, a2);
}
Code int64 operator ^(other) #
int64 operator ^(other) {
int64 o = _promote(other);
int a0 = _l ^ o._l;
int a1 = _m ^ o._m;
int a2 = _h ^ o._h;
return new int64._bits(a0, a1, a2);
}
Code int64 operator ~() #
int64 operator ~() {
var result = new int64._bits((~_l) & _MASK, (~_m) & _MASK, (~_h) & _MASK_2);
return result;
}
Code int64 operator >>(int n) #
int64 operator >>(int n) {
if (n < 0) {
throw new IllegalArgumentException("$n");
}
n &= 63;
int res0, res1, res2;
// Sign extend h(a).
int a2 = _h;
bool negative = (a2 & _SIGN_BIT_VALUE) != 0;
if (negative) {
a2 += 0x3 << _BITS2; // add extra one bits on the left
}
if (n < _BITS) {
res2 = _shiftRight(a2, n);
if (negative) {
res2 |= _MASK_2 & ~(_MASK_2 >> n);
}
res1 = _shiftRight(_m, n) | (a2 << (_BITS - n));
res0 = _shiftRight(_l, n) | (_m << (_BITS - n));
} else if (n < _BITS01) {
res2 = negative ? _MASK_2 : 0;
res1 = _shiftRight(a2, n - _BITS);
if (negative) {
res1 |= _MASK & ~(_MASK >> (n - _BITS));
}
res0 = _shiftRight(_m, n - _BITS) | (a2 << (_BITS01 - n));
} else {
res2 = negative ? _MASK_2 : 0;
res1 = negative ? _MASK : 0;
res0 = _shiftRight(a2, n - _BITS01);
if (negative) {
res0 |= _MASK & ~(_MASK >> (n - _BITS01));
}
}
return new int64._bits(res0 & _MASK, res1 & _MASK, res2 & _MASK_2);
}
Code bool operator ==(other) #
Code bool operator <(other) #
bool operator <(other) {
return this.compareTo(other) < 0;
}
Code int64 operator <<(int n) #
int64 operator <<(int n) {
if (n < 0) {
throw new IllegalArgumentException("$n");
}
n &= 63;
int res0, res1, res2;
if (n < _BITS) {
res0 = _l << n;
res1 = (_m << n) | (_l >> (_BITS - n));
res2 = (_h << n) | (_m >> (_BITS - n));
} else if (n < _BITS01) {
res0 = 0;
res1 = _l << (n - _BITS);
res2 = (_m << (n - _BITS)) | (_l >> (_BITS01 - n));
} else {
res0 = 0;
res1 = 0;
res2 = _l << (n - _BITS01);
}
return new int64._bits(res0 & _MASK, res1 & _MASK, res2 & _MASK_2);
}
Code int64 operator -(other) #
int64 operator -(other) {
int64 o = _promote(other);
int sum0 = _l - o._l;
int sum1 = _m - o._m + _shiftRight(sum0, _BITS);
int sum2 = _h - o._h + _shiftRight(sum1, _BITS);
int64 result = new int64._bits(sum0 & _MASK, sum1 & _MASK, sum2 & _MASK_2);
return result;
}
Code bool operator >(other) #
bool operator >(other) {
return this.compareTo(other) > 0;
}
Code bool operator >=(other) #
bool operator >=(other) {
return this.compareTo(other) >= 0;
}
Code bool operator <=(other) #
bool operator <=(other) {
return this.compareTo(other) <= 0;
}
Code int64 remainder(other) #
int64 remainder(other) {
if (other.isZero()) {
throw new IntegerDivisionByZeroException();
}
int64 o = _promote(other).abs();
_divMod(this, o, true);
return _remainder;
}
Code int64 shiftRightUnsigned(int n) #
int64 shiftRightUnsigned(int n) {
if (n < 0) {
throw new IllegalArgumentException("$n");
}
n &= 63;
int res0, res1, res2;
int a2 = _h & _MASK_2; // Ensure a2 is positive.
if (n < _BITS) {
res2 = a2 >> n;
res1 = (_m >> n) | (a2 << (_BITS - n));
res0 = (_l >> n) | (_m << (_BITS - n));
} else if (n < _BITS01) {
res2 = 0;
res1 = a2 >> (n - _BITS);
res0 = (_m >> (n - _BITS)) | (_h << (_BITS01 - n));
} else {
res2 = 0;
res1 = 0;
res0 = a2 >> (n - _BITS01);
}
return new int64._bits(res0 & _MASK, res1 & _MASK, res2 & _MASK_2);
}
Code List<int> toBytes() #
List<int> toBytes() {
List<int> result = new List<int>(8);
result[0] = _l & 0xff;
result[1] = (_l >> 8) & 0xff;
result[2] = ((_m << 6) & 0xfc) | ((_l >> 16) & 0x3f);
result[3] = (_m >> 2) & 0xff;
result[4] = (_m >> 10) & 0xff;
result[5] = ((_h << 4) & 0xf0) | ((_m >> 18) & 0xf);
result[6] = (_h >> 4) & 0xff;
result[7] = (_h >> 12) & 0xff;
return result;
}
Code String toDebugString() #
String toDebugString() {
return "int64[_l=$_l, _m=$_m, _h=$_h]";
}
Code String toHexString() #
String toHexString() {
int64 x = new int64._copy(this);
if (isZero()) {
return "0";
}
String hexStr = "";
int64 digit_f = new int64.fromInt(0xf);
while (!x.isZero()) {
int digit = x._l & 0xf;
hexStr = "${_hexDigit(digit)}$hexStr";
x = x.shiftRightUnsigned(4);
}
return hexStr;
}
Code int toInt() #
int toInt() {
int l = _l;
int m = _m;
int h = _h;
bool negative = false;
if ((_h & _SIGN_BIT_VALUE) != 0) {
l = ~_l & _MASK;
m = ~_m & _MASK;
h = ~_h & _MASK_2;
negative = true;
}
int result;
if (_haveBigInts) {
result = (h << _BITS01) | (m << _BITS) | l;
} else {
result = (h * 17592186044416) + (m * 4194304) + l;
}
return negative ? -result - 1 : result;
}
Code String toRadixString(int radix) #
String toRadixString(int radix) {
if ((radix <= 1) || (radix > 16)) {
throw "Bad radix: $radix";
}
int64 a = this;
if (a.isZero()) {
return "0";
}
if (a.isMinValue()) {
return _minValues[radix];
}
String result = "";
bool negative = false;
if (a.isNegative()) {
negative = true;
a = -a;
}
int64 r = new int64._bits(radix, 0, 0);
while (!a.isZero()) {
a = _divMod(a, r, true);
result = "${_hexDigit(_remainder._l)}$result";
}
return negative ? "-$result" : result;
}
Code String toString() #
Returns the value of this int64 as a decimal String.
String toString() {
int64 a = this;
if (a.isZero()) {
return "0";
}
if (a.isMinValue()) {
return "-9223372036854775808";
}
String result = "";
bool negative = false;
if (a.isNegative()) {
negative = true;
a = -a;
}
int64 ten = new int64._bits(10, 0, 0);
while (!a.isZero()) {
a = _divMod(a, ten, true);
result = "${_remainder._l}$result";
}
if (negative) {
result = "-$result";
}
return result;
}