Discussions related to Visual Prolog
choibakk
Active Member
Posts: 30
Joined: 5 Sep 2019 19:30

Domain range checking for bitwise operators

Unread post by choibakk »

I ran into an issue using the "~~" operator that I have distilled into a simple example question (VP10-64 bit):

In C++, I can do something like this:

Code: Select all

int main() {     unsigned char v1 = 0xff;     unsigned char v2 = ~v1 + 0x1;     printf("%d\n", v2); }
In prolog I'm wondering if the following range checking is intended. Consider the following compiler error (or is this a domain issue?):

Code: Select all

        hasDomain(unsigned8, V1),         hasDomain(unsigned8, V2),         V1 = 0xff,         V2 = ~~V1, % --> The value 4294967040 is out of the range [ 0 .. 255 ] for the domain 'core::unsigned8'         V3 = V2+1,         stdio::writef("%x\n", V3),
Thanks,
choibakk
Martin Meyer
VIP Member
Posts: 286
Joined: 14 Nov 2002 0:01

Re: Domain range checking for bitwise operators

Unread post by Martin Meyer »

I think the answer is (Thomas may correct me): The Language Reference says that the operators for bitwise operations are declared (only) on unsigned, unsigned64, and unsignedNative arguments.

When an unsigned8 argument is supplied to the NOT operator it will be converted to the 'best-fitting' domain, thus it will be converted to unsigned. The result of ~~V1, where V1 is of domain unsigned and has value 0, exceeds of course the limits of unsigned8.

To make NOT work with unsigned8 and unsigned16 the upper three resp. two bytes of the result must be zeroed. For example:

Code: Select all

class predicates     not8 : (unsigned8 X) -> unsigned8 NotX. clauses     not8(X) = ~~X ** 0xFF.   clauses     run() :-         stdIo::write(not8(1)).
Regards Martin
choibakk
Active Member
Posts: 30
Joined: 5 Sep 2019 19:30

Re: Domain range checking for bitwise operators

Unread post by choibakk »

Martin,

I had stepped through to see the domain change and the FFFFFF in the upper bytes. Thanks for the clarification on the documentation--I had completely overlooked that line. I hope in the future unsigned8 will be supported as well.

Thanks,
choibakk
Post Reply