问题
So basically it seems that GNU Prolog use 28bit integer on my 32bit x86 Linux.
The code below can not be compiled:
foo(A) :-
A0 is 0xdeadbeef,
A1 is A0 >> 8,
A2 is A0 >> 16,
A3 is A0 >> 24.
Then I am confused with two typical situations below:
How to represent a
32bit
integer in GNU Prolog (like 0xdeadbeef)? and pushing it further, how to represent a 64 bit integer ? On 64 bit x86 Linux,long long
type in C has 64 bit.When using GNU Prolog to call C, the interface defined in the manual has
integer
,positive
and others.. then if I want to pass a32bit
integer from Prolog to C, which type should I choose..? I found an ad-hoc solution and writing it in this question, is there any better solution?
Could anyone give me some help?
回答1:
GNU Prolog uses bounded integers, whose bounds depend on the architecture (32/64 bits). You can know these limits with (on a 64 bits):
| ?- current_prolog_flag(min_integer,X).
X = -1152921504606846976
yes
| ?- current_prolog_flag(max_integer,X).
X = 1152921504606846975
yes
A workaround to store bigger integers is to spit them into 2 parts and for instance use a Prolog structure to record them.
来源:https://stackoverflow.com/questions/24191587/how-to-use-a-long-int-in-gnu-prolog