Skip to content

Commit 0ff67e6

Browse files
author
Peter Cooper
authored
Merge pull request #8 from tdeo/set_0
Fix setting a 0-bit to 0
2 parents 6018faa + cff29e5 commit 0ff67e6

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

lib/bitarray.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def []=(position, value)
1515
if value == 1
1616
@field.setbyte(position >> 3, @field.getbyte(position >> 3) | (1 << (position % 8)))
1717
else
18-
@field.setbyte(position >> 3, @field.getbyte(position >> 3) ^ (1 << (position % 8)))
18+
@field.setbyte(position >> 3, @field.getbyte(position >> 3) & ~(1 << (position % 8)))
1919
end
2020
end
2121

test/test_bitarray.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,19 @@ def test_random_setting_and_unsetting
3030

3131
def test_multiple_setting
3232
1.upto(999) do |pos|
33-
2.times { @public_ba[pos] = 1 }
34-
assert_equal 1, @public_ba[pos]
33+
2.times do
34+
@public_ba[pos] = 1
35+
assert_equal 1, @public_ba[pos]
36+
end
3537
end
3638
end
3739

3840
def test_multiple_unsetting
3941
1.upto(999) do |pos|
40-
2.times { @public_ba[pos] = 0 }
41-
assert_equal 0, @public_ba[pos]
42+
2.times do
43+
@public_ba[pos] = 0
44+
assert_equal 0, @public_ba[pos]
45+
end
4246
end
4347
end
4448

0 commit comments

Comments
 (0)