Jump to content
  • Advertisement
Sign in to follow this  
johnnyBravo

(c++) Whats the difference between signed and unsigned variables?

This topic is 4972 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I was wondering what the difference between signed and unsigned variables is? Also say I said an Unsigned integer equalled an int or vice versa, would that be any problem at all, eg slow downs, loss of data etc eg. int num = (UINT)5; //or UINT unum = (int)5; Thanks

Share this post


Link to post
Share on other sites
Advertisement
this may be of some help.

There is no slow down when assigning an unsigned to a signed, or the other way around as in your examples below.
Performance will only be affected if you assign from a smaller data type to a larger one for example.

The only difference is the range of representable values.

Share this post


Link to post
Share on other sites
Basically.

However when using signed numbers they are stored in "two's compliment" form.

EDIT - Removed incorrect explaination of two's compliment. Its too late to think :-/

[Edited by - Ravyne on January 5, 2005 1:46:24 AM]

Share this post


Link to post
Share on other sites
Quote:

oh so basically an unsigned variable holds twice the amount of data


Aargh, this always annoys me [smile]. It holds exactly the same amount of data; just it holds twice the range of positive numbers, and no negative numbers.

An unsigned variable uses the first bit to indicate positive or negative status; therefore, in an unsigned variable the first bit can be used to to effectively double the positive range (not hold twice the amount of data).

You might also want to google for twos compliment, to see how negative values may be represented.

Jim.

Edit : Like buses - none come along for ages, then two at once.
Edit again : what sort of fool writes ones compliment, huh?

Share this post


Link to post
Share on other sites
Both signed and unsigned numbers have the same number of possible values, but the ranges are different.

signed 8-bit value (256 possible values): -128 to 127
unsigned 8-bit value (256 possible values): 0 to 255
signed 16-bit value (65536 possible values): -32768 to 32767
unsigned 16-bit value (65536 possible values): 0 to 65535
etc ...

The usual encoding for signed numbers is called "two's complement". Positive two's complement numbers have the same encoding as their unsigned encoding. The encoding of a negative number -X is (~X)+1. ("~" means invert all the bits).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!