• 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

Can someone please explain this problem?

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone please explain me how can we get output 122.
This example is from Marcus Green exam.

According to me in both if blocks the second condition won't get executed because the first condition is false. So at line 1, i=1, at line 2 also, i=1 and at line 3 x is 0, how can the output be 122??
(code taggs added)
[ May 09, 2004: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two things:
1. First "if" uses the conditional/logical "&&", and it is short cut. So, the right hand side of the && wont get evaluated. That is why the value of i remains 1.
2. The second if statement has one "&", which is a bitwise "AND" operator, hence evaluates both operands there by assigning a new value for the variable i (i = i * 2 = 2).
As for x is concerned, it is simply a bitwise AND (0010 AND 0010).
Regards,
Anil
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anil said:


2. The second if statement has one "&", which is a bitwise "AND" operator, hence evaluates both operands there by assigning a new value for the variable i (i = i * 2 = 2).
As for x is concerned, it is simply a bitwise AND (0010 AND 0010).



Unfortuately this is not the correct explanation Anil.
"&" is indeed a bitwise operation, but it is also a logical operation. Unlike "&&" both operands of "&" have to be evaluated to get its result.
The if condition:

is boolean & boolean, and in this case is false & false.
In the second usage (the evaluation of x's value) it is being used as a bitwise operator, as you correctly said.
 
Anil Hulikal
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Barry. Sorry, I did not mean to mislead. I was simply trying to explain in the context of how the value of variable "i" was getting assigned with a new value. I agree that I did not fully explain about "&".
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No problem, Anil. (I've made more mistakes than most on these forum I bet )
 
Amit Parnerkar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Anil and Barry, I think I was confused with single "&". This explanation really helps
Thank you
Amit
reply
    Bookmark Topic Watch Topic
  • New Topic