Representation of negative integers

Does ISO-Prolog have any recipes / guidelines for representing negative integers and operations on them? 2, maybe?

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

+3


source to share


2 answers


ISO / IEC 13211-1 has several requirements for integers, but no specific representation is required. If the integer representation is restricted, one of the following conditions is true:

7.1.2 Integer

...

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

In addition, analyzed the functors listed in 9.4 Bitwise functions , ie (>>)/2

, (<<)/2

, (/\)/2

, (\/)/2

, (\)/1

and xor/2

- the implementation of which is defined for negative values. For example.



8.4.1 (→) / 2 - bitwise shift right

9.4.1.1 Description

...
The value must be implemented depending on whether the shift is logical (padding with zeros) or arithmetic (padding with a copy of the sign bit).

The value must be implemented if VS

negative,
or VS

greater than the bit size of an integer.

Note that a specific implementation means that the respective processor must document this in the accompanying documentation. Therefore, you must read the manual before using the appropriate processor.

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

+3


source


Strictly speaking, these are two different questions:

  • The actual physical representation: this is not visible at the Prolog level, and therefore the standard quite rightly has no right to talk about it. Note that many Prolog systems have two or more internal representations (for example, two fixed addition sizes and sign + binary values), but represent one integer type to the programmer.

  • Bitwise Operations Results: While the standard defines these operations, it leaves much of its implementation to the implementation. This is a consequence of (a) there is no way to specify the width of the bit pattern, and (b) not making a definite match between negative numbers and bit patterns.



This means not only that all bitwise operations on negative numbers are not officially portable, but also has the curious effect that the result of bitwise negation is fully implementation-defined (even for positive arguments): it Y is \1

can legally give - 2, 268435454, 2147483646, 9223372036854775806 and etc. All you know is that negation returns the original number twice.

In practice, fortunately, there seems to be a consensus that "Bitwise arithmetic operations behave as if they worked in an unlimited length two's complement representation . "

0


source







All Articles