# Ruby: shift right operator (\>\>) does not work correctly

**URL:** <https://www.ruby-forum.com/t/ruby-shift-right-operator-does-not-work-correctly/57272>\
**Category:** Ruby\
**Created:** [April 8, 2006, 10:22pm UTC](https://www.ruby-forum.com/t/ruby-shift-right-operator-does-not-work-correctly/57272 "2006-04-08T22:22:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vitalie\_Lazu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/5daacb/32.png) [@Vitalie\_Lazu](https://www.ruby-forum.com/u/Vitalie_Lazu)\
**Post date:** [April 8, 2006, 10:22pm UTC](https://www.ruby-forum.com/t/ruby-shift-right-operator-does-not-work-correctly/57272/1 "2006-04-08T22:22:07Z")

</div>

It seems to be a bug with \>\> operator:

4653896912 \>\> 13 should be 43814 like it is in java or php, but not  
568102.

Tested with ruby 1.8.2 in windows xp and freebsd.

any related issues to this subject?

Best regards,  
Vitalie

---

<div class="post-metadata">

**Author:** ![Vitalie\_Lazu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/5daacb/32.png) [@Vitalie\_Lazu](https://www.ruby-forum.com/u/Vitalie_Lazu)\
**Post date:** [April 8, 2006, 10:52pm UTC](https://www.ruby-forum.com/t/ruby-shift-right-operator-does-not-work-correctly/57272/2 "2006-04-08T22:52:42Z")

</div>

Vitalie Lazu wrote:

> It seems to be a bug with \>\> operator:
> 
> 4653896912 \>\> 13 should be 43814 like it is in java or php, but not  
> 568102.
> 
> Tested with ruby 1.8.2 in windows xp and freebsd.
> 
> any related issues to this subject?
> 
> Best regards,  
> Vitalie

> Math.log(4653896912) / Math.log(2)  
> =\> 32.1157921080915

There you have it, the number 4653896912 cannot be represented in 32  
bits (it needs 33 bits, 34 if you add a sign bit). Java is truncating  
the value:

> (4653896912 & 0xffffffff)  
> =\> 358929616  
> 358929616 \>\> 13  
> =\> 43814

– Jim W.

---

<div class="post-metadata">

**Author:** ![Vitalie\_Lazu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/5daacb/32.png) [@Vitalie\_Lazu](https://www.ruby-forum.com/u/Vitalie_Lazu)\
**Post date:** [April 8, 2006, 11:34pm UTC](https://www.ruby-forum.com/t/ruby-shift-right-operator-does-not-work-correctly/57272/3 "2006-04-08T23:34:24Z")

</div>

Thanks all

---

<div class="post-metadata">

**Author:** ![Vitalie\_Lazu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/5daacb/32.png) [@Vitalie\_Lazu](https://www.ruby-forum.com/u/Vitalie_Lazu)\
**Post date:** [April 8, 2006, 11:02pm UTC](https://www.ruby-forum.com/t/ruby-shift-right-operator-does-not-work-correctly/57272/4 "2006-04-08T23:02:26Z")

</div>

Vitalie Lazu wrote:

> Vitalie

Why should _anything_ be like java and php?

$ ruby -e ‘puts 4653896912 \>\> 13’  
568102  
$ python -c ‘print 4653896912 \>\> 13’  
568102  
$ ruby -e ‘puts 4653896912.0 / (2**13)’  
568102.650390625  
$ python -c 'print 4653896912.0 / (2**13)’  
568102.650391  
$ echo ‘4653896912.0 / (2^13)’ | bc  
568102  
$ cat t.c  
main(){  
printf("%d\n", 4653896912 \>\> 13);  
}  
$ tcc -run t.c  
568102

Maybe there is a bignum problem in java/php?

Perl (v5.8.7), on the other hand, seems to be in its own world…

$ perl -e ‘printf “%s\n”, 4653896912 \>\> 13’  
524287

---

<div class="post-metadata">

**Author:** ![Vitalie\_Lazu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/5daacb/32.png) [@Vitalie\_Lazu](https://www.ruby-forum.com/u/Vitalie_Lazu)\
**Post date:** [April 9, 2006, 2:08pm UTC](https://www.ruby-forum.com/t/ruby-shift-right-operator-does-not-work-correctly/57272/5 "2006-04-09T14:08:35Z")

</div>

> 14.times do  
> irb#1(main):006:1\* |i|  
> irb#1(main):007:1\* puts “#{x} \>\> #{i} = #{x \>\> i }”  
> irb#1(main):008:1\> end  
> 4653896912 \>\> 0 = 4653896912 looks good  
> 4653896912 \>\> 1 = 2326948456 looks great  
> 4653896912 \>\> 2 = 1163474228 looks fine  
> 4653896912 \>\> 3 = 581737114 looks alright  
> 4653896912 \>\> 4 = 290868557 looks pretty  
> 4653896912 \>\> 5 = 145434278 looks ok  
> 4653896912 \>\> 6 = 72717139 looks nice  
> 4653896912 \>\> 7 = 36358569 looks correct (that is an ugly one)  
> 4653896912 \>\> 8 = 18179284 looks as it should  
> 4653896912 \>\> 9 = 9089642 looks half of the above  
> 4653896912 \>\> 10 = 4544821 looks as expected  
> 4653896912 \>\> 11 = 2272410 looks superb  
> 4653896912 \>\> 12 = 1136205 looks hey I am not a native speaker  
> 4653896912 \>\> 13 = 568102 looks good for ruby and bad for some  
> other  
> programming toys  
> =\> 14

Cheers  
Robert

–  
Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui  
concerne l’univers, je n’en ai pas acquis la certitude absolue.

- Albert Einstein

---

<div class="post-metadata">

**Author:** ![Vitalie\_Lazu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/5daacb/32.png) [@Vitalie\_Lazu](https://www.ruby-forum.com/u/Vitalie_Lazu)\
**Post date:** [April 9, 2006, 12:09am UTC](https://www.ruby-forum.com/t/ruby-shift-right-operator-does-not-work-correctly/57272/6 "2006-04-09T00:09:39Z")

</div>

Joel VanderWerf schrieb:

> Maybe there is a bignum problem in java/php?
> 
> Perl (v5.8.7), on the other hand, seems to be in its own world…
> 
> $ perl -e ‘printf “%s\n”, 4653896912 \>\> 13’  
> 524287

(flori@combinator:~ 0)$ perl -e 'use bignum; printf “%s\n”, 4653896912

> > 13’  
> > 568102
