bin and abs of negative integral values ¶The bin operator gets an integral value and yields a
bits value that reflects the internal bits of the integral
value. The semantics of this operator, as defined in the Algol 68
standard prelude, are:
op bin = (L int a) L bits:
if a >= L 0
then L int b := a; L bits;
for i from L bits width by -1 to 1
do (L F of c)[i] := odd b; b := b % L 2 od;
c
fi;
The abs operator performs the inverse operation of
bits. Given a L bits value, it yields the
L int value whose bits representation is the bits value.
The semantics of this operator, as defined in the Algol 68 prelude,
are:
op abs = (L bits a) L int:
begin L int c := L 0;
for i to L bits width
do c := L 2 * c + K abs (L F of a)[i] od;
c
end
Note how the bin of a negative integral value is not
defined: the implicit else-part of the conditional yields
skip, which is defined as any bits value in that context.
Note also how abs doesn’t make any provision to check
whether the resulting value is positive: it assumes it is so.
The GNU Algol 68 compiler, when working in strict Algol 68 mode
(-std=algol68), makes bin to always yield L
bits (skip) when given a negative value, as mandated by the
report. But the skip value is always the bits representation of zero,
i.e. 2r0. Strict Algol 68 programs, however, must not rely on
this.
When GNU extensions are enabled (-std=gnu68) the
bin of a negative value yields the two’s complement bit
pattern of the value rather than zero. Therefore, bin -
short short 2 yields 2r11111110. And abs
short short 2r11111110 yields -2.