¿Cómo se promueven los operandos?

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 ?

fuente: http://www.phrack.com/issues.html?issue=60&id=10

preguntado el 09 de marzo de 12 a las 15:03

Que te hace pensar s is promoted? -

3 Respuestas

Actualmente 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-defined or an implementation-defined signal is raised.

respondido 09 mar '12, 15:03

@Jake I did. It looks confusing. All that "promotion" / "demotion" of s is unmentioned by the standards - it's an artificial explanation. - cnicutar

@Jake A Phrack article is always useful. You just have to take it with a bit of salt. - cnicutar

Assembler have operation for moving todo registrarse o parte de esoMOV 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.

respondido 09 mar '12, 16:03

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.

respondido 09 mar '12, 16:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.