Re: Portable replacement
- From: Barry Schwarz <schwarzb@xxxxxxxx>
- Date: Wed, 30 Apr 2008 18:53:49 -0700
On Wed, 30 Apr 2008 10:49:47 +0200, Noob <root@localhost> wrote:
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;
What happens when u is 0, v is UINT16_MAX, and UINT16_MAX > INT16_MAX?
}
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.
Remove del for email
.
- Follow-Ups:
- Re: Portable replacement
- From: Noob
- Re: Portable replacement
- Prev by Date: Re: Functions, arrays, structs and passing by reference
- Next by Date: Re: Functions, arrays, structs and passing by reference
- Previous by thread: Functions, arrays, structs and passing by reference
- Next by thread: Re: Portable replacement
- Index(es):
Relevant Pages
|