Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Decrement operator

 
Rebecca Abraham
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I've a doubt concerning the decrement operator. Could someone please explain the following to me:
byte y = -128;
byte x = y--;
System.out.println ("y = " + y);
Output:
y = 127

I cant figure out how the value of y became 127.
Thanks.
Rebecca
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
y-- is equivalent to y = y - 1
that is y = -128 - 1 = -129
-128 = 1000 0000
-1 = 1111 1111
1000 0000
+1111 1111
-----------
1 0111 1111
The leftmost bit is ignored, thus resulting 127
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic