Skip to content

Javascript class implementation of a BitArray extending DataView

License

Notifications You must be signed in to change notification settings

Atriace/JSBitArray

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BitArray

Written by Redu, with contributions by Atriace due to the question of "How do I create a bit array in Javascript"

This is a Javascript class implementation of a BitArray extending DataView. It implements bit masks to access the bits which are technically stored as Bytes in the DataView. Conveniently, it also offers standard boolean operations.

Constructor: BitArray(sizeOrBuffer)

sizeOrBuffer : Either a pre-existing ArrayBuffer or a Number

Syntax
new BitArray(buffer);
new BitArray(number);

Examples

Starting with something simple...

var a = new BitMask(4);
a.set(0, true);
a.set(3, true);

console.log(a); // 1001

Basic proofs...

let a = new BitArray(4);
let b = new BitArray(4);

// Populate values
let aState = false;
let bState = true;
for (let i = 0; i < a.length; i++) {
	let curVal = (i % (a.length/2));
	if (curVal == 0) {
		aState = !aState;
	}
	a.set(i, aState);

	let bVal = i % 1;
	if (bVal == 0) {
		bState = !bState;
	}
	b.set(i, bState);
}

// Let's see the starting state
console.log(`a: ${a}, length: ${a.length}, popcount:${a.popcount}`);
console.log(`b: ${b}, length: ${b.length}, popcount:${b.popcount}`);

/* LOG
    a: 1100, length: 4, popcount:2
    b: 0101, length: 4, popcount:2
*/

Operators

let oAnd = b.and(a, true);
let oOr  = b.or(a, true);
let oXor = b.xor(a, true);
let oNot = b.not(true);

console.log(`and: ${oAnd}, length: ${oAnd.length}, popcount:${oAnd.popcount}`);
console.log(`or: ${oOr}, length: ${oOr.length}, popcount:${oOr.popcount}`);
console.log(`xor: ${oXor}, length: ${oXor.length}, popcount:${oXor.popcount}`);
console.log(`not: ${oNot}, length: ${oNot.length}, popcount:${oNot.popcount}`);

/* LOG
  and: 0100, length: 4, popcount:1
  or:  1101, length: 4, popcount:3
  xor: 1001, length: 4, popcount:2
  not: 1010, length: 4, popcount:6
*/

About

Javascript class implementation of a BitArray extending DataView

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published