Re: Portable replacement
- From: Noob <root@localhost>
- Date: Wed, 30 Apr 2008 10:49:47 +0200
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.
.
- References:
- Portable replacement
- From: Noob
- Re: Portable replacement
- From: pete
- Re: Portable replacement
- From: Noob
- Re: Portable replacement
- From: thomas . mertes
- Portable replacement
- Prev by Date: Free YouTube Video Downloader Software
- Next by Date: free fiction
- Previous by thread: Re: Portable replacement
- Next by thread: Re: Portable replacement
- Index(es):
Relevant Pages
|