Ada 图像属性显示无符号类型的负值

Ada image attribute displaying a negative value for an unsigned type

给定,

type X is new Integer range 0 .. (2 ** 31) - 1 ;
…
Y : X;

我正在使用 Ada 属性 X'Image(Y) 来显示无符号类型的值,但它显示的是有符号值。是这个属性的限制还是有问题?

声明

type x is new Integer range 0 .. (2**31) - 1;

定义派生类型。

根据 Ada 参考手册第 3.4 节

A derived_type_definition defines a derived type (and its first subtype) whose characteristics are derived from those of a parent type, and possibly from progenitor types.

A class of types is a set of types that is closed under derivation; that is, if the parent or a progenitor type of a derived type belongs to a class, then so does the derived type. By saying that a particular group of types forms a class, we are saying that all derivatives of a type in the set inherit the characteristics that define that set. The more general term category of types is used for a set of types whose defining characteristics are not necessarily inherited by derivatives; for example, limited, abstract, and interface are all categories of types, but not classes of types.

因此,您定义的不是无符号类型。是有符号类型,最小值为0,但仍然保留了继承自Integer的符号位。

定义无符号整数类型的正确方法是

type X is range 0 .. (2**31) - 1;

如果你想要一个完整的 32 位无符号整数,你可以定义

type X is range 0 .. (2**32) - 1;