Aruna Balasuriya

Ranch Hand

Posts: 44

Aruna Balasuriya

Ranch Hand

Posts: 44

posted 8 years ago

Java, along with practically every modern day processor, uses the twos complement format to store bytes. And yes, twos complement's range does that.

http://en.wikipedia.org/wiki/Two's_complement

Henry

Aruna Balasuriya wrote:I want to know how this -128 value get from.(How there is a difference between positive end and negative end)

Java, along with practically every modern day processor, uses the twos complement format to store bytes. And yes, twos complement's range does that.

http://en.wikipedia.org/wiki/Two's_complement

Henry

posted 8 years ago

numbers are represented by bit patterns. a bit can be '0' or '1'. Each bit added to the storage space doubles the total possible values.

So lets assume we only had one bit to store values. you could store two total. zero is usually one, and so that leaves one other value of -1.

Now assume we have 2 bits, which would allow us to store four values. Again, you need 0 to be one of them. so you could do (0,1,2,3), or (-1,0,1,2) or (-2,-1,0,1). This extrapolates to (-128, -127, -126...-2, -1, 0, 1,2,...126, 127) when you get up to 8 bits.

another way to think about it is that half the values that can be stored are negative, and half are NON-negative. one of those non-negative values is 0, meaning you have one-less spot to represent a positive number.

So lets assume we only had one bit to store values. you could store two total. zero is usually one, and so that leaves one other value of -1.

Now assume we have 2 bits, which would allow us to store four values. Again, you need 0 to be one of them. so you could do (0,1,2,3), or (-1,0,1,2) or (-2,-1,0,1). This extrapolates to (-128, -127, -126...-2, -1, 0, 1,2,...126, 127) when you get up to 8 bits.

another way to think about it is that half the values that can be stored are negative, and half are NON-negative. one of those non-negative values is 0, meaning you have one-less spot to represent a positive number.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors