¿Cómo se promueven los operandos?
-
10-12-2019 - |
Pregunta
Tengo el siguiente código en C:
int l;
short s;
l = 0xdeadbeef;
s = l;
Suponiendo INT es de 32 bits y cortos son 16 bits, cuando se realizan S= L, S, se promoverán a 32 bits y después de la asignación, solo se mantendrán en S inferiores 16 bits.Mi pregunta es que cuando se asciende a 32 bits, ¿se establecerán los 16 bits adicionales en 0x0 o 0xf?
Fuente: http://www.phrack.com/issues.html?problema= 60 y amp; id= 10
Solución
Actually s
is not promoted at all. Since s
is signed and l
is too large to fit in s
, assigning l to s in this case is implementation defined behavior.
6.3.1.3-3
Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised.
Otros consejos
Assembler have operation for moving whole register or part of it (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.