This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How does this work 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 "How does this work" Watch "How does this work" New topic
Author

How does this work

sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257

The Ouptput is 5. Can someone explain
Thanks


Sunita<br />SCJP 1.4
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
byte b1 = -5;
int i = 0xff;
byte b2 = (byte) (b1 ^ 1);
f 1011
f 1111
_______^
0 0100
b2++;
4 + 1
^ is or-exclusive. Only yields true if its two entries are distinct. That is 1^1 or 0^0 yield 0, but 1^0 or 0^1 yields 1
Read Cat and Mouse Games with bits for more.


SCJP2. Please Indent your code using UBB Code
Todd Killingsworth
Greenhorn

Joined: Jan 30, 2002
Posts: 28
Jose, where did this come from? Operand was 1, not -1.
f 1011
f 1111 <---- ???
_______^
0 0100
I'm getting an answer of -5.

Taking this from the top:

f 1011 -5
f 0001 1
_______^
0 1010 -6
f 0001 1
_______+
0 1011 -5

Please let me know if I'm missing something - my 2's compliment isn't very sturdy yet
Todd Killingsworth
sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
I also get -5 and I am confused
John Paverd
Ranch Hand

Joined: Nov 17, 2002
Posts: 115
Originally posted by sun par:
I also get -5 and I am confused

Sunita
If you replace byte b2 = (byte) (b1 ^ 1);
with byte b2 = (byte) (b1 ^ i);
the result will be 5. Perhaps there was a typo in the test question?
[ January 10, 2003: Message edited by: John Paverd ]

SCJP 1.4
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
I agree with John, not only do you get 5, but you actually use i - which otherwise you don't use.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How does this work