Node.js library to manipulate bits.
Basic: npm install bitwise
As Dependency: npm install bitwise --save
Require in Node: var bitwise = require('bitwise');
Returns an Array of length 8 containing the read bits.
Example: 42 → [0,0,1,0,1,0,1,0]
.
Returns a Byte (0-255) which equals the given bits.
Example: [0,0,1,0,1,0,1,0] → 42
Returns an Array containing bitLength bits starting at bitOffset.
Example:
var buffer = new Buffer('ED743E17', 'hex');
bitwise.readBuffer(buffer, 12);
// 0100 0011 1110 0001 0111
Modifies the buffer's bits to equal newBits starting at bitOffset.
Example:
var buffer = new Buffer('A43A', 'hex');
bitwise.modifyBuffer(buffer, [0,0,0,1, 0,0,1], 3);
// 1010 1001 0011 1010
Creates a new buffer and writes the given bits.
Example:
var buffer = bitwise.createBuffer([1,1,1,1, 0,0,0,1, 1,0,1,0]);
bitwise.readBuffer(buffer);
// 1111 0001 1010 0000
Converts a string into an array of bits. Ignores all characters except 1 and 0.
Example: '10 10 12$%_.0' → [1,0,1,0,1,0]
Converts a bit array to a string. If defined, inserts spacer
every spacing
characters, but never inserts it as the last substring.
Example: bitwise.toString([1,0,1,0,1,0], 2, '_') → '10_10_10'
Converts a section of a buffer to an unsigned integer.
Example:
// buffer 11110110
bitwise.readUInt(buffer, 3, 5) → 22
Converts a section of a buffer to a signed integer.
Example:
// buffer 11110110
bitwise.readUInt(buffer, 3, 5) → -10
Converts a section of a buffer to a complementary integer. A complementary integer is like an unsigned integer, but always represents negative numbers.
Example:
// buffer 11110110
bitwise.readUInt(buffer, 3, 5) → -22
Flips all given bits and returns the flipped bits.
Example:
bitwise.not([1,0,1,1,0,1]) → [0,1,0,0,1,0]
Applies the AND operation, expects two arrays of the same size and returns a new one.
Example:
bitwise.and([1,0,0,0,1,1,0,1], [0,1,1,0,0,1,0,0]) → [0,0,0,0,0,1,0,0]
Applies the OR operation, expects two arrays of the same size and returns a new one.
Example:
bitwise.or([1,0,0,0,1,1,0,1], [0,1,1,0,0,1,0,0]) → [1,1,1,0,1,1,0,1]
Applies the exclusive or operation, expects two arrays of the same size and returns a new one.
Example:
bitwise.xor([1,0,0,0,1,1,0,1], [0,1,1,0,0,1,0,0]) → [1,1,1,0,1,0,0,1]
Applies the NOR operation, expects two arrays of the same size and returns a new one.
Example:
bitwise.nor([1,0,0,0,1,1,0,1], [0,1,1,0,0,1,0,0]) → [1,1,1,0,1,0,0,1]
Applies the exclusive NOR operation, expects two arrays of the same size and returns a new one.
Example:
bitwise.xnor([1,0,0,0,1,1,0,1], [0,1,1,0,0,1,0,0]) → [1,1,1,0,1,0,0,1]
Applies the NAND operation, expects two arrays of the same size and returns a new one.
Example:
bitwise.nand([1,0,0,0,1,1,0,1], [0,1,1,0,0,1,0,0]) → [1,1,1,0,1,0,0,1]
NOT, OR, NOR, XOR, XNOR, AND, NAND
Proper documentation will (proably) follow later, if you need to know more now, just look into the unit test files.
Example:
var isLooping = true; // -> if first buffer is "empty", it will be read again from its start
var resultBuffer = bitwise.buffer.xor(bufferA, bufferB, isLooping);
- Added buffer bitwise operations
- Added nor, xnor, nand
- Fixed bitwise operations modifying original array
- Re-ordered the arguments in readInt, readCInt, readUInt
- Added not, and, or, xor
- Renamed flipBits to not