Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unary operator evaluation

 
Sherry Jacob
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi every1
Can any1 tell me how are these statements evaluated ?




They are unary operators. So how is the calculation being done ?
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It may help to understand that those are not unary operators; they're binary (left and right operands). They are evaluated left-to-right in order of precedence (accounting for parentheses, of course).
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. The unary operators are +, -, ++, --, ~, !, and cast operators.

Examples:

 
Sherry Jacob
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry I mentioned unary by mistake.

I meant Bitwise logical operators.

Thanks for pointing that out.

So how is it evaluated anyway ?
 
santhoshkumar samala
Ranch Hand
Posts: 156
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is evaluated is as follows:

The numbers converted into the binary format and the opertaors on the binary format.

int a = 1 | 2 ^ 3 & 5;
The operation will be like this 001|010^011&101

The precedence is like this &,^,|

so the steps to the result are

1)001|010^001
2)001|011
3)011

The result is 3
 
Ryan Kade
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The results are:

a=3
b=0
c=3

The order of operations for a has already been explained by santoshkumar, and as he said, it's equivalent to the forced order that you imposed for c.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic