Typumwandlung: Integer -> Integer



  • Hallo,
    meine Frage ist wie es sich in verhält, wenn man z.B. einen uint32_t zu uint16_t konvertiert. Im ISO C Standard hab ich dazu folgendes gefunden:

    C11 schrieb:

    6.3.1.3 Signed and unsigned integers
    1 When a value with integer type is converted to another integer type other than _Bool, if the value can be represented by the new type, it is unchanged.

    2 Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.

    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.

    http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1570.pdf

    Hab ich es richtig interpretiert, dass zumindest für vorzeichenlose Typen die höherwertigen Bits einfach verworfen/abgeschnitten werden? Ein Test mit Pelles C hat mein Vermutung zumindest bestätigt, aber das muss ja nicht heißen das es andere Compiler genauso machen.



  • Ja.



  • Danke 🕶


Anmelden zum Antworten