Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ Gets the data with the integers holding the bits information. Ex: `new BitSet("1

Returns the total number of bits set to 1 in this BitSet.

**and(other)**

Combines this BitSet with another BitSet of the same length using bitwise AND. The current BitSet is updated and returned.

**or(other)**

Combines this BitSet with another BitSet of the same length using bitwise OR. The current BitSet is updated and returned.

**xor(other)**

Combines this BitSet with another BitSet of the same length using bitwise XOR. The current BitSet is updated and returned.

**invert()**

Inverts this BitSet.
Expand Down
21 changes: 21 additions & 0 deletions lib/BitSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,27 @@ let BitSet = (function() {
return pos != null;
}

//combines this BitSet with another using bitwise AND
BitSet.prototype.and = function(other) {
checkOtherParamTypeAndLength.call(this, other);
this[dataSymbol] = this[dataSymbol].map((x, i) => x & other[dataSymbol][i]);
return this;
}

//combines this BitSet with another using bitwise OR
BitSet.prototype.or = function(other) {
checkOtherParamTypeAndLength.call(this, other);
this[dataSymbol] = this[dataSymbol].map((x, i) => x | other[dataSymbol][i]);
return this;
}

//combines this BitSet with another using bitwise XOR
BitSet.prototype.xor = function(other) {
checkOtherParamTypeAndLength.call(this, other);
this[dataSymbol] = this[dataSymbol].map((x, i) => x ^ other[dataSymbol][i]);
return this;
}

//inverts the array of bits
BitSet.prototype.invert = function () {
this[dataSymbol] = this[dataSymbol].map(x => ~x);
Expand Down
27 changes: 27 additions & 0 deletions lib/BitSetES5.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,33 @@ var BitSet = (function () {
return pos != null;
};

//combines this BitSet with another using bitwise AND
BitSet.prototype.and = function (other) {
checkOtherParamTypeAndLength.call(this, other);
this[dataSymbol] = this[dataSymbol].map(function (x, i) {
return x & other[dataSymbol][i];
});
return this;
};

//combines this BitSet with another using bitwise OR
BitSet.prototype.or = function (other) {
checkOtherParamTypeAndLength.call(this, other);
this[dataSymbol] = this[dataSymbol].map(function (x, i) {
return x | other[dataSymbol][i];
});
return this;
};

//combines this BitSet with another using bitwise XOR
BitSet.prototype.xor = function (other) {
checkOtherParamTypeAndLength.call(this, other);
this[dataSymbol] = this[dataSymbol].map(function (x, i) {
return x ^ other[dataSymbol][i];
});
return this;
};

//inverts the array of bits
BitSet.prototype.invert = function () {
this[dataSymbol] = this[dataSymbol].map(function (x) {
Expand Down
32 changes: 32 additions & 0 deletions spec/bitset.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,35 @@ describe('Initialize a bitSet with a string', () => {


});

describe('Bitwise operations', () => {

it('combines BitSets with AND', () => {
let bitset = new BitSet("1100");
let other = new BitSet("1010");

expect(bitset.and(other).stringify()).toBe("1000");
});

it('combines BitSets with OR', () => {
let bitset = new BitSet("1100");
let other = new BitSet("1010");

expect(bitset.or(other).stringify()).toBe("1110");
});

it('combines BitSets with XOR', () => {
let bitset = new BitSet("1100");
let other = new BitSet("1010");

expect(bitset.xor(other).stringify()).toBe("0110");
});

it('rejects BitSets with different lengths', () => {
let bitset = new BitSet("1100");
let other = new BitSet("10");

expect(() => bitset.and(other)).toThrow("Only can modify BitSets with the same length");
});

});