Bug: right shift (>>) for Bignum?

Hi!

I think I have stumbled on a bug in the right shift method on Bignums.
An expression like

(1 - 2**i) >> (i+1)

normally gives a value of -1 (i.e. minus one). But when the shift
amount is near a multiple of 32 (or 64 on 64-bit architectures)
it suddenly gives a value of 0 (i.e. zero) instead.

A unit test demonstrating the problem is given below.
On my IMac running OS-X I get an error when i=31.
I am using Ruby 1.8.5.


require “test/unit”

class TC_bignum_rshift_bug < Test::Unit::TestCase
def test_rshift_bug
(1…100).each do |i|
assert_equal( -1,
(1 - 2**i) >> (i+1),
“shift steps: #{i}” )
end
end
end

Regards,
Johan H.

Sorry for the mail here on Ruby-talk.
My intention was to post this on ruby-core,
where I suppose it belongs.

/Johan H.

Hi!

I think I have stumbled on a bug in the right shift method on Bignums.
An expression like

(1 - 2**i) >> (i+1)

normally gives a value of -1 (i.e. minus one). But when the shift
amount is near a multiple of 32 (or 64 on 64-bit architectures)
it suddenly gives a value of 0 (i.e. zero) instead.

A unit test demonstrating the problem is given below.
On my IMac running OS-X I get an error when i=31.
I am using Ruby 1.8.5.


require “test/unit”

class TC_bignum_rshift_bug < Test::Unit::TestCase
def test_rshift_bug
(1…100).each do |i|
assert_equal( -1,
(1 - 2**i) >> (i+1),
“shift steps: #{i}” )
end
end
end

Regards,
Johan H.

Hi,

In message “Re: Bug: right shift (>>) for Bignum?”
on Sat, 7 Oct 2006 05:24:36 +0900, “Johan H.”
[email protected] writes:

|I think I have stumbled on a bug in the right shift method on Bignums.
|An expression like
|
| (1 - 2**i) >> (i+1)
|
|normally gives a value of -1 (i.e. minus one). But when the shift
|amount is near a multiple of 32 (or 64 on 64-bit architectures)
|it suddenly gives a value of 0 (i.e. zero) instead.
|
|A unit test demonstrating the problem is given below.
|On my IMac running OS-X I get an error when i=31.
|I am using Ruby 1.8.5.

It is incredible to see a bug like this. Thank you for finding it.
It’s fixed on the CVS.

						matz.