File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 2's compliment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "2 Watch "2 New topic
Author

2's compliment

shri mon
Ranch Hand

Joined: Jan 11, 2005
Posts: 63
for byte

-2 --> 1111 1110


(-2 >>> 1) gives -1

Can anyone explain?
Kristof Janssens
Greenhorn

Joined: Jan 05, 2005
Posts: 23
I can't explain that because the answer should be(have you tested that code?):

-2 >>> 1 = 2147483647

-2: 1111 1111 1111 1111 1111 1111 1111 1110

>>>1 (move 1 to the right): 0111 1111 1111 1111 1111 1111 1111 1111

this is the max_value of an int, you lose the -

-2 >> 1 = -1
>>1: (move 1 to the right and keep the sign-bit): 1111 1111 1111 1111 1111 1111 1111 1111


SCJP 1.4 (90%)<br />SCJD (in progress)
Balazs Borbely
Ranch Hand

Joined: Oct 11, 2004
Posts: 33
For byte, shri mon is right.

Try this one:



The reason is the promotion to int before of the expression 'b1>>>1' is evaluated.

so, b1 promoted to int
b1: 1111 1111 1111 1111 1111 1111 1111 1110
'b1>>>1': 0111 1111 1111 1111 1111 1111 1111 1111

and after 'b1>>>1' narrowed to byte 0111 1111 1111 1111 1111 1111 1111 1111
-> 1111 1111 what is -1


'Make everything as simple as possible, but not simpler.' --Albert Einstein
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

And three's a crowd?

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Sorry for hijacking here, but here's a good joke.

A guy walks into a bar and sits at the bar. He orders a drink, as the bartender is on the otherside of the bar, the guy hears. "Nice Tie".

He looks around and sees no one around. A couple of seconds later, he hears "You have great looking eyes". Again he looks around and sees nothing.

So the bartender returns with they guys order, and the guy asks, "I am sitting here and I keep hearing 'nice tie', 'nice eyes', but there's no one around, am I just hearing voices or something?"

So the bartender looks down and points on the bar and says, "Complimentary nuts"

Mark
[ March 11, 2005: Message edited by: Mark Spritzler ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Groan......


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Louie van Bommel
Ranch Hand

Joined: Aug 17, 2004
Posts: 76
bla ha ha! :roll:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: 2's compliment