Win a copy of Rust Web Development this week in the Other Languages forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Tim Cooke
• Campbell Ritchie
• Ron McLeod
• Liutauras Vilda
• Jeanne Boyarsky
Sheriffs:
• Junilu Lacar
• Rob Spoor
• Paul Clapham
Saloon Keepers:
• Tim Holloway
• Tim Moores
• Jesse Silverman
• Stephan van Hulst
• Carey Brown
Bartenders:
• Al Hobbs
• Piet Souris
• Frits Walraven

# Unary operator evaluation

Ranch Hand
Posts: 128
• Number of slices to send:
Optional 'thank-you' note:
Hi every1
Can any1 tell me how are these statements evaluated ?

They are unary operators. So how is the calculation being done ?

Ranch Hand
Posts: 657
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
P.S. The unary operators are +, -, ++, --, ~, !, and cast operators.

Examples:

Sherry Jacob
Ranch Hand
Posts: 128
• Number of slices to send:
Optional 'thank-you' note:
I'm sorry I mentioned unary by mistake.

I meant Bitwise logical operators.

Thanks for pointing that out.

So how is it evaluated anyway ?

Ranch Hand
Posts: 156
• Number of slices to send:
Optional 'thank-you' note:
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

Ranch Hand
Posts: 69
• Number of slices to send:
Optional 'thank-you' note:
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.