Integer: Difference between revisions
mNo edit summary |
m (typos) |
||
Line 9: | Line 9: | ||
alternative types to '''integer''' are '''short''', which is a 16-bit integer taking up half the memory and with a half the range of values it is capable of storing, and '''long''', a 64-bit integer with double the range of values and double the memory usage, or sometimes even [[char]] | alternative types to '''integer''' are '''short''', which is a 16-bit integer taking up half the memory and with a half the range of values it is capable of storing, and '''long''', a 64-bit integer with double the range of values and double the memory usage, or sometimes even [[char]] | ||
Also,'''integers''', '''shorts''', and '''longs''' can be declared as ''unsigned'', this identifier eliminates the [[variable]]'s ability to store negative values and doubles its capacity of positive values. Therefore if you know you wont need any negative values, an ''unsigned short'' will have just as good a range of values as an ''int'', and with half the memory usage. | Also, '''integers''', '''shorts''', and '''longs''' can be declared as ''unsigned'', this identifier eliminates the [[variable]]'s ability to store negative values and doubles its capacity of positive values. Therefore if you know you wont need any negative values, an ''unsigned short'' will have just as good a range of values as an ''int'', and with half the memory usage. | ||
The following are all valid: | The following are all valid: | ||
Line 20: | Line 20: | ||
unsigned long positiveLong; | unsigned long positiveLong; | ||
Remember, if you want to store a value that is always negative, just use an | Remember, if you want to store a value that is always negative, just use an unsigned type and then stick a negative sign in front of it! | ||
unsigned short health = 100; | unsigned short health = 100; | ||
unsigned short attackEffect = 13; | |||
unsigned short recoveredHealth = 5; | unsigned short recoveredHealth = 5; | ||
short netChange = recoveredHealth - attackEffect; | short netChange = recoveredHealth - attackEffect; |
Revision as of 12:57, 23 October 2005
An integer is a variable that stores a whole number (in other words, a number without a decimal point.)
An integer is declared as:
int someIntegerName;
a standard integer is also know as a 32-bit integer. alternative types to integer are short, which is a 16-bit integer taking up half the memory and with a half the range of values it is capable of storing, and long, a 64-bit integer with double the range of values and double the memory usage, or sometimes even char
Also, integers, shorts, and longs can be declared as unsigned, this identifier eliminates the variable's ability to store negative values and doubles its capacity of positive values. Therefore if you know you wont need any negative values, an unsigned short will have just as good a range of values as an int, and with half the memory usage.
The following are all valid:
int normalInteger; unsigned int positiveInteger; short shortInteger; unsigned short positiveShort; long longInteger; unsigned long positiveLong;
Remember, if you want to store a value that is always negative, just use an unsigned type and then stick a negative sign in front of it!
unsigned short health = 100; unsigned short attackEffect = 13; unsigned short recoveredHealth = 5; short netChange = recoveredHealth - attackEffect; health += netChange;