Jump to content
  • Advertisement
Sign in to follow this  
ender_341

C++ What is signed/unsigned

This topic is 4907 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

i searched msdn and only found how to convert between the 2, the book im using didn't define them, but is using it in examples.

Share this post


Link to post
Share on other sites
Advertisement
signed means it has a positive/negative sign and unsigned is the opposite. Here is a google page of more results More often than not, when a data type is unsigned, it has twice the number capacity range.

unsigned chars are 0 - 255, while signed are -127 - 127 -127 - 128. Here's a quote:

Quote:
signed and unsigned

In addition, all these types come in two varieties: signed and unsigned. Sometimes you need negative numbers, and sometimes you don't. Integers (short and long) without the word "unsigned" are assumed to be signed. signed integers are either negative or positive. unsigned integers are always positive. Remember: signed is the default for integer variables.

Because you have the same number of bytes (and therefore the number of bits) for both signed and unsigned integers, the largest number you can store in an unsigned integer is twice as big as the largest positive number you can store in a signed integer. An unsigned short integer can handle numbers from 0 to 65,535. Half the numbers represented by a signed short are negative, thus a signed short can only represent numbers from –32,768 to 32,767.

Bit for signed and unsigned Integers

For signed integers, one bit is used to handle the sign itself; for unsigned that bit is used for more numbers. If you counted all the possible numbers available in signed and unsigned, you would find that you have the same count. The difference is how they are represented!


Complements of this site.

[edit]Thanks AP![/edit]

[Edited by - Drew_Benton on March 14, 2005 12:56:56 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
More often than not, when a data type is unsigned, it has twice the number capacity.

unsigned chars are 0 - 255, while signed are -127 - 127. Here's a quote:


sorry don't know how to quote. but thats not very accurate at all. when something is unsigned it has the same capacity just a different range, do the math. and a signed char has a range of -128 to 127

Share this post


Link to post
Share on other sites
Assume that numbers are stored in the usual fashion (two's complement). For an N-bit integral type T, a signed T can then store numbers in the range [-2N-1, 2N-1-1], while an unsigned T can store the range [0, 2N].

Note that short, int and long default to signed, but char does not. char, unsigned char, and signed char are all distinct types, and whether char happens to have a sign or not is implementation defined.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
sorry don't know how to quote. but thats not very accurate at all. when something is unsigned it has the same capacity just a different range, do the math. and a signed char has a range of -128 to 127


Yea, your right. Typo and mis-speak on my part. Thanks for pointing that out.

Share this post


Link to post
Share on other sites
Quote:
Original post by Drew_Benton
More often than not, when a data type is unsigned, it has twice the number capacity range.

unsigned chars are 0 - 255, while signed are -127 - 127 -127 - 128. Here's a quote:
incorrect, the ranges of an unsigned and signed integer are the same, range is defined as:
Quote:
quoting Collins Dictionary
Mathematics. The set of all values a given function may take on.
Statistics. The difference or interval between the smallest and largest values in a frequency distribution.
The statistical range [0,255] is the same as [-128,127] and as it so happens, the mathematical range's "capacity" does not change, it merely shifts domain.

EDIT: fixed my incorrect information

[Edited by - silvermace on March 14, 2005 3:59:05 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by silvermace
Quote:
Original post by Drew_Benton
More often than not, when a data type is unsigned, it has twice the number capacity range.

unsigned chars are 0 - 255, while signed are -127 - 127 -127 - 128. Here's a quote:
incorrect on two counts, first: signed-char can hold any value between -128 and 128 inclusive, second: the ranges of an unsigned and signed integer are the same, range is defined as:
Quote:
quoting Collins Dictionary
Mathematics. The set of all values a given function may take on.
Statistics. The difference or interval between the smallest and largest values in a frequency distribution.
The statistical range [0,255] is the same as [-128,128] and as it so happens, the mathematical range's "capacity" does not change, it merely shifts domain.
Sorry, but it's still -128 to 127. Try adding 128 to [-128,128] and you get [0,256] instead.
This isn't strictly true either since C supports one's complement architectures where the range can be defined as [-127,127]. And still these are only the minimum allowed ranges, a char could (and does on some systems) use a full 32-bit word for example.

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!