Johann L. schrieb:
> Ist aber nicht sonderlich portabel, weil >> für negative Werte
> implementation defined ist.
Korrekt. Jedoch auf den Systemen, die einen Barrelshifter besitzen wohl
anzunehmen. Jedenfalls ist mir noch kein ARM/x86 Compiler die mir dazu
einfallen untergekommen der kein arithmetic shift right durchführt für
signed.
> Zudem ist das Ergebnis undefined, falls in int10 schon ein Bit >= 10
> gesetzt sein sollte (signed overflow).
Nicht Korrekt: (Auszug aus dem Ansi-C std)
" The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated
bits are filled with zeros. If E1 has an unsigned type, the value of
the result is E1 multiplied by the quantity, 2 raised to the power E2,
reduced modulo ULONG_MAX+1 if E1 has type unsigned long, UINT_MAX+1
otherwise. (The constants ULONG_MAX and UINT_MAX are defined in the
header <limits.h> .)
The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1
has an unsigned type or if E1 has a signed type and a nonnegative
value, the value of the result is the integral part of the quotient of
E1 divided by the quantity, 2 raised to the power E2 . If E1 has a
signed type and a negative value, the resulting value is
implementation-defined."
Die "Herausgeschifteten" Bits gehen also einfach nur verloren.
Intern nutzt gcc-arm z.B. dieses Shifting um signed ordinal conversionen
durchzuführen.