Recent

Author Topic: QWORD behaving as Int64 instead of unsigned Int64  (Read 1668 times)

440bx

  • Hero Member
  • *****
  • Posts: 940
QWORD behaving as Int64 instead of unsigned Int64
« on: July 24, 2018, 10:13:35 pm »
Hello,

Is there some compiler directive needed to get QWORD to be a 64bit unsigned integer ?

the following definitions
Code: Pascal  [Select]
  1. var
  2.   Greater : QWORD = $FFFFFFFFFFFFFFFF;  // msb set
  3.   Lesser  : QWORD = $7FFFFFFFFFFFFFFF;  // msb not set
  4.  
  5.  
when comparing those two, the result is that the variable Greater is smaller than Lesser which shouldn't happen if they are truly unsigned.

Also when using writeln to output Greater, the result is -1 instead of the expected 18446744073709551615.

Is there something I need to tell the compiler to treat QWORD as an unsigned 64bit integer ?

Thank you for your help.
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Nitorami

  • Sr. Member
  • ****
  • Posts: 366
Re: QWORD behaving as Int64 instead of unsigned Int64
« Reply #1 on: July 24, 2018, 10:54:28 pm »
Per default, the parser treats this as int64. You need to write qword (...) to have it parsed as qword.

440bx

  • Hero Member
  • *****
  • Posts: 940
Re: QWORD behaving as Int64 instead of unsigned Int64
« Reply #2 on: July 24, 2018, 11:07:27 pm »
Per default, the parser treats this as int64. You need to write qword (...) to have it parsed as qword.

I am not clear about what you mean.  Using the variables Greater and Lesser in my first post, what do I have to do to get the comparison to show that the variable Greater is larger/above than lesser ?

This statement:

Code: Pascal  [Select]
  1. if Greater > Lesser then writeln('Greater') else writeln('Lesser');

writes 'Lesser' instead of 'Greater'.  What do I have to do to make it output 'Greater' ?

Thank you for your help.

using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Cyrax

  • Hero Member
  • *****
  • Posts: 737
Re: QWORD behaving as Int64 instead of unsigned Int64
« Reply #3 on: July 24, 2018, 11:11:21 pm »
He means this.

Code: Pascal  [Select]
  1. var
  2.   Greater : QWORD = QWORD($FFFFFFFFFFFFFFFF);  // msb set
  3.   Lesser  : QWORD = QWORD($7FFFFFFFFFFFFFFF);  // msb not set
  4.  
  5.  

440bx

  • Hero Member
  • *****
  • Posts: 940
Re: QWORD behaving as Int64 instead of unsigned Int64
« Reply #4 on: July 24, 2018, 11:27:06 pm »
He means this.

Code: Pascal  [Select]
  1. var
  2.   Greater : QWORD = QWORD($FFFFFFFFFFFFFFFF);  // msb set
  3.   Lesser  : QWORD = QWORD($7FFFFFFFFFFFFFFF);  // msb not set
  4.  
  5.  

Thank you for clarifying that.  I tried it and the comparison still gives the wrong result.

Here is the code, simple as can be:
Code: Pascal  [Select]
  1. var
  2.   Greater : QWORD = QWORD($FFFFFFFFFFFFFFFF);
  3.   Lesser  : QWORD = QWORD($7FFFFFFFFFFFFFFF);
  4.  
  5. begin
  6.   if QWORD(Greater) > QWORD(Lesser) then
  7.   begin
  8.     writeln('Greater is greater than lesser as it should be');
  9.   end
  10.   else
  11.   begin
  12.     writeln('PROBLEM: Greater is smaller than lesser');
  13.   end;
  14.  
  15.   writeln('Greater : ', QWORD(Greater));
  16.  
  17.  

I get that Greater is smaller than Lesser and the output of the writeln is -1.

using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Nitorami

  • Sr. Member
  • ****
  • Posts: 366
Re: QWORD behaving as Int64 instead of unsigned Int64
« Reply #5 on: July 24, 2018, 11:37:47 pm »
I am getting the correct result here. FPC 3.0.4, 32bit, Windows 10.

Same with Lazarus 1.6.4, using FPC 3.0.4, 64bit
« Last Edit: July 24, 2018, 11:46:07 pm by Nitorami »

440bx

  • Hero Member
  • *****
  • Posts: 940
Re: QWORD behaving as Int64 instead of unsigned Int64
« Reply #6 on: July 25, 2018, 12:06:22 am »
I am getting the correct result here. FPC 3.0.4, 32bit, Windows 10.

Same with Lazarus 1.6.4, using FPC 3.0.4, 64bit

Nitorami, does the writeln output -1 or what the value should really be for you ?

I haven't tried the code I posted in 32bit.  I've only tried it in 64bit.  Can you try it in 64bit ?   In the meantime, I'll try in 32bit see what I get.

Thanks.

ETA: I've just tried it in 32bit and the result is the same.  It still says the Greater is smaller than lesser.  I am using FPC 3.0.4 and Lazarus 1.8.2.

« Last Edit: July 25, 2018, 12:15:06 am by 440bx »
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Bart

  • Hero Member
  • *****
  • Posts: 3451
    • Bart en Mariska's Webstek
Re: QWORD behaving as Int64 instead of unsigned Int64
« Reply #7 on: July 25, 2018, 12:22:21 am »
Tested on Win10-65 with fpc 3.0.4.

Code: [Select]
C:\Users\Bart\LazarusProjecten\ConsoleProjecten>test
32-bit
Greater is greater than lesser as it should be
Greater : 18446744073709551615

C:\Users\Bart\LazarusProjecten\ConsoleProjecten>test
64-bit
Greater is greater than lesser as it should be
Greater : 18446744073709551615

Bart

440bx

  • Hero Member
  • *****
  • Posts: 940
Re: QWORD behaving as Int64 instead of unsigned Int64
« Reply #8 on: July 25, 2018, 12:32:09 am »
Tested on Win10-65 with fpc 3.0.4.

Code: [Select]
C:\Users\Bart\LazarusProjecten\ConsoleProjecten>test
32-bit
Greater is greater than lesser as it should be
Greater : 18446744073709551615

C:\Users\Bart\LazarusProjecten\ConsoleProjecten>test
64-bit
Greater is greater than lesser as it should be
Greater : 18446744073709551615

Bart

I wish I'd get those results.  Any idea as to why I get different results ?  BTW, I'm using Windows 7, 64bit  (I don't have a Windows 10 installation to test the program on.)

I've attached a screenshot with my compile settings and the output I get.
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Cyrax

  • Hero Member
  • *****
  • Posts: 737
Re: QWORD behaving as Int64 instead of unsigned Int64
« Reply #9 on: July 25, 2018, 12:36:49 am »
Can you give us a access to your program source code?

josh

  • Hero Member
  • *****
  • Posts: 745
Re: QWORD behaving as Int64 instead of unsigned Int64
« Reply #10 on: July 25, 2018, 12:38:46 am »
Correct Results here on Win 10 64 FPC 3.0.5 on both 32 and 64bit
Correct Results Win 10 64 FPC 3.1.1 on both 32 and 64 bit.
Correct Result Win 10 64 FPC 2.7.1 32bit only

Sorry no 3.0.4 to test on PC, I do have on a MAC to test, so could add later....


Attached simple form app, using your code displaying OK.
Just run and click the Button, stuff is added to memo1.
Does this work on yours???
« Last Edit: July 25, 2018, 12:43:47 am by josh »
Development Installation Lazarus 1.3, FPC 2.7.1,Windows 7/8 32/64, OSX, *nix

Test Environment Lazarus & FPC Trunk on Windows and OSX (Cocoa Mainly on OSX). Testing also Crosscompile windows to OSX.. 
Any posts made from 2015 will be based on Lazarus Trunk.

440bx

  • Hero Member
  • *****
  • Posts: 940
Re: QWORD behaving as Int64 instead of unsigned Int64
« Reply #11 on: July 25, 2018, 01:09:30 am »
Attached simple form app, using your code displaying OK.
Just run and click the Button, stuff is added to memo1.
Does this work on yours???
Yes, it does!


Can you give us a access to your program source code?
Given that Josh's program works fine.  Something in my code is causing the problem.  At this time, I cannot fathom what it may be but, obviously I did something that resulted in QWORDs not working as expected.   
 
I commented out all the code in my program except the definitions of Greater and Lesser, the if statement and the writeln.  After that, it worked. 
I'm just going to progressively uncomment sections of the code to figure out what caused the problem.

Thank you all for your help, greatly appreciate it.
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

440bx

  • Hero Member
  • *****
  • Posts: 940
Re: QWORD behaving as Int64 instead of unsigned Int64
« Reply #12 on: July 25, 2018, 01:22:54 am »
I should have thought about why it was not working as expected....

I am using code that was originally written for Delphi and one of the units declares a type named QWORD equal int64.  That explains everything.

Thank you for your efforts, the fact that it worked for you and not form me made it clear that the problem was somewhere in "my" code.



using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 8163
Re: QWORD behaving as Int64 instead of unsigned Int64
« Reply #13 on: July 25, 2018, 07:40:11 am »
I am using code that was originally written for Delphi and one of the units declares a type named QWORD equal int64.  That explains everything.
That is possibly not even your own unit, but Delphi itself: I got bitten once ( a long time ago) because Delphi 5 didn't have a true qword and just an alias for int64.
I believe even my beloved D7 has the same issue. On such low level thingies FPC is usually more precise, not only in this case.
Read the manuals and if you are a professional get a proper education in computer science. Makes the forum a lot cleaner.

440bx

  • Hero Member
  • *****
  • Posts: 940
Re: QWORD behaving as Int64 instead of unsigned Int64
« Reply #14 on: July 25, 2018, 11:56:47 am »
That is possibly not even your own unit, but Delphi itself: I got bitten once ( a long time ago) because Delphi 5 didn't have a true qword and just an alias for int64.

That was a good reminder that using somebody else's code is not risk free even when it is supposed to be a blackbox.  Live and re-learn.... oh well.
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.