"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/cuint/README.md" (25 Nov 2016, 6032 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format (assuming markdown format). Alternatively you can here view or download the uninterpreted source code file. A member file download can also be achieved by clicking within a package contents listing on the according byte size field.

C-like unsigned integers for Javascript

Synopsis

Javascript does not natively support handling of unsigned 32 or 64 bits integers. This library provides that functionality, following C behaviour, enabling the writing of algorithms that depend on it. It was designed with performance in mind and tries its best to be as fast as possible. Any improvement is welcome!

How it works

An unsigned 32 bits integer is represented by an object with its first 16 bits (low bits) and its 16 last ones (high bits). All the supported standard operations on the unsigned integer are then performed transparently.

e.g.
10000010000100000100010000100010 (2182104098 or 0x82104422) is represented by:
high=1000001000010000
low= 0100010000100010

NB. In case of overflow, the unsigned integer is truncated to its lowest 32 bits (in case of UINT32) or 64 bits (in case of UINT64).

The same applies to 64 bits integers, which are split into 4 16 bits ones.

Installation

In nodejs:

npm install cuint

In the browser, include the following (file is located in the build directory), and access the constructor with UINT32:

<script src="/your/path/to/uint32.js"></script> ... <script type="text/javascript"> var v1 = UINT32('326648991'); var v2 = UINT32('265443576'); var v1plus2 = v1.add(v2) // 592092567 </script>

Usage

To instantiate an unsigned 32 bits integer, do any of the following:

var UINT32 = require('cuint').UINT32 // NodeJS
UINT32( <low bits>, <high bits> )
UINT32( <number> )
UINT32( '<number>', <radix> ) // radix = 10 by default

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

Important

Most methods do modify the object they are applied to. For instance, the following is equivalent to x += y

UINT(x).add( UINT(y) )

This allows for chaining and reduces the cost of the emulation. To have z = x + y, do the following:

z = UINT(x).clone().add( UINT(y) )

Examples for UINT32

Examples for UINT64

Methods

Methods specific to UINT32 and UINT64:

Methods common to UINT32 and UINT64:

NB. methods with an * do modify the object it is applied to. Input objects are not modified.

TODO

License

MIT

Written with StackEdit.