Except for type void, the basic data types may have various modifiers preceding them.
You use a modifier to alter the meaning of the base type to fit various situations more
precisely. The list of modifiers is shown here:
signed
unsigned
long
short
You use a modifier to alter the meaning of the base type to fit various situations more
precisely. The list of modifiers is shown here:
signed
unsigned
long
short
You can apply the modifiers signed, short, long, and unsigned to integer base
types. You can apply unsigned and signed to characters. You may also apply long to
double. Table 2-1 shows all valid data type combinations, along with their minimal
ranges and approximate bit widths. (These values also apply to a typical C++
implementation.) Remember, the table shows the minimum range that these types will
have as specified by Standard C/C++, not their typical range. For example, on
computers that use two's complement arithmetic (which is nearly all), an integer will
have a range of at least 32,767 to –32,768.
The use of signed on integers is allowed, but redundant because the default integer
declaration assumes a signed number. The most important use of signed is to modify
char in implementations in which char is unsigned by default.
The difference between signed and unsigned integers is in the way that the highorder
bit of the integer is interpreted. If you specify a signed integer, the compiler
generates code that assumes that the high-order bit of an integer is to be used as a sign
flag. If the sign flag is 0, the number is positive; if it is 1, the number is negative.
In general, negative numbers are represented using the two's complement approach,
which reverses all bits in the number (except the sign flag), adds 1 to this number, and
sets the sign flag to 1.
Signed integers are important for a great many algorithms, but they only have half
the absolute magnitude of their unsigned relatives. For example, here is 32,767:
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
If the high-order bit were set to 1, the number would be interpreted as −1. However,
if you declare this to be an unsigned int, the number becomes 65,535 when the highorder
bit is set to 1.
types. You can apply unsigned and signed to characters. You may also apply long to
double. Table 2-1 shows all valid data type combinations, along with their minimal
ranges and approximate bit widths. (These values also apply to a typical C++
implementation.) Remember, the table shows the minimum range that these types will
have as specified by Standard C/C++, not their typical range. For example, on
computers that use two's complement arithmetic (which is nearly all), an integer will
have a range of at least 32,767 to –32,768.
The use of signed on integers is allowed, but redundant because the default integer
declaration assumes a signed number. The most important use of signed is to modify
char in implementations in which char is unsigned by default.
The difference between signed and unsigned integers is in the way that the highorder
bit of the integer is interpreted. If you specify a signed integer, the compiler
generates code that assumes that the high-order bit of an integer is to be used as a sign
flag. If the sign flag is 0, the number is positive; if it is 1, the number is negative.
In general, negative numbers are represented using the two's complement approach,
which reverses all bits in the number (except the sign flag), adds 1 to this number, and
sets the sign flag to 1.
Signed integers are important for a great many algorithms, but they only have half
the absolute magnitude of their unsigned relatives. For example, here is 32,767:
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
If the high-order bit were set to 1, the number would be interpreted as −1. However,
if you declare this to be an unsigned int, the number becomes 65,535 when the highorder
bit is set to 1.
No comments:
Post a Comment