Signed integers encoded in unsigned words #
An integer is stored as twice its magnitude plus a low sign bit. Zero has sign bit zero;
the natural number one is not a canonical encoding. Unlike two's complement, this encoding
does not depend on the word width. A signed w-bit integer, including -2^(w-1), fits in
one unsigned word of w + 1 bits. The signed range is the existing GenericRAM.Signed.Fits.
Main definitions #
Signed.encode,Signed.decode: the sign-and-magnitude representation and its interpretation.
Main results #
Signed.decode_encode: encoding preserves the integer.Signed.encode_fits: one additional unsigned bit suffices for every signed input.
References #
This representation and the bounds below are an original implementation choice, not a formalization of a cited algorithm.
Twice the magnitude, with the low bit set exactly for negative integers.
Instances For
Interpret the low bit as a sign and the remaining bits as a magnitude.
Instances For
Different integers have different encoded words.
Every signed w-bit integer fits in one unsigned word with one extra bit.