Option to make unsigned->signed conversion always well-defined?
Pedro Pedruzzi
pedro.pedruzzi@gmail.com
Fri Oct 7 17:20:00 GMT 2011
Em 07-10-2011 02:35, Miles Bader escreveu:
> Pedro Pedruzzi <pedro.pedruzzi@gmail.com> writes:
>> On Thu, Oct 6, 2011 at 11:04 AM, Miles Bader <miles@gnu.org> wrote:
>>> How about:
>>>
>>> bool overflowbit2(unsigned int a, unsigned int b)
>>> {
>>> const unsigned int sum = a + b;
>>> return ~(a ^ b) & sum & 0x80;
>>> }
>>
>> Miles, it is not the same. Take for example (0xff, 0xff). In 8-bit
>> 2's complement, this is (-1, -1) and does not overflow. Your
>> function says it does.
>
> Negative overflow isn't considered overflow...? wacky...
It is. For example -100 + -100 = -200 (less than INT8_MIN; does not
fit). But -1 + -1 = -2, is ok.
--
Pedro
More information about the Gcc
mailing list