C++ What is signed/unsigned
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.
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:
Complements of this site.
[edit]Thanks AP![/edit]
[Edited by - Drew_Benton on March 14, 2005 12:56:56 AM]
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]
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
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.
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_Bentonincorrect, the ranges of an unsigned and signed integer are the same, range is defined as:
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:quoting Collins DictionaryThe 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.
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.
EDIT: fixed my incorrect information
[Edited by - silvermace on March 14, 2005 3:59:05 AM]
Quote:Original post by silvermaceSorry, but it's still -128 to 127. Try adding 128 to [-128,128] and you get [0,256] instead.Quote:Original post by Drew_Bentonincorrect 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:
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:quoting Collins DictionaryThe 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.
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.
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)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement