• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

operator

 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class loap
{
public static void main(String[]args)
{
int x=4;
boolean b1,b2,b3,b4;
b1=b2=b3=b4=true;
x=(b1|b2 & b3^b4)?x++:--x;
System.out.println(x);
}
}
In the above programme i would like to know why the output is not showing any reduction in the value of varaible x.
As the result is false of the | and & operator.The result should have been 3 because of the pre decrement operator.

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nitin,
Change x=(b1|b2 & b3^b4)?x++:--x; to
x=((b1|b2) & (b3^b4))?x++:--x;
ur ans will be 3.
Arvind.

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class loap
{
public static void main(String[]args)
{
int x=4;
boolean b1,b2,b3,b4;
b1=b2=b3=b4=true;
x=(b1|b2 & b3^b4)?x++:--x;
System.out.println(x);
}
}
Hi Nitin,
The order of operator precedence is as follows:
& first, then ^, finally |
Thus b2 & b3 will yield true. A ^ (xor) of that result with b4 will yield false. Finally b1 | with false will yield true. And that is why you did not get the answer of 3.
Regards,
Lam
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case what was the output? Was it 4 or 5?
 
Lam Thai
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case what was the output? Was it 4 or 5?
just as you would assign
x = x++;
the answer is 4 (i.e. x = x and post-increment does not take effect)
Regards,
Lam
 
There are no more "hours", it's centi-days. They say it's better, but this tiny ad says it's stupid:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic