# 32 bit left shift of a longint variable does not work

1. ## 32 bit left shift of a longint variable does not work

Hi all,

I have two integer variables scaling_0 and scaling_1
I want to attach them so that scaling_0 are the lower 32 bit and scaling_1 are the upper 32 bit. The result should be stored in a longint variable scaling.
I try this:

scaling = longint'(scaling_0) + (longint'(scaling_1) * longint'(2**32));

or also i tried this

scaling = longint'(scaling_0) + (longint'(scaling_1) * << 32);

for scaling_0 = -1 and scaling_1 = 15 i get this result:

scaling = 0xE_FFFFFFFF

I expect 0xF_FFFFFFFF

i also tried the cast with longint unsigned but get a compiler error.  Reply With Quote

•

2. ## Re: 32 bit left shift of a longint variable does not work

Hi,

I'm not a programner, but I often need to read some code.
And here I'm always confused with the varable declarations.

When you say "integer" variable ... are you sure it is a signed_int_32 (are you sure it is not any other bit width ... and not unsigned)
When you say "longint" variable ... are you sure it is a signed_int_64 (are you sure it is not any other bit width ... and not unsigned)

If possible I recommend to declare the variables unambiguously.
Because the compiler need to treat the "cast" differently.

Klaus  Reply With Quote

•

3. ## Re: 32 bit left shift of a longint variable does not work

The 0xEFF... result is correct for the calculations you are doing. Why do you expect any different result?

Try same thing with proper numbers...
-1 = FFFF FFFF FFFF FFFF (64 bits)
15 shifted right 32 bits is =
F 0000 0000
Add them up and you get..

1 0000 000E FFFF FFFF

Right?
Truncate the '1' which is 65th bit. You get
E FFFF FFFF

How many eff's are there in your result?  Reply With Quote

--[[ ]]--