math-uint16-bits v1.0.0
Bits
Returns a string giving the literal bit representation of an unsigned 16-bit integer.
Installation
$ npm install math-uint16-bits
Usage
var bits = require( 'math-uint16-bits' );
bits( x )
Returns a string
giving the literal bit representation of an unsigned 16-bit integer.
var a = new Uint16Array( [ 1, 4, 9 ] );
var str = bits( a[0] );
// returns '0000000000000001'
str = bits( a[1] );
// returns '0000000000000100'
str = bits( a[2] );
// returns '0000000000001001'
Notes
Except for [typed arrays][typed-arrays], JavaScript does __not__ provide native user support for [unsigned 16-bit integers][integer]. According to the [ECMAScript standard][ecma-262], `number` values correspond to [double-precision floating-point numbers][ieee754]. While this `function` is intended for [unsigned 16-bit integers][integer], the `function` will accept [floating-point][ieee754] values and represent the values __as if__ they are [unsigned 16-bit integers][integer]. Accordingly, care __should__ be taken to ensure that __only__ nonnegative integer values less than `65536` (`2**16`) are provided. ``` javascript var str = bits( 1 ); // returns '0000000000000001' str = bits( 4 ); // returns '0000000000000100' str = bits( 9 ); // returns '0000000000001001' str = bits( 65535 ); // returns '1111111111111111' ```
Examples
var round = require( 'math-round' );
var MAX_UINT16 = require( 'const-max-uint16' );
var bits = require( 'math-uint16-bits' );
var x;
var y;
var b;
var i;
// Generate random unsigned 16-bit integers...
x = new Uint16Array( 100 );
for ( i = 0; i < x.length; i++ ) {
x[ i ] = round( Math.random()*MAX_UINT16 );
}
// Convert unsigned 16-bit integers to literal bit representations...
for ( i = 0; i < x.length; i++ ) {
b = bits( x[i] );
y = parseInt( b, 2 );
console.log( 'x: %d, b: %s, y: %d', x[i], b, y );
To run the example code from the top-level application directory,
$ node ./examples/index.js
Tests
Unit
This repository uses tape for unit tests. To run the tests, execute the following command in the top-level application directory:
$ make test
All new feature development should have corresponding unit tests to validate correct functionality.
Test Coverage
This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:
$ make test-cov
Istanbul creates a ./reports/coverage
directory. To access an HTML version of the report,
$ make view-cov
Browser Support
This repository uses Testling for browser testing. To run the tests in a (headless) local web browser, execute the following command in the top-level application directory:
$ make test-browsers
To view the tests in a local web browser,
$ make view-browser-tests
License
Copyright
Copyright © 2016. The Compute.io Authors.