• 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

Why is this output?

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
^ is bitwise exclusive or

2 ^ 3 =

00000000000000000000000000000010 ^
00000000000000000000000000000011

=

00000000000000000000000000000001

=

1
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://en.wikipedia.org/wiki/Binary_numeral_system

And Int is 32 bit in Java.
[ October 11, 2006: Message edited by: Tim LeMaster ]
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Tim LeMaster
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://www.math.grinnell.edu/~rebelsky/Courses/CS152/97F/Readings/student-binary.html
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi above,

Thanks for the reply. That link really helped me.

Regards,
Jothi Shankar Kumar. S
 
Don't MAKE me come back there with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic