Question

J'ai le code suivant en C:

        int l;
        short s;

        l = 0xdeadbeef;
        s = l;

En supposant que l'INT est de 32 bits et de courte de 16 bits, lorsque l'exécution S= L, S sera favorisée à 32 bits et après l'affectation, seuls 16 bits inférieurs seront conservés à l'art.Ma question est que lorsque S est favorisé 32 bits, les 16 bits supplémentaires seront-ils définis sur 0x0 ou 0xf?

Source: http://www.phrack.com/issues.html?numéro= 60 & id= 10

Était-ce utile?

La solution

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.

Autres conseils

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top