Skip to content

setRange sets the wrong bits when the range crosses a 32-bit word (regression in 5.3.0) #51

Open

Description

@dennisfoconnor

Since 5.3.0, setRange(from, to, value) sets only part of the range whenever it spans more than one 32-bit word. 5.2.3 handles the same calls correctly.

Reproduction (Node 26.13.1, bitset 5.3.0):

const BitSet = require('bitset');

const b = new BitSet();
b.setRange(0, 63, 1);
console.log(b.cardinality()); // 5.3.0: 32   5.2.3: 64
console.log(b.toString(2));   // 5.3.0: only bits 0–31 are set

Comparing setRange against setting the same bits one at a time:

const BitSet = require('bitset');
let mismatches = 0;
for (let from = 0; from < 200; from++) {
  for (let to = from; to < 200; to += 7) {
    const a = new BitSet(); a.setRange(from, to, 1);
    const b = new BitSet(); for (let i = from; i <= to; i++) b.set(i, 1);
    if (!a.equals(b)) mismatches++;
  }
}
console.log(mismatches); // 5.3.0: 99   5.2.3: 0

Ranges that stay inside one word are correct (setRange(10, 20, 1) gives 11 bits); ranges that cross a word boundary lose bits, e.g. setRange(1, 127, 1) gives 95 instead of 127.

Expected: every bit from from to to inclusive is set, as in 5.2.3 and as the README describes.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions