In standard Ada, the shift and rotate functions are available only
for the predefined modular types in package Interfaces
. However, in
GNAT it is possible to define these functions for any integer
type (signed or modular), as in this example:
function Shift_Left (Value : T; Amount : Natural) return T with Import, Convention => Intrinsic;
The function name must be one of
Shift_Left, Shift_Right, Shift_Right_Arithmetic, Rotate_Left, or
Rotate_Right. T must be an integer type. T’Size must be
8, 16, 32 or 64 bits; if T is modular, the modulus
must be 2**8, 2**16, 2**32 or 2**64.
The result type must be the same as the type of Value
.
The shift amount must be Natural.
The formal parameter names can be anything.
A more convenient way of providing these shift operators is to use the Provide_Shift_Operators pragma, which provides the function declarations and corresponding pragma Import’s for all five shift functions. For signed types the semantics of these operators is to interpret the bitwise result of the corresponding operator for modular type. In particular, shifting a negative number may change its sign bit to positive.