• 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

Pls help me out!

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all..
could u pls help me out with this one??
public class certify{

public static void main(String args[])
{
int i=10;int j=8;
i=i++;
System.out.println(i);
System.out.println(i^j);
System.out.println(00001010^00001000);
}
}

the output is..
10 (why not 11?)
2 (this is fine)
8 (why not 2 again??)

------------------
Hima
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The reason you're getting 10 from i = i++; because = operator has the lowest precedence and it's RHS while postfix operators are LHS.
As for your second question I also find it strange, but it seems that windows environment (if you work on win) interprates the binary differently. I'm not sure that reason for that but the following code shows that what we'd calculate as 8 from binary, in fact it prints 512.
public class certify{
public static void main(String args[])
{
int i=10;int j=8;
System.out.println(00001010);// prints 520
System.out.println(00001000);// prints 512
System.out.println(00001010^00001000); // prints 8
System.out.println(00000010);// prints 8
}
}
print out:
520
512
8
8
 
Hima Mangal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi..
thanx for the reply.. but i still don't understand.. could u pls elaborate on the first point?? ..
thanx again..
------------------
Hima
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hima,
As to your first doubt
int i = 10;
i = i++;
It is better if you assume it as
i++;
i=10;
What should happen is that i should return its value
and then increment it.
But what seems to happen always is that i value is incremented
in the first cycle and then in the second cycle, i is given the value it had before it incremented thus wasting the increment.
As for your second doubt,
I think you are assuming the
"System.out.println(00001010^00001000);" to be
binary numbers where in fact they are octal.
Hope that answers your question
Navin
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Regarding 3)
The operands are being accepted as Octal and with the help of conversion system we know
00001010 in Octal = 512 in decimal System
00001000 in Octal = 520 in decimal System

Now the 3) statment becomes
System.out.println(512^520);
giving you 8.
(I hope that u wont ask for: how 512^520=8???)

 
a hui
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess I'd like to know why these numbers are octal and not binary, and also how can a number be represented in binary in a program?

Thanks
AH
 
Hima Mangal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi everybody..
thanx for your detailed and precise explanations,.. and a special thanx for naveen.. ur answer cleared my doubts perfectly..
------------------
Hima
 
Navin Narayan
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi hui,

Originally posted by a hui:
I guess I'd like to know why these numbers are octal and not binary, and also how can a number be represented in binary in a program?

Thanks
AH



The numbers are in Octal since they start with 0 .
If they started with 0x , they would be in hex.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic