• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
syed sadath
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why is  the size of int is range from -32768 to 32767

 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not.  An int type's range it from 2^31-1 to -2^31, where "^" means exponentiation.
 
syed sadath
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A short value mimics a 16-bit hardware register and an int value mimics a 32-bit hardware register.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 56520
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!