Re: signed/unsigned integer conversion for right shift seems against

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



"Also sprach Tadeus Prastowo:"
> > int main() {
> >   signed   int x = 0x80000005u;
> >   unsigned int y = 0x00000002u;
> >   signed   int z = x >> y;
> >   printf("0x%0x\n", z);
> >   return 0;
> > }
> > % ./a.out
> > 0xe0000001
> >   ^ SIGNED right shift
> 
> To quote ISO-IEC 9899:1999 draft
> (http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf) Section
> 6.5.7 Paragraph 5: The result of E1 >> E2 is E1 right-shifted E2 bit
> positions.  [...]  If E1 has a signed type and a negative value, the
> resulting value is implementation-defined.
> End quote.
> 
> So, getting what you got should not surprise you, should it?

Suitably snide, but fashionably wrong. The type of E1 is UNSIGNED INT
by the type promotion/conversion rules I quoted.

Perhaps you could point to how and why type promotion/conversion do not
apply to this case, which - to recap - has a mixed sign pair of int args
to an arithmetic operator.  It is my understanding that 

  If the operand that has unsigned integer type has rank greater than
  or EQUAL TO the rank of the type of the other operand, the operand
  with SIGNED integer type IS CONVERTED to the type of the operand with
  UNSIGNED integer type.

That is the case here, as I parse everything I have read, which among
other things defines the type of signed and unsigned versions of an
integer type as equal.

  The rank of any unsigned integer type shall equal the rank of the
  corresponding signed integer type, if any.

Regards

PTB



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux