Is this a guaranteed signed/unsigned conversion?
- From: CBFalconer <cbfalconer@xxxxxxxxx>
- Date: Fri, 20 May 2005 20:08:26 GMT
Consider:
#include <stdlib.h>
/* An elementary optimizer is expected to remove most code */
/* Return the absolute value */
unsigned long ulabs(long j)
{
if (0 == LONG_MIN + LONG_MAX) {
if (j < 0) return -j;
else return j;
}
else if (LONG_MIN == j) return 1U + j + ULONG_MAX;
else if (j < 0) return -j;
else return j;
} /* ulabs */
Is this guaranteed to convert all long values to their absolute
unsigned long equivalent, assuming that the desired result is
representable as an unsigned long. Does it work over 2's, 1's
complement and sign-magnitude? Is there a better method? labs()
is not guaranteed.
--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
.
- Follow-Ups:
- Re: Is this a guaranteed signed/unsigned conversion?
- From: Lawrence Kirby
- Re: Is this a guaranteed signed/unsigned conversion?
- From: Christian Bau
- Re: Is this a guaranteed signed/unsigned conversion?
- From: Eric Sosman
- Re: Is this a guaranteed signed/unsigned conversion?
- From: Michael Mair
- Re: Is this a guaranteed signed/unsigned conversion?
- Prev by Date: find name problem
- Next by Date: Re: find name problem
- Previous by thread: find name problem
- Next by thread: Re: Is this a guaranteed signed/unsigned conversion?
- Index(es):
Relevant Pages
|