From 86711b9bf0dff8ff13a981adcd733ea8ac3526ca Mon Sep 17 00:00:00 2001 From: Maxi Benedetto Date: Tue, 1 Sep 2026 08:04:00 +0000 Subject: [PATCH] Add bitwise AND OR and XOR operations --- README.md | 12 ++++++++++++ lib/BitSet.js | 21 +++++++++++++++++++++ lib/BitSetES5.js | 27 +++++++++++++++++++++++++++ spec/bitset.test.js | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+) diff --git a/README.md b/README.md index 93b8aca..1e6c29d 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/BitSet.js b/lib/BitSet.js index 9fbb704..91cef8b 100644 --- a/lib/BitSet.js +++ b/lib/BitSet.js @@ -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); diff --git a/lib/BitSetES5.js b/lib/BitSetES5.js index 8e22b0e..d9ef729 100644 --- a/lib/BitSetES5.js +++ b/lib/BitSetES5.js @@ -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) { diff --git a/spec/bitset.test.js b/spec/bitset.test.js index 1782ead..df303f4 100644 --- a/spec/bitset.test.js +++ b/spec/bitset.test.js @@ -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"); + }); + +});