ada中mod运算符的使用
谁能告诉我以下声明的用法,如下所示。我是 ada 语言的初学者。我尝试过互联网,但还不够清楚。
type Unsigned_4 is mod 2 ** 4;
for Unsigned_4'Size use 4;
Can anyone please tell me the usage of the following declarations shown below.I am a beginner in ada language.I had tried the internet but that was not clear enough.
type Unsigned_4 is mod 2 ** 4;
for Unsigned_4'Size use 4;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Unsigned_4
是一种“模块化类型”,取值 0、1、.. 14、15,并进行环绕。您只需要 4 位来实现该类型,因此可以将其指定为其大小(我认为这可能只是一个确认规范,因为编译器已经清楚地知道这一点;如果您希望将其装入 3 位并说
对于 Unsigned_4'Size 使用 3;
编译器会告诉你你错了)。大多数编译器希望将类型的值存储在至少一个字节中,以便有效访问。当您在打包记录(pragma Pack)中使用该类型时,最小大小就会发挥作用。
Unsigned_4
is a "modular type" taking the values 0, 1, .. 14, 15, and wrapping round.You only need 4 bits to implement the type, so it's OK to specify that as its Size (I think this may be simply a confirming spec, since the compiler clearly knows that already; if you were hoping to fit it into 3 bits and said
for Unsigned_4'Size use 3;
the compiler would tell you that you were wrong).Most compilers will want to store values of the type in at least a byte, for efficient access. The minimum size comes into its own when you use the type in a packed record (pragma Pack).
“is mod”是 Ada 表示这是一个 模块化类型。模块化类型的工作方式有点像 C 中的无符号类型:它们没有负值,一旦达到最大可表示值,如果添加 1,您将得到 0。
如果您尝试与 Ada 中的普通(非模)整数相同,您会得到
constraint_error
The "is mod" is Ada's way of saying that this is a modular type. Modular types work a bit like
unsigned
types in C: They don't have negative values, and once you reach the largest representable value, if you add one you will get 0.If you were to try the same with a normal (non-modular) integer in Ada, you'd get
constraint_error