Skip to content

Commit

Permalink
Fix setting a 0-bit to 0
Browse files Browse the repository at this point in the history
  • Loading branch information
tdeo committed Jul 11, 2018
1 parent 6018faa commit cff29e5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/bitarray.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def []=(position, value)
if value == 1
@field.setbyte(position >> 3, @field.getbyte(position >> 3) | (1 << (position % 8)))
else
@field.setbyte(position >> 3, @field.getbyte(position >> 3) ^ (1 << (position % 8)))
@field.setbyte(position >> 3, @field.getbyte(position >> 3) & ~(1 << (position % 8)))
end
end

Expand Down
12 changes: 8 additions & 4 deletions test/test_bitarray.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,19 @@ def test_random_setting_and_unsetting

def test_multiple_setting
1.upto(999) do |pos|
2.times { @public_ba[pos] = 1 }
assert_equal 1, @public_ba[pos]
2.times do
@public_ba[pos] = 1
assert_equal 1, @public_ba[pos]
end
end
end

def test_multiple_unsetting
1.upto(999) do |pos|
2.times { @public_ba[pos] = 0 }
assert_equal 0, @public_ba[pos]
2.times do
@public_ba[pos] = 0
assert_equal 0, @public_ba[pos]
end
end
end

Expand Down

0 comments on commit cff29e5

Please sign in to comment.