converting signed 64 bit - 32 bit
Author Message
converting signed 64 bit - 32 bit

Can anyone tell me the best way of converting a signed 64 bit integer to two
signed 32 bit integers.

I have tried:

signed long long foo;
signed long lo = (signed long)foo;
signed long hi = (signed long)(foo >>32);

When foo = -9 (say) lo = -9 as expected but hi = -1.

Any ideas?

Tue, 20 Apr 2004 20:09:33 GMT
converting signed 64 bit - 32 bit

Quote:

>Can anyone tell me the best way of converting a signed 64 bit integer to
two
>signed 32 bit integers.

I will of course assume that you have checked the bit size of long and long
long.

Quote:

>I have tried:

>signed long long foo;
>signed long lo = (signed long)foo;
>signed long hi = (signed long)(foo >>32);

>When foo = -9 (say) lo = -9 as expected but hi = -1.

>Any ideas?

If I did I'd be out there implementing them, not here helping you!
;)

The >> operater can do one of two things for a signed operand.  1) it can
insert 0's into the high order bits, or 2) it can sign extend the value by
inserting 0's if its positive, or 1's if it's negative.  The platform you're
on does the latter.

The >> operator on an unsigned integral type is guaranteed to shift in 0's.

An 8 bit example using 2's complement binary:

(-9):      1111 1011

(-9 >> 1): 1111 1101   1) sign extended
(-9 >> 2): 1111 1110
(-9 >> 3): 1111 1111
(-9 >> 4): 1111 1111

(-9 >> 1): 0111 1101   2) zero filled
(-9 >> 2): 0011 1110
(-9 >> 3): 0001 1111
(-9 >> 4): 0000 1111

In your case you need to cast the integer to unsigned before you shift.

signed long hi = (signed long)((unsigned long long) foo >> 32);

HTH

--
Peter

Tue, 20 Apr 2004 20:31:17 GMT
converting signed 64 bit - 32 bit

Quote:

> >Can anyone tell me the best way of converting a signed 64 bit integer to
> two
> >signed 32 bit integers.

> I will of course assume that you have checked the bit size of long and
long
> long.

> >I have tried:

> >signed long long foo;
> >signed long lo = (signed long)foo;
> >signed long hi = (signed long)(foo >>32);

> >When foo = -9 (say) lo = -9 as expected but hi = -1.

> >Any ideas?

> If I did I'd be out there implementing them, not here helping you!
> ;)

> The >> operater can do one of two things for a signed operand.  1) it can
> insert 0's into the high order bits, or 2) it can sign extend the value by
> inserting 0's if its positive, or 1's if it's negative.  The platform
you're
> on does the latter.

> The >> operator on an unsigned integral type is guaranteed to shift in
0's.

> An 8 bit example using 2's complement binary:

>   (-9):      1111 1011

>   (-9 >> 1): 1111 1101   1) sign extended
>   (-9 >> 2): 1111 1110
>   (-9 >> 3): 1111 1111
>   (-9 >> 4): 1111 1111

>   (-9 >> 1): 0111 1101   2) zero filled
>   (-9 >> 2): 0011 1110
>   (-9 >> 3): 0001 1111
>   (-9 >> 4): 0000 1111

> In your case you need to cast the integer to unsigned before you shift.

> signed long hi = (signed long)((unsigned long long) foo >> 32);

Thanks for your help Peter, but...
This still gives hi = -1 on my compiler.
I understand that >> is implementation-defined for negative values but is
there a portable way to do this?

Chris

- Show quoted text -

Quote:

> HTH

> --
> Peter

Tue, 20 Apr 2004 20:42:42 GMT
converting signed 64 bit - 32 bit

Quote:

>  Can anyone tell me the best way of converting a signed 64 bit integer to two
>  signed 32 bit integers.

>  I have tried:

>  signed long long foo;
>  signed long lo = (signed long)foo;
>  signed long hi = (signed long)(foo >>32);

>  When foo = -9 (say) lo = -9 as expected but hi = -1.

What result did you expect for hi? If you are using a two's
complement system (and you very probably are), then the higher-order
word contains all-bits-1. That means -1 in two's complement.

In general, don't fiddle with the bits of signed values. If by
your shift you meant a division by (1UL << 32) then code it that
way. Does that give the result you wanted?

Gergo

--
The onset and the waning of love make themselves felt in the uneasiness
experienced at being alone together.
-- Jean de la Bruyere

Tue, 20 Apr 2004 20:54:23 GMT
converting signed 64 bit - 32 bit

Quote:

> >  signed long hi = (signed long)(foo >>32);
>[snip]
>  If by
>  your shift you meant a division by (1UL << 32) ...

I'm sorry, that should be (1ULL << 32). Unsigned long need not be
wider than 32 bit.

Gergo

--
Be valiant, but not too venturous.
Let thy attire be comely, but not costly.
-- John Lyly

Tue, 20 Apr 2004 21:07:44 GMT
converting signed 64 bit - 32 bit
Quote:

> >Can anyone tell me the best way of converting a signed 64 bit integer to
> two
> >signed 32 bit integers.

> I will of course assume that you have checked the bit size of long and long
> long.

> >I have tried:

> >signed long long foo;
> >signed long lo = (signed long)foo;
> >signed long hi = (signed long)(foo >>32);

> >When foo = -9 (say) lo = -9 as expected but hi = -1.

> >Any ideas?

> If I did I'd be out there implementing them, not here helping you!
> ;)

> The >> operater can do one of two things for a signed operand.  1) it can
> insert 0's into the high order bits, or 2) it can sign extend the value by
> inserting 0's if its positive, or 1's if it's negative.  The platform you're
> on does the latter.

> The >> operator on an unsigned integral type is guaranteed to shift in 0's.

> An 8 bit example using 2's complement binary:

>   (-9):      1111 1011

>   (-9 >> 1): 1111 1101   1) sign extended
>   (-9 >> 2): 1111 1110
>   (-9 >> 3): 1111 1111
>   (-9 >> 4): 1111 1111

>   (-9 >> 1): 0111 1101   2) zero filled
>   (-9 >> 2): 0011 1110
>   (-9 >> 3): 0001 1111
>   (-9 >> 4): 0000 1111

> In your case you need to cast the integer to unsigned before you shift.

> signed long hi = (signed long)((unsigned long long) foo >> 32);

> HTH

Not likely to help much. I will shorten the OP's example for readability
to short and char (both signed and two's complement).

short foo = -9;     1111 1111 1111 0111
char lo = foo;      1111 0111
char hi = foo >> 8; 1111 1111

Any bits shifted in by >> are ignored here. lo is naturally -9 and hi is
-1. I don't see anything suprising here.

Of course one's complement or signed magnitude will have different
values but shifting the high order bits of foo to low order will not
change those bits in any case.
--

"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---

Tue, 20 Apr 2004 21:22:22 GMT
converting signed 64 bit - 32 bit

Quote:

>> >Can anyone tell me the best way of converting a signed 64 bit integer to
>> two
>> >signed 32 bit integers.

>> I will of course assume that you have checked the bit size of long and
long
>> long.

>> >I have tried:

>> >signed long long foo;
>> >signed long lo = (signed long)foo;
>> >signed long hi = (signed long)(foo >>32);

>> >When foo = -9 (say) lo = -9 as expected but hi = -1.

>> >Any ideas?

>> If I did I'd be out there implementing them, not here helping you!
>> ;)

>> The >> operater can do one of two things for a signed operand.  1) it can
>> insert 0's into the high order bits, or 2) it can sign extend the value
by
>> inserting 0's if its positive, or 1's if it's negative.  The platform
you're
>> on does the latter.

>> The >> operator on an unsigned integral type is guaranteed to shift in
0's.

>> An 8 bit example using 2's complement binary:

>>   (-9):      1111 1011

>>   (-9 >> 1): 1111 1101   1) sign extended
>>   (-9 >> 2): 1111 1110
>>   (-9 >> 3): 1111 1111
>>   (-9 >> 4): 1111 1111

>>   (-9 >> 1): 0111 1101   2) zero filled
>>   (-9 >> 2): 0011 1110
>>   (-9 >> 3): 0001 1111
>>   (-9 >> 4): 0000 1111

>> In your case you need to cast the integer to unsigned before you shift.

>> signed long hi = (signed long)((unsigned long long) foo >> 32);

>> HTH

>Not likely to help much. I will shorten the OP's example for readability
>to short and char (both signed and two's complement).

>short foo = -9;     1111 1111 1111 0111
>char lo = foo;      1111 0111
>char hi = foo >> 8; 1111 1111

>Any bits shifted in by >> are ignored here. lo is naturally -9 and hi is
>-1. I don't see anything suprising here.

>Of course one's complement or signed magnitude will have different
>values but shifting the high order bits of foo to low order will not
>change those bits in any case.
>--

>"Everything should be made as simple as possible, but not simpler."
>                    --- Albert Einstein ---

True! :)

Perhaps a more illustrive example of what was going through my mind:

signed long long foo = -9;
signed long long lo = (unsigned long)foo;
signed long long hi = (unsigned long)(foo >>32);

But that's not what the OP asked for. :(

I'm tired, 'night all!

--
HTH

Tue, 20 Apr 2004 21:52:47 GMT
converting signed 64 bit - 32 bit

Quote:
>Can anyone tell me the best way of converting a signed 64 bit integer to two
>signed 32 bit integers.

>I have tried:

>signed long long foo;
>signed long lo = (signed long)foo;
>signed long hi = (signed long)(foo >>32);

>When foo = -9 (say) lo = -9 as expected but hi = -1.

>Any ideas?

Please define first the 1 x 64-bit to 2 x 32-bit conversion.

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland

Tue, 20 Apr 2004 21:56:18 GMT
converting signed 64 bit - 32 bit

Quote:

> Can anyone tell me the best way of converting a signed 64 bit integer to two
> signed 32 bit integers.

You cannot in general do this meaningfully.  Here's the reason:

A signed 64-bit integer has (the equivalent of)
1 sign bit + 63 value bits.
A signed 32-bit integer has (the equivalent of)
1 sign bit + 31 value bits.

You are trying to squeeze 63 value bits into 2*31 = 62 value bits.

You could use a signed 32-bit integer + an *unsigned* 32-bit integer.

Note that explicit sizes in terms of bits are not portable across
C90 implementations.

Wed, 21 Apr 2004 01:59:50 GMT
converting signed 64 bit - 32 bit

Quote:
>Can anyone tell me the best way of converting a signed 64 bit integer to two
>signed 32 bit integers.

>I have tried:

>signed long long foo;
>signed long lo = (signed long)foo;
>signed long hi = (signed long)(foo >>32);

>When foo = -9 (say) lo = -9 as expected but hi = -1.

>Any ideas?

That is certainly a conversion from a signed 64 bit integer to two signed
32 bit integers. If the results aren't what you expected then you need to
specify what it is you expected/wanted and why.

As others have poiinted out you woul;d probably be better off converting
to a 32 bit signed and a 32 bit unsigned integer.

--
-----------------------------------------

-----------------------------------------

Wed, 21 Apr 2004 19:48:15 GMT
converting signed 64 bit - 32 bit
On Fri, 2 Nov 2001 12:09:33 -0000, "Chris West"

Quote:

>Can anyone tell me the best way of converting a signed 64 bit integer to two
>signed 32 bit integers.

>I have tried:

>signed long long foo;
>signed long lo = (signed long)foo;
>signed long hi = (signed long)(foo >>32);

>When foo = -9 (say) lo = -9 as expected but hi = -1.

>Any ideas?

Why does this surprise you?  It is exactly what I see when I dump such
a variable in hex and then manually treat it as two 32 bit quantities.

What are you expecting?  Give some examples.

<<Remove the del for email>>

Thu, 22 Apr 2004 01:46:24 GMT

 Page 1 of 1 [ 11 post ]

Relevant Pages