C++ What is signed/unsigned

Started by
11 comments, last by GameDev.net 19 years, 1 month ago
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.
-Matt S.
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]
Thanks for the quick reply, i think i get what they are now.
-Matt S.
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
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.
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.
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]
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
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.
8 bits to a byte, 1 bit sign, 7 bits integer, 2^7 = 128, range of signed char is thus +/- 128 ?? (thats AFAIK)
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website

This topic is closed to new replies.

Advertisement