Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

why is the size of int is range from -32768 to 32767

Greenhorn
Posts: 2
why is  the size of int is range from -32768 to 32767

Sheriff
Posts: 4745
131
It's not.  An int type's range it from 2^31-1 to -2^31, where "^" means exponentiation.

Greenhorn
Posts: 2
Actually it is short i.e, 2^15-1 to -2^15(-32768 to 32767)

short size is 2 bytes that equals to 16 bits

first bit(msb) is for sign(+,-) since integer is of signed data type  and hence 2^15 - 1 for +ve value(2^15,2^14.....2^0) and why -2^15 for -ve value

Sheriff
Posts: 23451
46
A short value mimics a 16-bit hardware register and an int value mimics a 32-bit hardware register.

Ranch Hand
Posts: 461
22
• 1
suppose if a variable is of n bits.then most significant bit is the sign bit leaving (n-1) bits for representing number.so total combinations possible are 2^(n-1)(for negative value representation bound is -2^(n-1)),now after negative value their comes 0 and positive numbers so excluding one combination for 0 leaves 2^(n-1)-1 combination and thus entails positive bound to be 2^(n-1)-1.

in your case for 16 bit
negative bound( -2 ^ ( 16-1 ) )--->zero--->( 2 ^ (16-1) -1)positive bound

hope it helps!
kind regards,
praveen

Marshal
Posts: 58382
178

syed sadath wrote:. . . first bit(msb) is for sign(+,-) . . .

Lost of people are told that the leftmost bit in an integer is a sign bit, but that is incorrect. The easiest way to regard it is as equivalent to −(2³¹) but that is not really accurate.
In fact it is a complementary number where exactly half the range is negative and half the range non‑negative. For negative numbers you get the equivalent of subtracting |n| from 2³². Even though  2³² doesn't actually exist as an int.

You get sign bits in IEEE754 floating‑point numbers; the n − 1 bits on the right are exactly the same for x and −x.

Campbell Ritchie
Marshal
Posts: 58382
178
If the leftmost bit in an integer were a sign bit, rather than having a value, then
0b1111_1111 == −0b0111_1111
(in 8 bits). There is a specification for such a representation, but I have never seen it used in real life.

Bartender
Posts: 10575
66
• 2

syed sadath wrote:Actually it is short i.e, 2^15-1 to -2^15(-32768 to 32767)...

OK, well you should say that. Accuracy is very important in programming, and a short is NOT an int ... nor indeed a Short, which is a different animal altogether.

The answer is actually very straightforward:
1. The number of possible values that can be held by a binary field of n bits is 2ⁿ, which is an even number.

2. If the value has to be signed - as both short and int are in Java - then those values should probably be divided by 2, giving 2ⁿ⁻¹ values of each "sign".

3. Except for one thing: What about 0?
You could have two of them: +0 and −0, and then you'd have a span of -2ⁿ⁻¹−1...+2ⁿ⁻¹−1 - indeed an older form of binary representation called 1's complement did precisely that.

The problem then is that arithmetic logic becomes more difficult, especially at the circuit board level (explained in the link), so most computers these days use 2's complement, which has only one '0' value. And since we don't normally think of 0 as being "negative", it makes sense to make it non-negative.
It also allows 0 to be represented by "all 0's", which has other useful properties - not the least of which being visual.

However, that means removing 1 value from the "positive" side to represent 0 itself, and hence the imbalance.

HIH

Winston

 Consider Paul's rocket mass heater.