• 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

Decrement operator

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic