Search code examples
prologiso-prolog

Representation of negative integers


Does ISO-Prolog have any prescriptions / recommendations regarding the representation of negative integers and operations on them? 2's complement, maybe?

Asking as a programmer/user: Are there any assumptions I can safely make when performing bit-level operations on negative integers?


Solution

  • ISO/IEC 13211-1 has several requirements for integers, but a concrete representation is not required. If the integer representation is bounded, one of the following conditions holds

    7.1.2 Integer

    ...

    minint = -(*minint)
    minint = -(maxint+1)

    Further, the evaluable functors listed in 9.4 Bitwise functors, that is (>>)/2, (<<)/2, (/\)/2, (\/)/2, (\)/1, and xor/2 are implementation defined for negative values. E.g.,

    8.4.1 (>>)/2 – bitwise right shift

    9.4.1.1 Description

    ...
    The value shall be implementation defined depending on
    whether the shift is logical (fill with zeros) or arithmetic
    (fill with a copy of the sign bit).

    The value shall be implementation defined if VS is negative,
    or VS is larger than the bit size of an integer.

    Note that implementation defined means that a conforming processor has to document this in the accompanying documentation. So before using a conforming processor, you have to read the manual.

    De facto, there is no current Prolog processor (I am aware of) that does not provide arithmetic right shift and does not use 2's complement.