Numeric types
decoder.Int64 / decoder.UInt64
JavaScript represents numeric variables using IEEE double precision floating point numbers which are not capable of storing the full numeric range supported by 64-bit integers. The types decoder.UInt64 and decoder.Int64 are thus provided to enable such types to be used and passed to Datafields. Both objects contain the same methods and thus only decoder.Int64 is documented but notes pertaining to both are given.
decoder.UInt64 stores an unsigned integer in the range 0 to 2^64
decoder.Int64 stores an unsigned integer in the range -2^63 to 2^63
Please note that the multiple methods for working with both int32 and uint32 values are due to the v8 JavaScript mechanism for representing numbers, which is able to distinguish between the two in order to increase the address space of unsigned integers.
Int64 constructor
Construct a new Int64 and sets its initial value to zero.
var i =
new
decoder.Int64()
Int64.setZero() : Void
Reset the value of the integer to zero. "= 0" cannot be used as JavaScript does not support operator overloading. This would overwrite the Int64 with a JavaScript number.
var i =
new
decoder.Int64()
i.setZero()
// redundant
Int64.isZero() : Boolean
Tests whether the Int64 is zero or not.
var i =
new
decoder.Int64()
i.setZero()
i.isZero()
// true
i.incrementBy(
100
)
i.isZero()
// false
Int64.toInt32() : Number
Convert the value the Int64 represents into a simple JavaScript number. The Int64 must contain a value above -2^16 and below 2^16 to be representable as a JavaScript number.
var i =
new
decoder.Int64()
i.fromInt32(
999
)
var jsI = i.toInt32()
jsI +=
64
// can now be manipulated using JavaScript methods.
Int64.toUInt32() : Number
Convert the value the Int64 represents into a simple JavaScript number. The Int64 must contain a value less than 2^32. Whilst this method works on an Int64 its semantics are more suitable using with the UInt64 type.
var i =
new
decoder.Int64()
i.fromInt32(
999
)
var jsI = i.toUInt32()
jsI +=
64
// can now be manipulated using JavaScript methods.
Int64.mostSignificantBits() : Number
Get the 32 most significant bits as a JavaScript number.
var i =
new
decoder.Int64()
i.set(Math.pow(
2
,
53
))
if
(i.mostSignificantBits() !== Math.pow(
2
,
53
-
32
))
throw
new
Error(
"This is impossible"
)
Int64.leastSignificantBits() : Number
Get the 32 least significant bits from the Int64 as a JavaScript number.
var i =
new
decoder.Int64()
i.set(Math.pow(
2
,
33
))
i.increment(
459
)
if
(i.leastSignificantBits() !==
459
)
throw
new
Error(
"This is impossible"
)
Int64.bitIsSet(n : Number) : Boolean
Returns true if and only if the nth bit is set (where 0 is the least significant bit and 63 is the most significant bit).
var i =
new
decoder.Int64()
i.set(Math.pow(
2
,
33
))
i.increment(
3
)
if
(! (i.bitIsSet(
0
) && i.bitIsSet(
1
) && i.bitIsSet(
33
))
throw
new
Error(
"This is impossible"
)
Int64.fromUInt32(value : Number) : Void
Set the Int64 from the JavaScript number value. The JavaScript number must currently contain a non-negative integer below 2^32. In most instances it is advised to call set instead which will forward to the appropriate setter based on the type of the argument.
var i =
new
decoder.Int64()
i.fromUInt32(
40000
)
Int64.fromInt32(value : Number) : Void
Set the Int64 from the JavaScript number value. The JavaScript number must currently contain an unsigned integer that may be positive or negative between -2^16 and 2^16. In most cases set should be used.
var i =
new
decoder.Int64()
i.fromInt32(-
40000
)
Int64.fromInt64(value : Int64) : Void
Set the Int64 another Int64. In most cases set should be used.
var src =
new
decoder.Int64()
var dest =
new
decoder.Int64()
src.set(
100
)
dest.fromInt64(i2)
Int64.fromUInt64(value : UInt64) : Void
Set the Int64 a UInt64. In most cases set should be used. If the UInt64 is greater than 2^63 then an overflow exception is raised.
var src =
new
decoder.UInt64()
var dest =
new
decoder.Int64()
src.set(
100
)
dest.fromUInt64(i2)
Int64.set(value : UInt64 or Int64 or Number) : Void
This method forwards to fromUInt64, fromInt64, fromUInt32 or fromInt32 depending on the type of the argument.
var src1 =
new
decoder.UInt64()
var src2 =
new
decoder.Int64()
var src3 =
10
var dest =
new
decoder.Int64()
dest.set(src1)
dest.set(src2)
dest.set(src3)
Int64.multiply(value : UInt64 or Int64 or Number) : Void
This method sets the value to the product of itself with value.
var i =
new
decoder.UInt64()
i.set(
14
)
dest.multiply(
2
)
Int64.divide(value : UInt64 or Int64 or Number) : Void
This method sets the value to the division of itself with value.
var i =
new
decoder.UInt64()
i.set(
14
)
dest.divide(
2
)
Int64.increment(value : Int64 or UInt64 or Number) : Void
Increment the value of the Int64 by a JavaScript number or large int wrapper. To decrement the Int64 a negative number can be passed to this method.
var i =
new
decoder.Int64()
i.setZero()
i.increment(
100
)
i.increment(-
50
)
// i now represents a value of 50
var largeInc =
new
decoder.UInt64(Math.pow(
2
,
53
))
largeInc.multiplyBy(
2
)
i.increment(largeInc)
Int64.consume(buffer : decoder.Buffer) : Void
This method decodes the first 8 bytes from the current pointer of buffer in network-byte order and stores this value in the object. It then advances the "current" Buffer pointer by 8 bytes such that it points immediately after the decoded value.
var i =
new
decoder.Int64()
function on_data(dec) {
// decode and log all integers in the buffer
while
(dec.buffer.sizeGreaterThanOrEqualTo(
8
)) {
i.consume(dec.buffer)
console.log(i.get())
}
}
Int64.consumeLE(buffer : decoder.Buffer) : Void
Int64.consumeLE is identical to Int64.consume but the integer is decoded not using network-byte order but little-endian (hence LE) byte order.
Int64.decode(buffer : decoder.Buffer, offset: Number) : Void
This method is the same as Int64.consume() but with two distinctions:
After decoding the integer buffer's "current" pointer is not advanced but remains pointing at the data that was just decoded.
An offset is supplied. This must be a positive integer and relates to the position in the buffer at which to decode the buffer.
var i =
new
decoder.Int64()
function on_data(dec) {
// decode and log the integer at an offset of 2 from the buffer "current" pointer.
if
(dec.buffer.sizeGreaterThan(
12
)) {
i.decode(dec.buffer,
2
)
console.log(i.get())
}
}
Int64.decodeLE(buffer : decoder.Buffer) : Void
Int64.decodeLE is identical to Int64.decode but the integer is decoded not using network-byte order but little-endian (hence LE) byte order.