Unsigned integers in Ada have a value range from 0 to some positive number (not necessarily 1 subtracted from some power of 2). They are defined using the mod keyword because they implement a wrap-around arithmetic.
for which also shift and rotate operations are defined. The values of n depend on compiler and target architecture.
You can use range to sub-range a modular type:
But beware: the Modulus of Half_Byte is still 256! Arithmetic with such a type is interesting to say the least.
type Unsigned is mod 2**5; -- modulus 32 X: Unsigned := 2#10110#; -- 22 not X = 2#01001# -- bit reversal: 9 ( = 31 - 22 ) as expected
The other operators work similarly.
Now take a modulus that is not a power of two. Naive expectations about the results may lead out of the value range. As an example take again the not operator (see the RM for the others):
The definition of not is therefore:
not X = Unsigned'Last – X -- here: 4 – 1 = 2#011#
Ada Reference ManualEdit