Originally posted by j rangavallika:-
In those 16 bits the short will use only 15 bits to represent the original magnitude of the number and the remaing bit to store the sign, 1 for negative and 0 for positive so it will accomdate 2^15 only.
I am afraid you are mistaken there.
Java does not use that method of integer arithmetic, which is called sign and magnitude (S&M). An S&M integer in 16 bits runs from minus32767 (-2^15 + 1) to minus0, AND from 0 to 32767, so it has two values of zero, and a total range one less than -32768 to +32767, as Ernest Friedmann-Hill correctly quotes.
Java uses the two's complement format, where the bits from 0 to 15 (reading right to left, remember), represent
1248163264128256512102420484096819216384and minus32768, respectively.This is a gross simplification of the two's complement format, but it will work.
The two's complement format has at least these four advantages over S&M:
It is monotonic: each binary pattern represents a number one different from its neighbours.It uses the whole of the available range, because there is no duplicationAn adding circuit can add complement numbers very easily, and subtract them by entering a single bit representing "minus"Nobody cracks bad jokes about its acronym I found a nice article about two's complement [url=http://en.wikipedia.org/wiki/Two%27s_complement]in Wikipedia, [/url]if it is any use to you.
CR
[ May 12, 2006: Message edited by: Campbell Ritchie ]