cuint
Advanced tools
Comparing version 0.0.3 to 0.1.0
@@ -0,1 +1,6 @@ | ||
0.1.0 / 2014-01-03 | ||
================== | ||
* added support for unsigned 64 bits integers | ||
0.0.3 / 2014-01-02 | ||
@@ -2,0 +7,0 @@ ================== |
@@ -20,4 +20,2 @@ /** | ||
var ONE = new UINT32(1) | ||
/** | ||
@@ -277,6 +275,7 @@ * Represents an unsigned 32 bits integer | ||
UINT32.prototype.negate = UINT32.prototype.not = function () { | ||
this._low = ~this._low & 0xFFFF | ||
this._high = ~this._high & 0xFFFF | ||
var v = ( ~this._low & 0xFFFF ) + 1 | ||
this._low = v & 0xFFFF | ||
this._high = (~this._high + (v >>> 16)) & 0xFFFF | ||
return this.add(ONE) | ||
return this | ||
} | ||
@@ -283,0 +282,0 @@ |
@@ -20,4 +20,2 @@ /** | ||
var ONE = new UINT64(1) | ||
/** | ||
@@ -46,3 +44,3 @@ * Represents an unsigned 64 bits integer | ||
if (arguments.length == 1) | ||
if (typeof a16 == 'undefined') | ||
return fromNumber.call(this, a00) | ||
@@ -66,3 +64,3 @@ | ||
function fromBits (a00, a16, a32, a48) { | ||
if (arguments.length == 2) { | ||
if (typeof a32 == 'undefined') { | ||
this._a00 = a00 & 0xFFFF | ||
@@ -350,8 +348,11 @@ this._a16 = a00 >>> 16 | ||
UINT64.prototype.negate = UINT64.prototype.not = function () { | ||
this._a00 = ~this._a00 & 0xFFFF | ||
this._a16 = ~this._a16 & 0xFFFF | ||
this._a32 = ~this._a32 & 0xFFFF | ||
this._a48 = ~this._a48 & 0xFFFF | ||
var v = ( ~this._a00 & 0xFFFF ) + 1 | ||
this._a00 = v & 0xFFFF | ||
v = (~this._a16 & 0xFFFF) + (v >>> 16) | ||
this._a16 = v & 0xFFFF | ||
v = (~this._a32 & 0xFFFF) + (v >>> 16) | ||
this._a32 = v & 0xFFFF | ||
this._a48 = (~this._a48 + (v >>> 16)) & 0xFFFF | ||
return this.add(ONE) | ||
return this | ||
} | ||
@@ -454,2 +455,3 @@ | ||
UINT64.prototype.shiftRight = UINT64.prototype.shiftr = function (n) { | ||
n %= 64 | ||
if (n >= 48) { | ||
@@ -490,2 +492,3 @@ this._a00 = this._a48 >>> (n - 48) | ||
UINT64.prototype.shiftLeft = UINT64.prototype.shiftl = function (n, allowOverflow) { | ||
n %= 64 | ||
if (n >= 48) { | ||
@@ -528,7 +531,29 @@ this._a48 = this._a00 << (n - 48) | ||
UINT64.prototype.rotateLeft = UINT64.prototype.rotl = function (n) { | ||
var v = (this._high << 16) | this._low | ||
v = (v << n) | (v >>> (32 - n)) | ||
this._low = v & 0xFFFF | ||
this._high = v >>> 16 | ||
n %= 64 | ||
if (n == 0) return this | ||
if (n >= 32) { | ||
// A.B.C.D | ||
// B.C.D.A rotl(16) | ||
// C.D.A.B rotl(32) | ||
var v = this._a00 | ||
this._a00 = this._a32 | ||
this._a32 = v | ||
v = this._a48 | ||
this._a48 = this._a16 | ||
this._a16 = v | ||
if (n == 32) return this | ||
n -= 32 | ||
} | ||
var high = (this._a48 << 16) | this._a32 | ||
var low = (this._a16 << 16) | this._a00 | ||
var _high = (high << n) | (low >>> (32 - n)) | ||
var _low = (low << n) | (high >>> (32 - n)) | ||
this._a00 = _low & 0xFFFF | ||
this._a16 = _low >>> 16 | ||
this._a32 = _high & 0xFFFF | ||
this._a48 = _high >>> 16 | ||
return this | ||
@@ -544,7 +569,29 @@ } | ||
UINT64.prototype.rotateRight = UINT64.prototype.rotr = function (n) { | ||
var v = (this._high << 16) | this._low | ||
v = (v >>> n) | (v << (32 - n)) | ||
this._low = v & 0xFFFF | ||
this._high = v >>> 16 | ||
n %= 64 | ||
if (n == 0) return this | ||
if (n >= 32) { | ||
// A.B.C.D | ||
// D.A.B.C rotr(16) | ||
// C.D.A.B rotr(32) | ||
var v = this._a00 | ||
this._a00 = this._a32 | ||
this._a32 = v | ||
v = this._a48 | ||
this._a48 = this._a16 | ||
this._a16 = v | ||
if (n == 32) return this | ||
n -= 32 | ||
} | ||
var high = (this._a48 << 16) | this._a32 | ||
var low = (this._a16 << 16) | this._a00 | ||
var _high = (high >>> n) | (low << (32 - n)) | ||
var _low = (low >>> n) | (high << (32 - n)) | ||
this._a00 = _low & 0xFFFF | ||
this._a16 = _low >>> 16 | ||
this._a32 = _high & 0xFFFF | ||
this._a48 = _high >>> 16 | ||
return this | ||
@@ -551,0 +598,0 @@ } |
{ | ||
"name": "cuint", | ||
"version": "0.0.3", | ||
"version": "0.1.0", | ||
"description": "Unsigned integers for Javascript", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
121
README.md
@@ -7,6 +7,3 @@ # C-like unsigned integers for Javascript | ||
TODO | ||
64 bits integers not supported yet! | ||
## How it works | ||
@@ -49,6 +46,14 @@ | ||
UINT32( <number> ) | ||
UINT32( '<number>' ) | ||
UINT32( '<number>', <radix> ) // radix = 10 by default | ||
## Examples | ||
To instantiate an unsigned 64 bits integer, do any of the following: | ||
var UINT64 = require('cuint').UINT64 // NodeJS | ||
UINT64( <low bits>, <high bits> ) | ||
UINT64( <first low bits>, <second low bits>, <first high bits>, <second high bits> ) | ||
UINT64( <number> ) | ||
UINT64( '<number>', <radix> ) // radix = 10 by default | ||
## Examples for UINT32 | ||
* Using low and high bits | ||
@@ -81,48 +86,92 @@ > `UINT32( 2, 1 ) // 65538` | ||
## Examples for UINT64 | ||
* Using low and high bits | ||
> `UINT64( 2, 1 ) // 4294967298` | ||
> { remainder: null, _a00: 2, _a16: 0, _a32: 1, _a48: 0 } | ||
* Using first/second low and high bits | ||
> `UINT64( 2, 1, 0, 0 ) // 65538` | ||
> { remainder: null, _a00: 2, _a16: 1, _a32: 0, _a48: 0 } | ||
* Using a number (signed 32 bits integer) | ||
> `UINT64( 65538 ) // 65538` | ||
> { remainder: null, _a00: 2, _a16: 1, _a32: 0, _a48: 0 } | ||
* Using a string | ||
> `UINT64( '65538' ) // 65538` | ||
> { remainder: null, _a00: 2, _a16: 1, _a32: 0, _a48: 0 } | ||
* Using another string | ||
> `UINT64( '3266489917' )` | ||
> { remainder: null, _a00: 44605, _a16: 49842, _a32: 0, _a48: 0 } | ||
* Divide 2 unsigned 64 bits integers - note that the remainder is also provided | ||
> `UINT64( 'F00000000000', 16 ).div( UINT64( '800000000000', 16 ) )` | ||
> { remainder: | ||
> { remainder: null, | ||
> _a00: 0, | ||
> _a16: 0, | ||
> _a32: 28672, | ||
> _a48: 0 }, | ||
> _a00: 1, | ||
> _a16: 0, | ||
> _a32: 0, | ||
> _a48: 0 } | ||
## Methods | ||
Methods apply to both _UINT32_ and _UINT64_ unless otherwise specified. | ||
* `UINT32.fromBits(<low bits>, <high bits>)*` | ||
Set the current _UINT32_ object with its low and high bits | ||
* `UINT32.fromNumber(<number>)*` | ||
Set the current _UINT32_ object from a number | ||
* `UINT32.fromString(<string>, <radix>)*` | ||
Set the current _UINT32_ object from a string | ||
* `UINT32.toNumber()` | ||
Convert this _UINT32_ to a number | ||
* `UINT32.toString(<radix>)` | ||
Convert this _UINT32_ to a string | ||
* `UINT32.add(<uint>)*` | ||
Add two _UINT32_. The current _UINT32_ stores the result | ||
* `UINT32.subtract(<uint>)*` | ||
Subtract two _UINT32_. The current _UINT32_ stores the result | ||
* `UINT32.multiply(<uint>)*` | ||
Multiply two _UINT32_. The current _UINT32_ stores the result | ||
* `UINT32.div(<uint>)*` | ||
Divide two _UINT32_. The current _UINT32_ stores the result. | ||
The remainder is made available as the _remainder_ property on the _UINT32_ object. | ||
* `UINT64.fromBits(<low bits>, <high bits>)*` | ||
Set the current _UINT64_ object with its low and high bits | ||
* `UINT64.fromBits(<first low bits>, <second low bits>, <first high bits>, <second high bits>)*` | ||
Set the current _UINT64_ object with all its low and high bits | ||
Set the current _UINT64_ object with its low and high bits | ||
* `UINT.fromNumber(<number>)*` | ||
Set the current _UINT_ object from a number (first 32 bits only) | ||
* `UINT.fromString(<string>, <radix>)` | ||
Set the current _UINT_ object from a string | ||
* `UINT.toNumber()` | ||
Convert this _UINT_ to a number | ||
* `UINT.toString(<radix>)` | ||
Convert this _UINT_ to a string | ||
* `UINT.add(<uint>)*` | ||
Add two _UINT_. The current _UINT_ stores the result | ||
* `UINT.subtract(<uint>)*` | ||
Subtract two _UINT_. The current _UINT_ stores the result | ||
* `UINT.multiply(<uint>)*` | ||
Multiply two _UINT_. The current _UINT_ stores the result | ||
* `UINT.div(<uint>)*` | ||
Divide two _UINT_. The current _UINT_ stores the result. | ||
The remainder is made available as the _remainder_ property on the _UINT_ object. | ||
It can be null, meaning there are no remainder. | ||
* `UINT32.negate()` alias `UINT32.not()` | ||
Negate the current _UINT32_ | ||
* `UINT32.equals(<uint>)` alias `UINT32.eq(<uint>)` | ||
* `UINT.negate()` alias `UINT.not()` | ||
Negate the current _UINT_ | ||
* `UINT.equals(<uint>)` alias `UINT.eq(<uint>)` | ||
Equals | ||
* `UINT32.lessThan(<uint>)` alias `UINT32.lt(<uint>)` | ||
* `UINT.lessThan(<uint>)` alias `UINT.lt(<uint>)` | ||
Less than (strict) | ||
* `UINT32.greaterThan(<uint>)` alias `UINT32.gt(<uint>)` | ||
* `UINT.greaterThan(<uint>)` alias `UINT.gt(<uint>)` | ||
Greater than (strict) | ||
* `UINT32.or(<uint>)*` | ||
* `UINT.or(<uint>)*` | ||
Bitwise OR | ||
* `UINT32.and(<uint>)*` | ||
* `UINT.and(<uint>)*` | ||
Bitwise AND | ||
* `UINT32.xor(<uint>)*` | ||
* `UINT.xor(<uint>)*` | ||
Bitwise XOR | ||
* `UINT32.shiftRight(<number>)*` alias `UINT32.shiftr(<number>)*` | ||
* `UINT.shiftRight(<number>)*` alias `UINT.shiftr(<number>)*` | ||
Bitwise shift right | ||
* `UINT32.shiftLeft(<number>[, <allowOverflow>])*` alias `UINT32.shiftl(<number>[, <allowOverflow>])*` | ||
* `UINT.shiftLeft(<number>[, <allowOverflow>])*` alias `UINT.shiftl(<number>[, <allowOverflow>])*` | ||
Bitwise shift left | ||
* `UINT32.rotateLeft(<number>)*` alias `UINT32.rotl(<number>)*` | ||
* `UINT.rotateLeft(<number>)*` alias `UINT.rotl(<number>)*` | ||
Bitwise rotate left | ||
* `UINT32.rotateRight(<number>)*` alias `UINT32.rotr(<number>)*` | ||
* `UINT.rotateRight(<number>)*` alias `UINT.rotr(<number>)*` | ||
Bitwise rotate right | ||
* `UINT32.clone()` | ||
Clone the current _UINT32_ | ||
* `UINT.clone()` | ||
Clone the current _UINT_ | ||
@@ -129,0 +178,0 @@ NB. methods with an * do __modify__ the object it is applied to. Input objects are not modified. |
@@ -9,3 +9,3 @@ var assert = require('assert') | ||
it('should properly initialize', function (done) { | ||
var u = UINT32(0, 0) | ||
var u = UINT32() | ||
@@ -31,3 +31,3 @@ assert.equal( u._low, 0 ) | ||
describe('0, 1', function () { | ||
describe('1, 0', function () { | ||
it('should properly initialize', function (done) { | ||
@@ -42,3 +42,3 @@ var u = UINT32(1, 0) | ||
describe('1, 0', function () { | ||
describe('0, 1', function () { | ||
it('should properly initialize', function (done) { | ||
@@ -181,2 +181,12 @@ var u = UINT32(0, 1) | ||
describe('8000 with radix 16', function () { | ||
it('should properly initialize', function (done) { | ||
var u = UINT32( '8000', 16 ) | ||
assert.equal( u._low, 32768 ) | ||
assert.equal( u._high, 0 ) | ||
done() | ||
}) | ||
}) | ||
describe('80000000 with radix 16', function () { | ||
@@ -183,0 +193,0 @@ it('should properly initialize', function (done) { |
102363
48
3452
182