#include
#include
int main()
{
int i = -1;
long l = i;
unsigned long ul = i;
enum {
zero = 0
};
printf("sizeof(i) = %zi, sizeof(l) = %zi\n", sizeof(i), sizeof(l));
printf("sizeof(zero) = %zi\n", sizeof(zero));
printf("sizeof(size_t) = %zi\n", sizeof(size_t));
printf("l = i -> %lx\n", l);
printf("(long)i -> %lx\n", (long)i);
printf("(long)-1 -> %lx\n", (long)-1);
printf("-1L -> %lx\n", -1L);
printf("ul -> %lx\n", ul);
i = l;
printf("i = l -> %x\n", i);
return 0;
}
Wyniki:
sizeof(i) = 4, sizeof(l) = 8
sizeof(zero) = 4
sizeof(size_t) = 8
l = i -> ffffffffffffffff
(long)i -> ffffffffffffffff
(long)-1 -> ffffffffffffffff
-1L -> ffffffffffffffff
ul -> ffffffffffffffff
i = l -> ffffffff
Natrafiłem też na taki dokument: http://www.ibm.com/developerworks/library/l-port64.html. Oto ciekawsze fragmenty:
Declarations
To enable your code to work on both 32-bit and 64-bit systems, note the following regarding declarations:
- Declare integer constants using "L" or "U", as appropriate.
- Ensure that an unsigned int is used where appropriate to prevent sign extension.
- If you have specific variables that need to be 32-bits on both platforms, define the type to be int.
- If the variable should be 32-bits on 32-bit systems and 64-bits on 64-bit systems, define them to be long.
- Declare numeric variables as int or long for alignment and performance. Don’t try to save bytes using char or short.
- Declare character pointers and character bytes as unsigned to avoid sign extension problems with 8-bit characters.
In C/C++, expressions are based upon associativity, precedence of operators and a set of arithmetic promotion rules. To enable your expression to work correctly on both 32-bit and 64-bit systems, note the following rules:
- Addition of two signed ints results in a signed int.
- Addition of an int and a long results in a long.
- If one of the operands is unsigned and the other is a signed int, the expression becomes an unsigned.
- Addition of an int and a double results in a double. Here, the int is converted to a double before addition.
Bit shifting
Untyped integral constants are of type (unsigned) int. This might lead to unexpected truncation while shifting.
For example, in the following code snippet, the maximum value for
a
can be 31. This is because the type of 1 << a
is int. long t = 1 << a;
To get the shift done on a 64-bit system,
1L
should be used as shown below: long t = 1L << a;
Oraz taki dokument http://www.unix.org/version2/whatsnew/lp64_wp.html gdzie można znaleźć uzasadnienie dla modelu LP64:
Brak komentarzy:
Prześlij komentarz