![]() |
|
|
|
| ||||||
|
Welcome to the The ProgrammersTalk Community forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
![]() |
![]() | | LinkBack | Thread Tools | Display Modes | ![]() |
| |
| |||
| There isn't really any difference when it comes to C and C++. The languages state that the default of any integer type (char, short (int), int, long (int) and long long (int) in C99) is signed. With normal integers, determining whether to use signed vs unsigned is as simple as asking yourself the following question: do you wish to allow for negative values? If you want to allow yourself to use negative values (or your users if you are creating an API or your fellow coders if you are creating an open source application for others to freely modify), choose signed. Note that with the char data type, signed vs unsigned doesn't matter. 0 to 127 is the same. -128 to -1 (signed) is the same as 128 to 255 (unsigned), even in binary. That's probably why there is an option in some versions of Visual Studio to allow for changing the default to unsigned char. After all, working with negative values seems somewhat unnatural. ![]() __________________ "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off." -- Bjarne Stroustrup, creator of what is now known as C++ For more quotes by Bjarne Stroustrup, check out http://www.research.att.com/~bs/bs_faq.html#really-say-that. |
| The Following 3 Users Say Thank You to rpgfan3233 For This Useful Post: | ||
| ||||
| RPG said it well, let me state it simply: Unsigned means no negative values Signed means negative values are allowed. When using a signed number, an extra bit is used for the +/- indication. To use memory most effectively, choose the appropriate type. Most of the time you won't notice any difference if you choose incorrectly, but your program will be more robust if you choose the proper type. |
| The Following 2 Users Say Thank You to TeraTask For This Useful Post: | ||
HelloWorld (07-22-2007), Lee (07-23-2007) | ||
| ||||
| Thank you both, i will just have to try with my program and see which goes best, if any. ![]() |
| |||
| There is another, more subtle reason why you may choose to switch to unsigned integral types - this is for portability when reading & writing binary data. The problem with negative numbers is that there is no agreed standard way to represent them. Some systems may use one's complement, where the range of numbers represented by 8 bits is -127 through to +127, other systems may use two's complement, where the range of numbers for 8 bits is -128 to +127 (Under one's complement, there is such thing as a 'negative zero' - when the sign bit is set to 1, and all other bits are zero) This poses the problem that you must be sure what system your target platform uses, else your program can end up exhibiting very strange behaviour between different platforms. On the other hand, unsigned integral types are all represented the same way, and differences depend on the size of a byte (in bits). (Although endian'ness and the number of bytes between different types are a factor) Last edited by Bench : 07-23-2007 at 11:31 AM. |
| The Following 2 Users Say Thank You to Bench For This Useful Post: | ||
HelloWorld (07-23-2007), Lee (07-23-2007) | ||
| ||||
| Thanks again, i will be making a game with C++ and DirectX to be played on a windows system, for doing that what should i use? (The book says to use the /J so it changes things to unsigned char so is that what i should type instead of just char?) |
| ||||
| I think you're stressing on it too hard Lee. Unless you're dealing with the ends of the spectrum on values, then you just need to pay attention to this rule: Quote:
|
| The Following User Says Thank You to TeraTask For This Useful Post: | ||
Lee (07-23-2007) | ||
| |||
| Indeed. The Ones complement vs twos complement problem only occurs when reading binary data between different kinds of hardware (Luckily most modern PC hardware uses twos complement anyway). The kind of situations where you'll encounter the difference will be embedded devices, old mainframes, Digital signal processors, calculators.. Provided you don't attempt to mix signed and unsigned values in the same expression, you won't go too far wrong. (Here's a horrible example of what may go wrong if you do )Code: #include <iostream>
int main()
{
char ch = -1;
unsigned int i = ch;
std::cout << i ;
} Last edited by Bench : 07-23-2007 at 03:06 PM. |
| The Following User Says Thank You to Bench For This Useful Post: | ||
Lee (07-23-2007) | ||
![]() |
| Thread Tools | |
| Display Modes | |
| |