Thread: numerical range of int and short

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    numerical range of int and short

    Hi

    The range for short is: -32,768 to 32,767
    The range for int is: -2,147,483,648 to 2,147,483,647

    (int is system dependent which means that on 16-bit system it would be occupy 2 bytes which would result in small numerical range)

    The simple way to know the range for short is (the same goes for int): There are 2 bytes occupied by a short. There are 16 bits in 2 bytes. 2^16 = 65536. Now divide the "65536" by 2 to get -ve and +ve ranges. 65536/2 = 32768. Now we have to include "0" too in the range. The -ve range is: -32,768, and +ve range is: 32,767. The question which can come to mind is that why the +ve range is not "32,768" instead of "32,767". Let's check a simple case to understand this.

    When 30 is divided by 2, the result is 15. This result doesn't include "0" and "0" is only used as a reference. But if we were to include "0" also then the +ve and -ve range for 30 would be: -15 to +14.

    I hope you understand my question. Please guide me on this. Thank you.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jackson6612
    I hope you understand my question.
    What is your question? I see a question (why the +ve range is not "32,768" instead of "32,767"), but it looks like you already have the answer to that.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    I'm not sure you asked a question. I don't think you did. You implied the possibility that a question may exist. Is that your question?

    Seriously, how has your favorite internet search engine not already answered this question of yours?

    My favorite, Google, returned a few hundred million results after tossing a few random words from your post at it. I'm thinking at least a few probably answers the questions.

    Soma

  4. #4
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by jackson6612 View Post
    Hi

    The range for short is: -32,768 to 32,767
    The range for int is: -2,147,483,648 to 2,147,483,647

    (int is system dependent which means that on 16-bit system it would be occupy 2 bytes which would result in small numerical range)

    The simple way to know the range for short is (the same goes for int): There are 2 bytes occupied by a short. There are 16 bits in 2 bytes. 2^16 = 65536. Now divide the "65536" by 2 to get -ve and +ve ranges. 65536/2 = 32768. Now we have to include "0" too in the range. The -ve range is: -32,768, and +ve range is: 32,767. The question which can come to mind is that why the +ve range is not "32,768" instead of "32,767". Let's check a simple case to understand this.

    When 30 is divided by 2, the result is 15. This result doesn't include "0" and "0" is only used as a reference. But if we were to include "0" also then the +ve and -ve range for 30 would be: -15 to +14.

    I hope you understand my question. Please guide me on this. Thank you.
    Think about an 8 bit number. There are 256 possible combinations. If it's unsigned that equates to a value ranging from [0, 255]. But if it's signed then the "unsigned part" only has 7 bits to work with, so 128 possible combinations ranging from [0, 127]. The sign bit, if zero, is really just an extension of the plain old unsigned zero, otherwise it must necessarily represent some nonzero value, meaning 128 possible negative combinations ranging from [-128, -1]. The same principle applies to wider integers.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Also, all types (except char) have implementation defined sizes, including short. And int is not guaranteed to be 2 bytes on a 16-bit machine.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User Inanna's Avatar
    Join Date
    May 2011
    Posts
    69
    all types (except char) have implementation defined sizes
    Even char has an implementation defined size. sizeof(char) is always 1, but the number of bits in char does not have to be 8. 8 bits is the norm though, so this bit of trivia is really only good for looking smart. Like I hope I am doing now.

  7. #7
    spaghetticode
    Guest
    I think his (or her) question is rather, why is it -32,768 to +32,767 instead of -32,767 to +32,768. That's how I understood it.

    EDIT: And my suggestion for an answer would be, that 0 is regarded a positive number.

  8. #8
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Well, it's like this: the last ( most significant ) bit is considered the sign bit of a number. With that in mind, and with quick and easier addition/subtraction, most nowadays computers use the 2's complement notation.

    Now, the reason of a signed 16-bit integer ranging from -32,768 to 32,767 is because of the 2's complement we're using. If you keep incrementing a signed 16-bit integer it will go from 0 to 32,767, then drop to -32,768 and all the way down to -1. 32767 is 0x7FFF, -32768 is 0x8000 etc. -1 is 0xFFFF
    Devoted my life to programming...

  9. #9
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by dennis.cpp View Post
    EDIT: And my suggestion for an answer would be, that 0 is regarded a positive number.
    Positive and negative are defined in terms of their relative distance from zero, so zero itself is neither. Similarly, one is not prime simply because it necessarily defines primality...

  10. #10
    spaghetticode
    Guest
    @gardhr - is that a mathematical definition? (Just for interest.)

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by gardhr View Post
    Positive and negative are defined in terms of their relative distance from zero, so zero itself is neither.
    Positive and negative are not defined in terms of distance from zero. Mathematicians typically define positive and negative in terms of direction from zero, where positive and negative are opposite directions from each other. Let's say we have a straight line going from our left to our right, and some point on that line is designated as zero. If points to the right of zero are deemed to represent positive values, points to the left represent negative values.

    The designation of which point is marked as zero, and which direction is positive, depends on frame of reference. For example, if we have a ruler with a set of values marked, we might see negative values to the left of zero and positive values to the right of zero. But someone else, looking at the same ruler from the other side, might see negative values to his right of zero and positive values to his left.

    So, from this perspective, zero is neither positive nor negative.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  12. #12
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by grumpy View Post
    Positive and negative are not defined in terms of distance from zero. Mathematicians typically define positive and negative in terms of direction from zero, where positive and negative are opposite directions from each other. Let's say we have a straight line going from our left to our right, and some point on that line is designated as zero. If points to the right of zero are deemed to represent positive values, points to the left represent negative values.

    The designation of which point is marked as zero, and which direction is positive, depends on frame of reference. For example, if we have a ruler with a set of values marked, we might see negative values to the left of zero and positive values to the right of zero. But someone else, looking at the same ruler from the other side, might see negative values to his right of zero and positive values to his left.

    So, from this perspective, zero is neither positive nor negative.
    Yes, direction...not distance! But your argument that the distinction between the two is merely dependent on the frame of reference isn't exactly accurate. Multiplying two negative numbers produces one of a different sign, taking the square root of one produces an imaginary number, etc, so there are some important differences.

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by gardhr View Post
    But your argument that the distinction between the two is merely dependent on the frame of reference isn't exactly accurate.
    True. I left out plenty of conditions and caveats. In a discussion of the meaning of positive versus negative for integral values, delving into complex number theory is a little excessive.
    Quote Originally Posted by gardhr View Post
    Multiplying two negative numbers produces one of a different sign, taking the square root of one produces an imaginary number, etc, so there are some important differences.
    You're delving more than necessary into complex number theory than I consider is necessary for this discussion.

    But, to follow your diversion slightly .... the differences you are highlighting are trivial enough that they are not really differences in complex number theory. The frame of reference remains important though (in the sense of which directions along the real and imaginary axes are deemed to be "positive").

    Multiplying two real values of the same sign always gives a positive value.

    In the complex number space, there are always two square roots of any value - for real values, that is true regardless of whether the value is positive or negative. For any complex value, the two square roots are related by a rotation though 180 degrees about the origin. The relationship between square roots of a positive real value and of a negative real value is a rotation of 90 degrees about the origin. The sign of the rotational angle (which direction of rotation is positive) still depends on which directions on the real and imaginary axes are stipulated to represent positive.
    Last edited by grumpy; 06-03-2011 at 03:09 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A program that can only accept a numerical value?
    By Bano360 in forum C Programming
    Replies: 4
    Last Post: 11-15-2010, 07:55 PM
  2. Ascending Numerical Order
    By Yizi in forum C Programming
    Replies: 7
    Last Post: 12-08-2009, 07:05 PM
  3. Replies: 2
    Last Post: 04-21-2008, 07:43 PM
  4. Numerical Integration
    By Crazed in forum C Programming
    Replies: 13
    Last Post: 03-03-2008, 03:01 PM
  5. Replies: 7
    Last Post: 02-08-2008, 06:31 PM