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

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

 



On Tue, Feb 6, 2018 at 4:29 PM, Peter T. Breuer <ptb@xxxxxxxxxxxxxx> wrote:
> "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.

To quote the same document's Section 6.5.7 Paragraph 3: The integer
promotions are performed on each of the operands. The type of the
result is
that of the promoted left operand.
End quote.

And to quote Section 6.3.1.1 Paragraph 2: [...] If an int can
represent all values of the original type, the value is converted to
an int; otherwise, it is converted to an unsigned int. These are
called the integer promotions. All other types are unchanged by the
integer promotions.
End quote.

So, E1 has type signed int, hasn't it?

> Regards
>
> PTB

--
Best regards,
Tadeus



[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