ada中mod运算符的使用

发布于 2024-08-30 09:34:08 字数 162 浏览 9 评论 0原文

谁能告诉我以下声明的用法,如下所示。我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫烠⑼条掵仅有一顆心 2024-09-06 09:34:08

Unsigned_4 是一种“模块化类型”,取值 0、1、.. 14、15,并进行环绕。

   U : Unsigned_4;
begin
   U := Unsigned_4'Last;      -- 15
   U := U + 1;                -- 0

您只需要 4 位来实现该类型,因此可以将其指定为其大小(我认为这可能只是一个确认规范,因为编译器已经清楚地知道这一点;如果您希望将其装入 3 位并说对于 Unsigned_4'Size 使用 3; 编译器会告诉你你错了)。

大多数编译器希望将类型的值存储在至少一个字节中,以便有效访问。当您在打包记录(pragma Pack)中使用该类型时,最小大小就会发挥作用。

Unsigned_4 is a "modular type" taking the values 0, 1, .. 14, 15, and wrapping round.

   U : Unsigned_4;
begin
   U := Unsigned_4'Last;      -- 15
   U := U + 1;                -- 0

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).

帅的被狗咬 2024-09-06 09:34:08

“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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文