|
|
Thomas Mertes wrote:
What about something like:
int greater_or_equal4 (unsigned u, unsigned v)
{
return ((int) u) >= ((int) v);
}
that way greater_or_equal4(65000, 10) would return 0
(at least when the size of int and unsigned is 2).
For the record, the original functions, renamed foo1 and foo2
to prevent people from focusing on their result.
int foo1(uint16_t u, uint16_t v)
{
return (int16_t)(u-v) >= 0;
}
int foo2(unsigned u, unsigned v)
{
return ((u-v) & 0xffffU) <= 0x7fffU;
}
On my platform, foo1 and foo2 are equivalent.
My claim is that foo2 is portable, while foo1 is not.
greater_or_equal4 is not equivalent to foo2.
(Consider u=32000 and v=33000)
greater_or_equal4 has even worse shortcomings than foo1, as it
requires int and unsigned int to be 16 bits wide, which is not
true on my platform.
|
|