File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

2's compliment

 
shri mon
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for byte

-2 --> 1111 1110


(-2 >>> 1) gives -1

Can anyone explain?
 
Kristof Janssens
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Balazs Borbely
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mark Spritzler
ranger
Sheriff
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And three's a crowd?

Mark
 
Mark Spritzler
ranger
Sheriff
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Groan......
 
Louie van Bommel
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bla ha ha! :roll:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic