• 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

byte assignment

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What should be the output of the following
byte b = -128;
System.out.println(--b);
I expected it to have -128 as that is the MIN Value that can be stored in a byte, but I got 127 any explanation???
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is something with regard to arithmetic operations.
Hex. representation of -128 is 0xFF80(this is the 2's complement of 128)
Hex. representation of -1 is 0xFFFF(2's complement of 1)
When you say '--b' what happens is b=b-1 i.e.b=(-128)+(-1)
or
b=(0xFF80) + (0xFFFF)
thus b=0x007F
thus b=127 as 7F is Hexadecimal representation of 127
-Nysal
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic