Skip to content

Commit

Permalink
Fix bug where set operations affect two bits (64/32 bit issue)
Browse files Browse the repository at this point in the history
  • Loading branch information
brendon9x authored and tyler committed Feb 26, 2011
1 parent f5b378b commit 8efca8f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions ext/bitset/bitset.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ void raise_index_error() {
rb_raise(rb_eIndexError, "Index out of bounds");
}

#define _bit_segment(bit) ((bit) >> 6)
#define _bit_mask(bit) (1 << ((bit) & 0x3f))
#define _bit_segment(bit) ((bit) >> 6UL)
#define _bit_mask(bit) (1UL << ((bit) & 0x3f))

void validate_index(Bitset * bs, int idx) {
if(idx < 0 || idx >= bs->len)
Expand Down
10 changes: 10 additions & 0 deletions spec/bitset_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@
bs.clear?(0,2,3,6).should == true
end

it 'returns works with the full range of 64 bit values' do
bs = Bitset.new(68)
bs.set 0, 2, 66
bs.clear?(32, 33, 34).should == true
end

it 'returns False if not all bits indexed are clear' do
bs = Bitset.new(8)
bs.set 1, 4
Expand Down Expand Up @@ -231,6 +237,10 @@
bs = Bitset.new(4)
bs.set 0, 2
bs.to_s.should == "1010"

bs = Bitset.new(68)
bs.set 0, 2, 66
bs.to_s.should == "101" + ("0" * 63) + "10"
end
end

Expand Down

0 comments on commit 8efca8f

Please sign in to comment.