s-binary
Work in progress
Usage example
var binary = require('s-binary');
binary.toInt('11001100');
var five = binary.toBinary(5, 8);
var nine = binary.toBinary(9, 8);
var negativeNine = binary.complement(nine);
var sum = binary.add(five, negativeNine);
var checkAnswer = binary.complement(sum);
binary.toInt(checkAnswer);
Installation
npm install --save s-binary
Available methods
This module assumes big-endian byte order
Unit conversion
binary to integer
binary.toInt('11001100');
binary to hex
binary.toHex('11001100');
binary to Unicode
binary.toUnicode('0110100001101001');
string/integer/buffer to binary
binary.toBinary(204);
binary.toBinary('foo');
Arithmetic
Simple addition
binary.add('111', '1');
Bit addition
binary.addBits('1', '1');
Multiplication
binary.multiply('1010', '10');
Division
binary.divide('1010', '101');
Logic
NOT
binary.not('1011');
AND
binary.and('101', '110');
OR
binary.or('101', '110');
XOR
binary.xor('101', '110');
NAND
binary.nand('101', '110');
NOR
binary.nor('101', '110');
Two's complement
binary.complement('00000101');
Helper methods
(Left) pad
binary.pad('01', 4);
Equalize lengths by padding shorter value
binary.equalize('11', '0001');
Split
binary.split('1111111100000000', 8);
LSB (get least significant bits)
Last bit from each byte. Note: bytes can be of any length.
var data = ['01000100', '11001100', '01010101'];
binary.lsb(data);