# Operator precedence of exponentiation (\*\*) and complement (~)

**URL:** <https://www.ruby-forum.com/t/operator-precedence-of-exponentiation-and-complement/163923>\
**Category:** Ruby\
**Created:** [March 26, 2009, 7:03am UTC](https://www.ruby-forum.com/t/operator-precedence-of-exponentiation-and-complement/163923 "2009-03-26T07:03:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [March 26, 2009, 7:03am UTC](https://www.ruby-forum.com/t/operator-precedence-of-exponentiation-and-complement/163923/1 "2009-03-26T07:03:04Z")

</div>

The operator precedence table at:

[The Ruby Language](http://phrogz.net/ProgrammingRuby/language.html#table_18.4)

states that the exponentiation operator (\*\*) has higher precedence than  
the complement operator (~). Ditto for operator precedence in Python.

Yet the following program:

x = ~2\*\*3  
print x, “\n” # prints -27 in Ruby; prints -9 in Python

surprised me by printing -27 (I am using ruby 1.8.6).

BTW, the following program prints -9 in both Ruby and Python:

x = ~(2\*\*3)  
print x, “\n”

Is this a Ruby bug?

Thanks,  
/-\

```
  The new Internet Explorer 8 optimised for Yahoo!7: Faster, Safer, 

```

Easier.

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [March 26, 2009, 7:52am UTC](https://www.ruby-forum.com/t/operator-precedence-of-exponentiation-and-complement/163923/2 "2009-03-26T07:52:47Z")

</div>

2009/3/26 Andrew S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Â x = ~2\*\*3

According to the japanese reference  
manual([http://doc.loveruby.net/refm/api/view/spec/operator](http://doc.loveruby.net/refm/api/view/spec/operator)),  
this is not a bug.  
I guess this precedence change was made on ruby 1.8.0

C:\work\ruby168\bin\>ruby -v -e ‘p ~2\*\*3’  
ruby 1.6.8 (2002-12-24) [i586-mswin32]  
-9

C:\work\ruby180\bin\>ruby -v -e ‘p ~2\*\*3’  
ruby 1.8.0 (2003-08-04) [i386-mswin32]  
-27

Regards,

Park H.

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [March 26, 2009, 9:07pm UTC](https://www.ruby-forum.com/t/operator-precedence-of-exponentiation-and-complement/163923/3 "2009-03-26T21:07:33Z")

</div>

Heesob P. wrote:

> ruby 1.8.0 (2003-08-04) [i386-mswin32]  
> -27

Thanks. Here’s another one that surprised me. Running:

y = 5 % x = 3  
print “x=”, x, " y=", y, “\n”

produces:

x=3 y=2

That is, it seems that:

y = 5 % x = 3

is being parsed as:

y = 5 % (x = 3)

despite the % operator having a higher precedence than the = operator.  
I expected it to be parsed as:

y = (5 % x) = 3

producing a syntax error.

If this is a deliberate feature, I’d be interested in learning more  
about it.  
If anyone knows a good reference on the Ruby parser, please let me know.

Thanks,  
/-\

```
  Enjoy a safer web experience. Upgrade to the new Internet Explorer 

```

8 optimised for Yahoo!7. Get it now.
