# Bug: right shift (\>\>) for Bignum?

**URL:** <https://www.ruby-forum.com/t/bug-right-shift-for-bignum/71314>\
**Category:** Ruby\
**Created:** [October 6, 2006, 10:15pm UTC](https://www.ruby-forum.com/t/bug-right-shift-for-bignum/71314 "2006-10-06T22:15:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Johan\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@Johan\_H](https://www.ruby-forum.com/u/Johan_H)\
**Post date:** [October 6, 2006, 10:15pm UTC](https://www.ruby-forum.com/t/bug-right-shift-for-bignum/71314/1 "2006-10-06T22:15:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Johan\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@Johan\_H](https://www.ruby-forum.com/u/Johan_H)\
**Post date:** [October 6, 2006, 10:23pm UTC](https://www.ruby-forum.com/t/bug-right-shift-for-bignum/71314/2 "2006-10-06T22:23:11Z")

</div>

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

/Johan H.

---

<div class="post-metadata">

**Author:** ![Johan\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@Johan\_H](https://www.ruby-forum.com/u/Johan_H)\
**Post date:** [October 6, 2006, 10:25pm UTC](https://www.ruby-forum.com/t/bug-right-shift-for-bignum/71314/3 "2006-10-06T22:25:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Johan\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@Johan\_H](https://www.ruby-forum.com/u/Johan_H)\
**Post date:** [October 7, 2006, 5:52pm UTC](https://www.ruby-forum.com/t/bug-right-shift-for-bignum/71314/4 "2006-10-07T17:52:22Z")

</div>

Hi,

In message “Re: Bug: right shift (\>\>) for Bignum?”  
on Sat, 7 Oct 2006 05:24:36 +0900, “Johan H.”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

```
