numbits

0.1.0 • Public • Published

numbits

JavaScript's native bit math operations, exposed as functions with names.

Why names?

  • Easier for readers of your code to distinguish bit math from accidential typo.
  • Easier for your linter to guess whether it's a typo.

Is it slower?

  • Dunno, probably depends on your optimizer.

Usage

from usage.js:

var bit = require('numbits'), eq = require('assert').deepStrictEqual,
  a = 23, b = 42;
function expect(func, result) { eq(func(a, b), +result); }
 
eq([a, b],    [ 0b00010111,
                0b00101010 ]);
expect(bit.and, 0b00000010 );
expect(bit.or,  0b00111111 );
expect(bit.xor, 0b00111101 );
 
eq(a,           0b00010111 );
expect(bit.not, --1 );
 
= 1;
expect(bit.shl,    0b00101110 );   // alias: .shup (shift up)
eq(a,            0b00010111 );
expect(bit.shr, 0b00001011 );     // alias: .shdn (shift down)
 
= 3;
expect(bit.shup,       0b10111000 );
eq(a,               0b00010111 );
expect(bit.shdn, 0b00000010 );

License

ISC

Package Sidebar

Install

npm i numbits

Weekly Downloads

1

Version

0.1.0

License

ISC

Last publish

Collaborators

  • mk-pmb