How to use long

Comprehensive long code examples:

How to use long.valueOf:

78
79
80
81
82
83
84
85
86
87
88
89
 * meta is up-to-date.
 */
CONST.META_VERSION = 0;


/** long constant for zero */
// CONST.ZERO_L = Long.valueOf(0L);
CONST.NINES = "99999999999999";
CONST.ZEROES = "00000000000000";


// Other constants

How to use long.MIN_VALUE:

24
25
26
27
28
29
30
31
32
33
 */
CONST.LATEST_TIMESTAMP = Long.MAX_VALUE;
/**
 * Timestamp to use when we want to refer to the oldest cell.
 */
CONST.OLDEST_TIMESTAMP = Long.MIN_VALUE;
/**
 * LATEST_TIMESTAMP in bytes form
 */
CONST.LATEST_TIMESTAMP_BYTES = Bytes.toBytes(CONST.LATEST_TIMESTAMP);

How to use long.MAX_VALUE:

20
21
22
23
24
25
26
27
28
29
/**
 * Timestamp to use when we want to refer to the latest cell.
 * This is the timestamp sent by clients when no timestamp is specified on
 * commit.
 */
CONST.LATEST_TIMESTAMP = Long.MAX_VALUE;
/**
 * Timestamp to use when we want to refer to the oldest cell.
 */
CONST.OLDEST_TIMESTAMP = Long.MIN_VALUE;

How to use long.isLong:

98
99
100
101
102
103
104
105
106
107

log.info('Creating two users');
const venueInfo1 = getVenueInfoFromQrCodeV3(qrCode1);
const starTimeUnconverted1 = location1.qrCodePayload.locationData!
    .startTimestamp!;
const startTime1: number = Long.isLong(starTimeUnconverted1) ?
      (starTimeUnconverted1 as Long).toNumber() :
      (starTimeUnconverted1 as number);
const arrivalTime1 = startTime1 + 60 * 60 * 2;
const departureTime1 = arrivalTime1 + 60 * 60 * 1; // stayed for an hour

How to use long.MAX_UNSIGNED_VALUE:

6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
} else if (unsigned && value <= 0) {
    return Long.MIN_UNSIGNED_VALUE;
} else if (!unsigned && value + 1 >= TWO_PWR_63_DBL) {
    return Long.MAX_SIGNED_VALUE;
} else if (unsigned && value >= TWO_PWR_64_DBL) {
    return Long.MAX_UNSIGNED_VALUE;
} else if (value < 0) {
    return Long.fromNumber(-value, false).negate();
} else {
    return new Long((value % TWO_PWR_32_DBL) | 0, (value / TWO_PWR_32_DBL) | 0, unsigned);

How to use long.MIN_UNSIGNED_VALUE:

6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
if (isNaN(value) || !isFinite(value)) {
    return Long.ZERO;
} else if (!unsigned && value <= -TWO_PWR_63_DBL) {
    return Long.MIN_SIGNED_VALUE;
} else if (unsigned && value <= 0) {
    return Long.MIN_UNSIGNED_VALUE;
} else if (!unsigned && value + 1 >= TWO_PWR_63_DBL) {
    return Long.MAX_SIGNED_VALUE;
} else if (unsigned && value >= TWO_PWR_64_DBL) {
    return Long.MAX_UNSIGNED_VALUE;

How to use long.ONE:

6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
var approx, rem, res;
if (this.equals(Long.MIN_SIGNED_VALUE)) {
    if (other.equals(Long.ONE) || other.equals(Long.NEG_ONE)) {
        return Long.MIN_SIGNED_VALUE;  // recall that -MIN_VALUE == MIN_VALUE
    } else if (other.equals(Long.MIN_SIGNED_VALUE)) {
        return Long.ONE;
    } else {
        // At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|.
        var halfThis = this.shiftRight(1);
        approx = halfThis.div(other).shiftLeft(1);

How to use long.MAX_SIGNED_VALUE:

83
84
85
86
87
88
89
90
91
    expect(check.sint32).to.exist;
    expect(check.sint32).to.deep.equal(decoded);
});

it('Can encode the max int64', function () {
    decoded = long.MAX_SIGNED_VALUE;
    encoded = client.encode('Test1', { int64: decoded });
    expect(encoded).to.be.an.instanceof(Buffer);
});

How to use long.fromBytesLE:

182
183
184
185
186
187
188
189
190
}

readLong() {
    const size = BinaryUtils.getSize(BinaryUtils.TYPE_CODE.LONG)
    this._ensureSize(size);
    const value = Long.fromBytesLE([...this._buffer.slice(this._position, this._position + size)]);
    this._position += size;
    return value;
}

How to use long.MIN_SIGNED_VALUE:

95
96
97
98
99
100
101
102
103
    expect(check.int64).to.exist;
    expect(check.int64).to.deep.equal(decoded);
});

it('Can encode the min int64', function () {
    decoded = long.MIN_SIGNED_VALUE;
    encoded = client.encode('Test1', { int64: decoded });
    expect(encoded).to.be.an.instanceof(Buffer);
});

How to use long.fromValue:

83
84
85
86
87
88
89
90
91
92
}

writeLong(value) {
    try {
        if (!Long.isLong(value)) {
            value = Long.fromValue(value);
        }
    }
    catch (err) {
        throw Errors.IgniteClientError.valueCastError(value, BinaryUtils.TYPE_CODE.LONG);

How to use long.ZERO:

303
304
305
306
307
308
309
310
311
312
BigInteger.prototype._destructiveMulAdd = function (x, y, z) {
  // Perform the multiplication word by word
  var ylong = Long.fromNumber(y >>> 32);
  var zlong = z >>> 32;
  var len = x.length;
  var product = Long.ZERO;
  var carry = 0;
  for (var i = len-1; i >= 0; i--) {
    
    product = ylong.multiply( Long.fromNumber(x[i] >>> 32) ).add(Long.fromInt(carry));

How to use long.fromInt:

46
47
48
49
50
51
52
53
54
55
    createValidatorSetUpdateMock,
  );
});

it('should rotate validator set and return ValidatorSetUpdate if height is divisible by ROTATION_BLOCK_INTERVAL', async () => {
  height = Long.fromInt(15);

  const quorumHash = Buffer.alloc(64).fill(1).toString('hex');

  validatorSetMock.rotate.resolves(true);

How to use long.fromBits:

7
8
9
10
11
12
13
14
15
16
// Demonstrating the use together with Long.js (https://github.com/dcodeIO/Long.js)
//
// Packing a long requires the input of a 2 part array containing the [low, high] bits
// of the specific long value.
// Unpacking a long results in a 3 part array containing [low, high, unsigned] bits and flag.
// The decoded value can be applied directly to Long.fromBits()
//
// Test number u            228290380562207 (BE: 0x00, 0x00, 0xcf, 0xa0, 0xff, 0x09, 0xff, 0x1f)
//                                          (LE: 0x1f, 0xff, 0x09, 0xff, 0xa0, 0xcf, 0x00, 0x00)
// Test number s           -228290380562207 (BE: 0xff, 0xff, 0x30, 0x5f, 0x00, 0xf6, 0x00, 0xe1)

How to use long.fromString:

253
254
255
256
257
258
259
260
261
262
var MAX_CONSTANT = 16;

var longRadix = [null, null,
  Long.fromString('4000000000000000',16), Long.fromString('383d9170b85ff80b',16),
  Long.fromString('4000000000000000',16), Long.fromString('6765c793fa10079d',16),
  Long.fromString('41c21cb8e1000000',16), Long.fromString('3642798750226111',16),
  Long.fromString('1000000000000000',16), Long.fromString('12bf307ae81ffd59',16),
  Long.fromString( 'de0b6b3a7640000',16), Long.fromString('4d28cb56c33fa539',16),
  Long.fromString('1eca170c00000000',16), Long.fromString('780c7372621bd74d',16),
  Long.fromString('1e39a5057d810000',16), Long.fromString('5b27ac993df97701',16),

How to use long.prototype:

6594
6595
6596
6597
6598
6599
6600
6601
6602
6603

/**
 * @return {number} The value, assuming it is a 32-bit integer.
 * @expose
 */
Long.prototype.toInt = function() {
    return this.unsigned ? this.low >>> 0 : this.low;
};

/**

How to use long.Long:

4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
    if (0 <= value && value < 256) {
        cachedObj = UINT_CACHE[value];
        if (cachedObj)
            return cachedObj;
    }
    obj = new Long(value, (value | 0) < 0 ? -1 : 0, true);
    if (0 <= value && value < 256)
        UINT_CACHE[value] = obj;
    return obj;
}

How to use long.fromNumber:

13
14
15
16
17
18
19
20
21
22
23
24
25
var Bytes = require('./util/bytes');


var CONST = {};


CONST.PROTOCOL = 'org.apache.hadoop.hbase.ipc.HRegionInterface';
CONST.CLIENT_VERSION = Long.fromNumber(29);


/**
 * Timestamp to use when we want to refer to the latest cell.
 * This is the timestamp sent by clients when no timestamp is specified on

How to use long.default:

1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
    };
},
toJSON(message) {
    const obj = {};
    message.transactionId !== undefined &&
        (obj.transactionId = (message.transactionId || long_1.default.UZERO).toString());
    message.sender !== undefined && (obj.sender = message.sender);
    return obj;
},
fromPartial(object) {