• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

BItwise & logical operators question

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which statements @ the O/P of the foll. are true?
public class Logic
{
public static void main(String args[])
{
int i = 0;
int j = 0;

boolean t = true;
boolean r;
r = (t & 0<(i+=1));
r = (t && 0<(i+=2));
r = (t | 0<(j+=1));
r = (t | | 0<(j+=2));

System.out.println(i+""+j);
}//main
}//class
1. 1st digit printed is 1
2. 1st digit printed is 2
3. 1st digit printed is 3
4. 2nd digit printed is 1
5. 2nd digit printed is 2
6. 2nd digit printed is 3

 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chamya:
Please let us know what you think should be the ans.
Regds.
- satya
 
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi chamya,
the statement 3 and 4 are true.
remember && and | | are short circuit operators.so they may not evaluate the operand on RHS if the result can be deduced by calculating LHS opearand only.
regards
deekasha
 
Chamya Pehalvan
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not know the answere to the given code & hence my question to whoever can help me solve it!

Originally posted by Chamya Pehalvan:
Which statements @ the O/P of the foll. are true?
public class Logic
{
public static void main(String args[])
{
int i = 0;
int j = 0;

boolean t = true;
boolean r;
r = (t & 0<(i+=1));
r = (t && 0<(i+=2));
r = (t | 0<(j+=1));
r = (t | | 0<(j+=2));

System.out.println(i+""+j);
}//main
}//class
1. 1st digit printed is 1
2. 1st digit printed is 2
3. 1st digit printed is 3
4. 2nd digit printed is 1
5. 2nd digit printed is 2
6. 2nd digit printed is 3


 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chamya Pehalvan:
I do not know the answere to the given code & hence my question to whoever can help me solve it!


Pen and paper

the answers are 3 and 4
reply
    Bookmark Topic Watch Topic
  • New Topic