Ada Programming/Types/mod
From Wikibooks, the open-content textbooks collection
Contents |
[edit] Description
Unsigned integers in Ada have a value range from 0 to some positive number (not necessarily one less than a power of 2). They are defined using the mod keyword because they implement a wrap-around arithmetic.
mod Modulus
where 'First is 0 and 'Last is Modulus - 1.
Wrap-around arithmetic means that 'Last + 1 = 0 = 'First, and 'First - 1 = 'Last. Additionally to the normal arithmetic operators, bitwise and, or and xor are defined for the type.
The predefined package Interfaces (RM B.2 (Annotated)) presents unsigned integers based on powers of 2
type Unsigned_n is mod 2**n;
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:
type Byte is mod 256; subtype Half_Byte is Byte range 0 .. 127;
But beware: the Modulus of Half_Byte is still 256! Arithmetic with such a type is interesting to say the least.

