aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why is this output? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why is this output?" Watch "Why is this output?" New topic
Author

Why is this output?

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9622
    
    2

Hi all,

Please explain me why and HOW is it printing the o/p like it does below?

int result = 2 ^ 3 //prints 1
int result = 3 ^ 3 //prints 0
int result = 3 ^ 77 //prints 78 ???


Thanks,
Jothi Shankar Kumar. S


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
^ is bitwise exclusive or

2 ^ 3 =

00000000000000000000000000000010 ^
00000000000000000000000000000011

=

00000000000000000000000000000001

=

1
Tim LeMaster
Ranch Hand

Joined: Aug 31, 2006
Posts: 226
Thats the bitwise exclusive OR (XOR) operator (One and only one bit can be on) so for

2 ^ 3


3 ^ 3


3 ^ 77

[ October 11, 2006: Message edited by: Tim LeMaster ]
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9622
    
    2

Hi Guys,

Thanks for the reply. But how did you know that 2 is represented as 000000000000......10 in binary form? I just wanna know that.

Regards,
Jothi Shankar Kumar. S
Tim LeMaster
Ranch Hand

Joined: Aug 31, 2006
Posts: 226
http://en.wikipedia.org/wiki/Binary_numeral_system

And Int is 32 bit in Java.
[ October 11, 2006: Message edited by: Tim LeMaster ]
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9622
    
    2

Hi Tim,

Will the SCJP exam require us to know all this? I'm planning for SCJP 1.4. Will this do any good to me to know in depth about this?

Regards,
Jothi Shankar Kumar. S
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19061
    
  40

Originally posted by Jothi Shankar Kumar Sankararaj:
Hi Guys,

Thanks for the reply. But how did you know that 2 is represented as 000000000000......10 in binary form? I just wanna know that.

Regards,
Jothi Shankar Kumar. S


Binary form is represented (with no surprise) with base 2 mathematics. See this for more information.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Tim LeMaster
Ranch Hand

Joined: Aug 31, 2006
Posts: 226
I believe 1.4 has bitwise and shift operators on the exam. The 1.5 exam I don't think does as it focus more on the new features in 1.5 and more Java API questions.

Either way I believe you should be able to interpret binary and effect of the bitwise operators on it.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9622
    
    2

Hi all,

Thanks for those who helped me with this. Is there any hard and fast rule with which we can convert decimal numbers to binary?? Anyone on this?

Regards,
Jothi Shankar Kumar. S
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi cowboys,

Jothi Shankar Kumar Sankararaj wrote:

Is there any hard and fast rule with which we can convert decimal numbers to binary??



What do you mean by decimal numbers?
If you mean integers to the base 10, then try e.g.


If you mean floating point numbers (doubles and floats) than the answer is no.
They are treated with different precission, depending your methods or classes have the strictfp modifier or not.

Yours,
bu.


all events occur in real time
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9622
    
    2

Hi all,

Tha above explanation is fine but I wont have the chance to compile when I write my exam??javascript: x()
Big Grin

The above explanation is fine but what if I want to try it out in a paper when I find similiar question on the exam? I remember learing binary to decimal conversion in my school days. So anyone please let me know the logic behind the conversion.

Regards,
Jothi Shankar Kumar. S
Aniket Patil
Ranch Hand

Joined: May 02, 2006
Posts: 218
http://www.math.grinnell.edu/~rebelsky/Courses/CS152/97F/Readings/student-binary.html


SCJP 5.0 | SCWCD 1.4 <br /> <br />If you don't know where you are going, any road will take you there!
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9622
    
    2

Hi above,

Thanks for the reply. That link really helped me.

Regards,
Jothi Shankar Kumar. S
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why is this output?