Casten von unsigned 16bit nach signed 32bit

Gast #4006395
Lesenswert?

Hallo,

wie ist das genau mit dem Casten in C?
1
unsigned short u16 = 40000;
2
signed long s32;
3

4
s32 = u16;

Hier wird sowohl die Bitgröße als auch das Vorzeichen geändert. GCC 
wandelt offenbar erst von

unsigned short in unsigned long

um und dann von

unsigned long in signed long.

Ist dieses Verhalten in C (C89, C99, C11) definiert oder ist das 
Compiler-spezifisch?

Grüße
Jörg
#4006446
Lesenswert?

Hier ist, was der C-Standard dazu zu sagen hat
1
6.3 Conversions
2

3
6.3.1 Arithmetic operands
4

5
6.3.1.3 Signed and unsigned integers
6
1 When a value with integer type is converted to another integer
7
  type other than _Bool, if the value can be represented by the new type,
8
  it is unchanged.
9
2 Otherwise, if the new type is unsigned, the value is converted by
10
  repeatedly adding or subtracting one more than the maximum value that
11
  can be represented in the new type until the value is in the range
12
  of the new type.60)
13
3 Otherwise, the new type is signed and the value cannot be represented
14
  in it; either the result is implementation-defined or an
15
  implementation-defined signal is raised.


http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf

Da ein signed long alle Werte eines unsigned short darstellen kann, 
kommt 6.3.1.3-1 zum Zug, nachdem sich keine Veränderung im Wert ergibt. 
Diese Regelung erfordert mehr oder weniger, dass beim Casten in einen 
größeren Datentyp erst mal die Bitgröße angepasst werden muss, ehe dann 
eine mögliche Neuinterpretation eines Vorzeichens ins Spiel kommt. In 
der Praxis und unter der Annahme einer 2-er Komplement Darstellung bei 
signed Datentypen, läuft es in der Praxis beim Casten in einen höheren 
Datentyp darauf hinaus
1
     Quelldatentyp ist ein unsigned Typ ?
2
   Ja            |            Nein
3
   +-------------+---------------+
4
   |                             |
5
   v                             |
6
  hänge entsprechen              v
7
  viele 0 Bytes links an      MSB vom Wert gesetzt?
8
                          Nein   |                ja
9
                          +------+-----------------+
10
                          |                        |
11
                        hänge entsprechend        hänge entsprechend
12
                        viele 0x00 Bytes links    viele 0xFF Bytes links
13
                        an                        an
Gast #4006450
Lesenswert?

Hallo Jörg,

ich nehme an, du spielst auf die im C-Standard spezifizierte Integer 
Promotion an. Von der CERT gibt es im Secure Coding Umfeld eine recht 
gute Erklärung hierzu:

https://www.securecoding.cert.org/confluence/display/seccode/INT02-C.+Understand+integer+conversion+rules

"Integer types smaller than int are promoted when an operation is 
performed on them. If all values of the original type can be represented 
as an int, the value of the smaller type is converted to an int; 
otherwise, it is converted to an unsigned int."

mfg
ChriS

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren