Re: Convert Java <long> to C <?>
- From: Jordan Abel <random832@xxxxxxxxx>
- Date: 31 Mar 2006 04:03:03 GMT
On 2006-03-31, Chris Torek <nospam@xxxxxxxxx> wrote:
In article <slrne2ovhv.21gm.random832@xxxxxxxxxxxxx>
Jordan Abel <random832@xxxxxxxxx> wrote:
Java values are, IIRC, stored as twos-complement big-endian....
unsigned long long x = 0;
signed long long y;
/* sign-extend if long long is wider than 64 bits */
#if ULLONG_MAX > 0xFFFFFFFFFFFFFFFFull
if(x & 0x8000000000000000ull) x |= ~0xFFFFFFFFFFFFFFFFull;
#endif
y = x;
This only works if the C implementation happens to use two's
complement as well (of course, most do).
I thought conversion of unsigned to signed always converted to
[unsigned value]-(UINT_MAX+1) where that result would be in the range of
the signed type and the original value would not.
But of course, you already have the conversion code using a "#if",
so we can handle the problem by doing the arithmetic mod 2-sup-64
if ULLONG_MAX is 0xffffffffffffffffULL:
that #if was to sign-extend if long long is wider than 64 bits.
.
- References:
- Convert Java <long> to C <?>
- From: Simple Simon
- Re: Convert Java <long> to C <?>
- From: Jordan Abel
- Re: Convert Java <long> to C <?>
- From: Chris Torek
- Convert Java <long> to C <?>
- Prev by Date: Re: checking constant conditions on compile time
- Next by Date: Re: More on pointers to pointers.
- Previous by thread: Re: Convert Java <long> to C <?>
- Next by thread: Re: Convert Java <long> to C <?>
- Index(es):
Relevant Pages
|