math-uint32-to-binary-string

Returns a string giving the literal bit representation of an unsigned 32-bit integer.

Usage no npm install needed!

<script type="module">
  import mathUint32ToBinaryString from 'https://cdn.skypack.dev/math-uint32-to-binary-string';
</script>

README

Binary String

NPM version Build Status Coverage Status Dependencies

Returns a string giving the literal bit representation of an unsigned 32-bit integer.

Installation

$ npm install math-uint32-to-binary-string

Usage

var binaryString = require( 'math-uint32-to-binary-string' );

binaryString( x )

Returns a string giving the literal bit representation of an unsigned 32-bit integer.

var a = new Uint32Array( [ 1, 4, 9 ] );

var str = binaryString( a[0] );
// returns '00000000000000000000000000000001'

str = binaryString( a[1] );
// returns '00000000000000000000000000000100'

str = binaryString( a[2] );
// returns '00000000000000000000000000001001'

Notes

  • Except for typed arrays, JavaScript does not provide native user support for unsigned 32-bit integers. According to the ECMAScript standard, number values correspond to double-precision floating-point numbers. While this function is intended for unsigned 32-bit integers, the function will accept floating-point values and represent the values as if they are unsigned 32-bit integers. Accordingly, care should be taken to ensure that only nonnegative integer values less than 4,294,967,296 (2**32) are provided.

    var str = binaryString( 1 );
    // returns '00000000000000000000000000000001'
    
    str = binaryString( 4 );
    // returns '00000000000000000000000000000100'
    
    str = binaryString( 9 );
    // returns '00000000000000000000000000001001'
    
    str = binaryString( 4294967295 );
    // returns '11111111111111111111111111111111'
    

Examples

var round = require( 'math-round' );
var binaryString = require( 'math-uint32-to-binary-string' );

var x;
var y;
var b;
var i;

// Generate random unsigned 32-bit integers...
x = new Uint32Array( 100 );
for ( i = 0; i < x.length; i++ ) {
    x[ i ] = round( Math.random()*1e5 );
}

// Convert unsigned 32-bit integers to literal bit representations...
for ( i = 0; i < x.length; i++ ) {
    b = binaryString( 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

MIT license.

Copyright

Copyright © 2016. The Compute.io Authors.