• 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.MIN_VALUE

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone please explan why I am getting this output. I thouht Byte.MIN_VALUE should be - 2 to the power of 7 and hence its binary representation would be

2(power) 7 is represented as 10000000
complement 01111111
-2(power) 7 is 10000000

But this is not the represention in the output in line 3, and line 4 for hex

C:\>java test
Byte.MAX_VALUE in Binary:1111111
Byte.MAX_VALUE in Hex:7f
Byte.MIN_VALUE in Binary:11111111111111111111111110000000 //o/p line 3
Byte.MIN_VALUE in Hex:ffffff80 // o/p line 4

I have used Integer.toHexString function passing Byte.MAX_VALUE and Byte.MIN_VALUE, also used Integer.toBinaryString.

 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Integer methods toBinaryString and toHexString both take int as an argument. So when you pass a byte, it's automatically converted to a 32-bit int.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The toHexString() and toBinaryString() methods takes an int -- not a byte. The MIN_VALUE value has to be casted to an int first prior to calling the method. What you are seeing is the bit / hex pattern of the value after the cast has occurred.

BTW, this is true with MAX_VALUE too, but you don't see it because the cast conversion is simply a padding of lots of zeros.

Henry
 
Ramya Iyer
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marc and Henry,

I understand that the value is getting converted into an int. In the first case for MAX_VALUE, we have zero's (sign bit) padded to the left and in the second case MIN_VALUE, we have sign bit padded to the left, which in this case is 1.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic