+ Post New Thread
Results 1 to 3 of 3
  1. #1
    Newbie level 3
    Points: 23, Level: 1

    Join Date
    Jul 2019
    Posts
    3
    Helped
    0 / 0
    Points
    23
    Level
    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.

    •   AltAdvertisement

        
       

  2. #2
    Super Moderator
    Points: 75,593, Level: 67
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    15,334
    Helped
    3491 / 3491
    Points
    75,593
    Level
    67

    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
    Please don´t contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



    •   AltAdvertisement

        
       

  3. #3
    Advanced Member level 4
    Points: 6,209, Level: 18
    kripacharya's Avatar
    Join Date
    Dec 2012
    Location
    New Delhi
    Posts
    1,197
    Helped
    180 / 180
    Points
    6,209
    Level
    18

    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?



--[[ ]]--