[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  Programming
. -> [Folder]  General
. . -> [Subject]  Newbie problems learning C++

Newbie problems learning C++

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Ready   (15 posts)  [Biography] bio
Date Wed 09 Jun 2004 04:17 PM (UTC)
Message
Hi i've only just started learning C++ and i've already come up against a problem. While learning about identifiers i've noticed on the site im learning from that it mentions ranges. being signed and unsigned.
I am 100% new and I'm not quite sure what it means by this or even what it means by range could someone enlighten me please?
[Go to top] top

Posted by Meerclar   USA  (733 posts)  [Biography] bio
Date Reply #1 on Wed 09 Jun 2004 05:58 PM (UTC)

Amended on Wed 09 Jun 2004 05:59 PM (UTC) by Meerclar

Message
In a nutshell, signed and unsigned ONLY apply to numeric identifiers (ie, int or long). Signed identifiers can be negative and used for normal math functions. Unsigned identifiers are only positive and have somewhat obvious implication for math functions (try subtracting with unsigned ints sometime :P). Unsigned is typically used when you need more positive number range than signed would give you but you dont want as much range as the next larger identifier type would give you - or if you already use the largest identifier and its still not quite big enough.


--example because this is confusing to read--
short -32k to 32k
unsigned short 0 to 64k

Meerclar - Lord of Cats
Coder, Builder, and Tormenter of Mortals
Stormbringer: Rebirth
storm-bringer.org:4500
www.storm-bringer.org
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #2 on Wed 09 Jun 2004 07:49 PM (UTC)

Amended on Wed 09 Jun 2004 09:48 PM (UTC) by Nick Gammon

Message
Another very important use for an unsigned integer is to signify that whatever you are dealing with is only ever positive. This is important for instance for sizes and lengths - size and length (of structures, strings, whatever) can never be negative so making it unsigned is how you enforce that. Of course, somebody can still screw up and subtract something pushing your number into the negative i.e. some incredibly huge number. For instance, with an unsigned short if you did 0 - 1 you will obtain something around 64k. (If you care about the exact value, it is 2^16 - 1 = 65535)

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #3 on Wed 09 Jun 2004 09:52 PM (UTC)
Message
Quote:

... try subtracting with unsigned ints sometime ...


You can subtract, you just can't get a negative result. This will be OK:


unsigned int i = 20;
unsigned int j;

j = i - 10;


However subtract more, and you get something like this:


#include <stdio.h>
int main (void)
  {

  unsigned int i = 20;
  unsigned int j;

  j = i - 30;

  printf ("j = %u.\n", j);

  return 0;
  }


Compiling this and testing it gives:


j = 4294967286.


Probably not what you are expecting. :)

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Cash   USA  (626 posts)  [Biography] bio
Date Reply #4 on Thu 10 Jun 2004 12:57 AM (UTC)
Message
Although it doesnt really matter except it might save a little typing, generally if you use unsigned then the int is implied unless you specify otherwise. Thus:

unsigned int x;

and

unsigned x;

are exactly the same. Also, for thoughts on how this might be applicable to muds is vnums. I still wonder why vnums were put as int (and sh_int in many cases) when none of the rooms were supposed to be negative. In the ZMP codebase (http://www.sourceforge.net/projects/zmp) I used an unsigned simply because I don't want my room numbers going negative, ever. Also, the reason I didnt use something bigger like a long is because the world will be small since it is geared totally around the Zoids and their teams.

Just some thoughts for you. :)

~Nick Cash
http://www.nick-cash.com
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #5 on Thu 10 Jun 2004 01:19 AM (UTC)
Message
Easy: -1 is a very convenient way of representing an invalid vnum, whereas with an unsigned int you often have the problem of what to use as "invalid vnum". Do you use 0, or the maximum value? Either way you're removing a potentially valid vnum.

Of course my solution to this problem was to make the vnum no longer a number but a C++ structure with methods and an internal field to test validity... :-)

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Ready   (15 posts)  [Biography] bio
Date Reply #6 on Thu 10 Jun 2004 11:10 AM (UTC)
Message
So let me get this straight.....


Signed intergers are used for mathmatical equations etc, but cos they go into the minus their largest positive number is quite small.
so....
Unsigned intergers are used when its not a mthmaticall equation etc. as then you can have a higher positive number like with vnums.

Am I getting this?

Then what Ksilyan is saying is that with Muds you need high Vnums and so need unsigned intergers, but then when you set an invalid vnum you would be using up a perfectly good vnum and so he made the vnum a C++ structure to get around this.

Forgive me for going over it again but I'm new and I just want to make sure I get everything right.
[Go to top] top

Posted by Ready   (15 posts)  [Biography] bio
Date Reply #7 on Thu 10 Jun 2004 01:53 PM (UTC)
Message
Also just to make sure data types are used to store your variables, and they tell your computer what to store the variables as. Right?
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #8 on Thu 10 Jun 2004 02:54 PM (UTC)
Message
Quote:
Signed intergers are used for mathmatical equations etc, but cos they go into the minus their largest positive number is quite small.

Well, "small" is relative - with a signed 32 bit integer the range is roughly -2^31 to 2^31, i.e. more or less -2,147,483,648 to 2,147,483,648 (I am most likely one or two off, can't be bothered). So yes, small is relative; 2 *billion* in either direction really isn't that small for vnums!

I don't think you really need unsigned integers for vnums, unless of course you're planning on having more than 2 billion rooms (uh-huh :P). There seems to be this myth about how integers are "small" but mostly that's because people don't understand how it all works.

Quote:
Then what Ksilyan is saying is that with Muds you need high Vnums and so need unsigned intergers, but then when you set an invalid vnum you would be using up a perfectly good vnum and so he made the vnum a C++ structure to get around this.

No, that's not what I said... You don't need vnums *that* high. If you're using 16-bit shorts, then yes, you do; but if you're actually using integers, leaving the sign bit is fine and in fact very convenient for representing invalid vnums (-1).

The C++ structure I made was out of convenience, a somewhat "cleaner" version of the vnum. My vnums are two-piece, "unsigned short.unsigned short", with a third field (bool) to indicate "valid" or not. The idea is to make the notion of valid independent of the value, and to make programming with them easier and more convenient. Also, having a struct like this also helps a great deal in ensuring type safety (e.g. you can only assign vnums to vnums, you can only compare vnums, etc.), which C++ is much better at than C.

Quote:
Also just to make sure data types are used to store your variables, and they tell your computer what to store the variables as. Right?

I guess that's one way of putting it. I would prefer saying that they tell the program what is contained in the variable, e.g. a c-style struct has a grouping of fields, a class has variables and methods, etc.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #9 on Thu 10 Jun 2004 09:37 PM (UTC)
Message
Quote:

Signed intergers are used for mathmatical equations etc, but cos they go into the minus their largest positive number is quite small.


No, their largest positive number is not "quite small" it is simply 1/2 of what you can store in an unsigned number, because you have used one bit for the sign. You can see the differences if you look in the file limits.h:


  • INT_MIN

    Minimum value for a variable of type int. –2147483648

  • INT_MAX

    Maximum value for a variable of type int. 2147483647

  • UINT_MAX

    Maximum value for a variable of type unsigned int. 4294967295 (0xffffffff)



Notice how the maximum value for unsigned int is twice the value for a signed int? That is because one of the 32 bits used to store the number is used to show if it is negative or not. However I would not say that 2147483647 is "quite small".

Whether or not you use the numbers for maths really depends on the application. For instance, you probably don't carry -3 swords on you, however banks normally show a balance you owe to them as negative. eg. if you have -$200 on your bank statement it means you owe them $200.

As for "wasting a vnum" by using unsigned ints you are throwing away a possible 2147483647 vnums (all the negative ones) just so you can use -1 to represent an invalid vnum. Seems a bigger waste to me to have 2147483647 that you cannot use, rather than using (say) 0 as an invalid vnum. That is only "wasting" one of them.

However unless you are planning to have 2,147,483,647 (over 2 billion) rooms, then it is pretty academic.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


24,546 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]