64 bit math novice question

Hello,

I am new and I would highly appreciate help for a 64 Bit math question:
signed long long Off = 0;
signed long long u = 0;
unsigned int psensor_c2 = 0;
unsigned int psensor_c4 = 0;
signed int dT = 0;
int i = 0;

i = psensor_c2 << 16; // i = f.e. 0x012345

u = ((signed long long) (psensor_c4 * dT)) / 128; // u = 0x900000

line3: Off = (signed long long) 0x900000 + 0x012345;
line4: Off = (signed long long) i + u;

line3 gives me as result 0x912345
line4 gives me as result 0xffffffff00912345

My problem is: I have a signed integer with a maximum of 40 Bit length. So I need 64 Bit math. And I tell this the compiler with the hint (signed long long).
Why does exactly the same line of code give different results ? I expect the line3 result and don t understand why in line 4 the ffffffff are added for the high word.

Do the experts recommend me to write this in assembler, so that I can be sure that it is done exactly the way I want to have my 64 Bit math. Or are there compiler specialist, who know, how to tell the compiler, that he should add a signed 64 bit value to a 32 bit integer.

Any hints are welcome.

Parents
  • 0xffffffff00912345 is the same as
    unsigned 18446744069424096069
    signed -4285455547

    But you are not showing us everything.

    i can't be 0x12345 if you created it by shifting something else 16 steps left. The low four digits must be zero from the shift.

    When computing u, you are playing with signed and unsigned, but not letting us see what values you use - if you got a sign extend because the high but of a signed entity was set.

    Printing a negative value with unsigned formatting results in a printed number that starts with the high bit(s) set.

    You are not showing if you are using any formatted printing, or if you are taking the variable values from the debugger.

Reply
  • 0xffffffff00912345 is the same as
    unsigned 18446744069424096069
    signed -4285455547

    But you are not showing us everything.

    i can't be 0x12345 if you created it by shifting something else 16 steps left. The low four digits must be zero from the shift.

    When computing u, you are playing with signed and unsigned, but not letting us see what values you use - if you got a sign extend because the high but of a signed entity was set.

    Printing a negative value with unsigned formatting results in a printed number that starts with the high bit(s) set.

    You are not showing if you are using any formatted printing, or if you are taking the variable values from the debugger.

Children
More questions in this forum