Re: Convert Java <long> to C <?>



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.
.



Relevant Pages