Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
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:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

operator orders confusing

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found the operator oder is different between bit shift and bitwise:
what is the order for:
8 | 9 & 10 ^ 11
and what is:
int x= 8 >> 9 <<< 10 >> 11
and what is:
boolean y==true==false==true;
can anybody please explain to me?

Thanks.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I can explain you reg.
boolean y==true==false==true;
It should be written as
boolean y=true==false==true; there should not be == after y
the evaluation is from right to left.
so y = true==false==true;
>> = true==(false==true);// bcoz false!=true therefore val of //expression is false
>> = true==(false);
>> = true==false; // here too val if exp is false;
>> = false;
therefore y = false
HTH
sasi

Originally posted by Michael Lin:
I found the operator oder is different between bit shift and bitwise:
what is the order for:
8 | 9 & 10 ^ 11
and what is:
int x= 8 >> 9 <<< 10 >> 11
and what is:
boolean y==true==false==true;
can anybody please explain to me?

Thanks.


 
sasi dhulipala
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
once again reg int x= 8 >> 9 <<< 10 >> 11;<br /> the left shift operator is << not <<< <br /> so the expression should be <br /> int x = 8 >> 9 << 10 >> 11;<br /> evaluating from Right to left<br /> x = 8 >> 9 << 10 >> 11;<br /> = 8 >> 9 << (10 >> 11);
= 8 >> 9 << ( 0); //bcoz 10 >> 11 = 0;
= 8 >>( 9 << 0);<br /> = 8 >> 9; //bcoz 9 << 0 = 9;
= 0;
there fore 0 is printed;
HTH
sasi
[This message has been edited by sasi dhulipala (edited January 04, 2001).]
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in both the cases the evaluation should be from left to right.
though, in these cases the direction of evaluation doesnt effect the fine result.
-Aj
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int x= 8 | 9 & 10 ^ 11; yeilds 11. Because the operator precedence in decending order is: &,^,| and =; To remember the precedence order of &, ^, |, just bear in mind the order is the same as from the most strict to the least strict.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BUT the order of assocoativity for shift operators is left 23 right so doesn't it mean that it has to be evaluated from left to right rather then right 2 left which has been done above
PLz clarify i am confused
------------------
"Winners don't do different things
They do things differently"
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think >> >>> and << operator goes form left to right ..
anyway here is the test for that
int a = Integer.MAX_VALUE;
System.out.println(a<<1>>1); //-1
System.out.println(a<<(1>>1)); //21......
System.out.println((a<<1)>>1); //-1
hope this will clear the doubts....
 
Tom Tang
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The precedence of shift operator is in decreasing order as "<<,>>,>>>".
 
anil bisht
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Tom
there is no precendence for shift operators...
System.out.println(a>>32<<2);//-4<br /> System.out.println((a>>32)<<2);//-4<br /> System.out.println(a>>(32<<1));//-1
so i think it works left to right
 
Ajay Patel
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lets lay the argument to rest.
- all the shift operators have the same precedence, and
- assocoativity is left to right for shift operators.
i think Anil's examples are great and should clarify everyone's doubts.
-Aj
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic