javascript / expert
Snippet
Bitwise Index Searching across Segmented Int32Array Buffers
Bitwise operations across contiguous Int32Array binary buffers provide low-overhead bitmask filtering without object allocations.
snippet.js
javascript
1
2
3
4
5
6
7
8
9
10
11
12
13
function findBitmaskMatches(arrayBuffer, mask) {const intArray = new Int32Array(arrayBuffer);const matches = [];for (let i = 0; i < intArray.length; i++) {if ((intArray[i] & mask) === mask) {matches.push(i);}}return matches;}const buffer = new Int32Array([0b1010, 0b1100, 0b1011, 0b0010]).buffer;const indices = findBitmaskMatches(buffer, 0b1010);
nodejs
Breakdown
1
function findBitmaskMatches(arrayBuffer, mask) {
Declares function accepting raw ArrayBuffer and integer bitmask filter.
2
const intArray = new Int32Array(arrayBuffer);
Constructs 32-bit signed integer typed view over the raw buffer.
3
const matches = [];
Initializes results array for matching buffer element indices.
4
for (let i = 0; i < intArray.length; i++) {
Loops over typed array elements sequentially in memory.
5
if ((intArray[i] & mask) === mask) {
Applies bitwise AND check against target bitmask.
6
matches.push(i);
Appends matching array index to results list.
7
}
Closes condition block.
8
}
Closes typed array loop block.
9
return matches;
Returns collection of matching indices.
10
}
Closes function declaration.
11
const buffer = new Int32Array([0b1010, 0b1100, 0b1011, 0b0010]).buffer;
Allocates ArrayBuffer initialized with binary bit pattern values.
12
const indices = findBitmaskMatches(buffer, 0b1010);
Executes bitmask search over binary buffer.