You should use unsigned types
- Only for quantities that are never negative (B-1)
- (B-1) Such as populations and bean counts
To create unsigned versions of the basic integer types
- You just use the keyword unsigned to modify the declarations (A-1):
Example of (A-1)
unsigned short change; // unsigned short type unsigned int univac; // unsigned int type unsigned eniac; // also unsigned int unsigned long edvac; // unsigned long type
Note that unsigned by itself
- Is short for unsigned int
(A-2) illustrates the use of unsigned types
- It also shows you what happen if your program tries to go beyond the limits for integer types (B-2)
- (B-2) Finally, it gives you one last look at the preprocessor # define statement
Example of (A-2)
#include <iostream> #define ZERO 0 // makes ZERO symbol for 0 value #include <climits> // defines INT_MAX as largest int value int main() { using namespace std; short johnPresperEckert = SHRT_MAX; // initialize a variable to max value unsigned short johnWilliamMauchly = johnPresperEckert; // okay if variable johnPresperEckert already defined cout << "Eckert has " << johnPresperEckert << " dollars and Mauchly has " << johnWilliamMauchly; cout << " dollars deposited." << endl << " Add $1 to each account. "<< endl << "Now "; johnPresperEckert = johnPresperEckert + 1; johnWilliamMauchly = johnWilliamMauchly + 1; cout << "Eckert has " << johnPresperEckert << " dollars and Mauchly has " << johnWilliamMauchly; cout << " dollars deposited.\nPoor Eckert." << endl; johnPresperEckert = ZERO; johnWilliamMauchly = ZERO; cout << "Eckert has " << johnPresperEckert << " dollars and Mauchly has " << johnWilliamMauchly; cout << " dollars deposited." << endl << " Take $1 from each account. "<< endl << "Now "; johnPresperEckert = johnPresperEckert - 1; johnWilliamMauchly = johnWilliamMauchly - 1; cout << "Eckert has " << johnPresperEckert << " dollars and Mauchly has " << johnWilliamMauchly; cout << " dollars deposited." << endl << "Lucky Mauchly" << endl; return 0; }
Compatibility Note
- The example (A-2), uses the climits file; older compilers might need to use limits.h, and some very old compilers might not have either file available
Here’s the output from the program in (A-2):

The program
- Sets a short variable (johnPresperEckert) and an unsigned short variable (johnWilliamMauchly) to the largest short value, which is 32,767 on our system (B-3)
- (B-3) Then it adds 1 to each value (B-4)
- (B-4) This causes no problems for johnWilliamMauchly because the new value is still much less than the maximum value for an unsigned integer (B-5)
- (B-5) But johnPresperEckert goes from 32,767 to -32,768 (B-6)
- (B-6) Similarly, subtracting 1 from 1 from 0 creates no problems for johnPresperEckert, but it makes the unsigned variable johnWilliamMauchly go from 0 to 65,535 (B-7)
- (B-7) As you can see, these integers behave much like an odometers (B-8)
- (B-8) If you go past the limit, the values just start over at the other end of the range
- (Image-1) C++ guarantees that unsigned types behave in this fashion
- However, C++ doesn’t guarantee that signed integer types can exceed their limits (overflow and underflow) without complaint, but that is the most common behavior on current implementations
(Image-1) Typical overflow behavior for integers


Beyond long
- C99 has added a couple new types that most likely will be part of the next edition of the C++ Standard (B-9)
- Indeed, many C++ compilers already support them
- (B-9) The types are long long and unsigned long long. Both are guaranteed to be at least 64 bits and to be at least as wide as the long and unsigned long types

Discussion
No comments for “Unsigned Types”