This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.

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:\>javatest 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.

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.

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.