Tengo el siguiente código en C:
int l; short s; l = 0xdeadbeef; s = l;
Assuming int is 32 bits and short is 16 bits, when performing s = l, s will be promoted to 32 bits and after assignment, only lower 16 bits will be kept in s. My question is that when s is promoted to 32 bits, will the additional 16 bits be set to 0x0 or 0xf ?
preguntado el 09 de marzo de 12 a las 15:03
s is not promoted at all. Since
s está firmado y
l is too large to fit in
s, assigning l to s in this case is implementation defined behavior.
Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-deﬁned or an implementation-deﬁned signal is raised.
Assembler have operation for moving todo registrarse o parte de eso
MOV EAX, 0,
MOV AX, 0,
MOV AL, 0 - respectively 32bits, 16bits, 8bits). As
short is 16-bit integer
MOV AX, 0 form would be used, although, that depends on compiler implementation.
I assume you're going to promote s to some wider type. This depends on the destination type: whether it is signed or unsigned. If the destination type is signed there will be signed promotion done. Otherwise -- unsigned promotion. The signed promotion fills higher bits by 0 or 1 depending on the sign of the promoted value.