C语言中如何判断最小信息单元的大小?

发布于 2024-10-27 04:01:37 字数 367 浏览 3 评论 0原文

在 C 中,有 sizeof 运算符来确定给定数据类型或对象的字节大小。

同样, 中的 CHAR_BIT 被定义为反映字节中的位数。

现在这可能有点假设,但我如何知道最小信息单元可以存储的不同值的数量,即主机环境是否提供位、trits、nats 或其他什么。

回答

显然,C 标准假设主机环境是按位操作的。需要这样的位能够存储至少两个值。

从这个问题中提出的值得注意的建议

三进制机器的最小信息单元的名称:TIT
四元机最小信息单元名称:a QUIT

In C, there's the sizeof operator to determine the byte-size of a given data type or object.

Likewise, there's CHAR_BIT from <limits.h> which is defined to reflect the number of bits in a byte.

Now this might be slightly hypothetical, but how do I tell the number of different values that the smallest unit of information can store, i.e. whether the host environment provides bits, trits, nats or whatever.

Answer

Apparently, the C standard assumes that the host environment operates on bits. Such a bit is required to be able to store at least two values.

Notable proposals that arose from this question

Name of the smallest unit of information of a ternary machine: a TIT
Name of the smallest unit of information of a quaternary machine: a QUIT

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

何处潇湘 2024-11-03 04:01:37

我认为根据定义,一个位是一个二进制数字,它必须是零或一,所以答案总是二(该位是 0 或 1)。

编辑:在回答您的新问题时,我认为没有标准的方法可以做到这一点。 C ISO 规范(N1124,§3.5/1)将位定义为

执行环境中的数据存储单元,足够大以容纳可以容纳两个值之一的对象。

由于 C 规范试图最大化该语言的可移植性,因此它没有指定超出这一点的位。这意味着从 C 内部,您无法了解更多有关位大小的信息。

I think by definition a bit is a binary digit which must be zero or one, so the answer is always two (either the bit is 0 or 1).

EDIT: in response to your new question, I believe that there is no standard way to do this. The C ISO spec (N1124, §3.5/1) defines a bit as

A unit of data storage in the execution environment large enough to hold an object that may hold one of two values.

Since the C spec tries to maximize the portability of the language, it doesn't specify what a bit is beyond this point. This means that from within C, you cannot tell any more than this about the size of a bit.

情话难免假 2024-11-03 04:01:37

BIT 一词是 B-inary dig-IT 的缩写,因此根据定义,它恰好有两种可能的状态。不存在歧义或实现定义的行为,只有数学确定性。

The term BIT is a contraction of B-inary dig-IT, so by definition it has exactly two possible states. There is no ambiguity or implementation defined behaviour, just mathematical certainty.

像极了他 2024-11-03 04:01:37

您可以声明一个结构来保存单个位:

typedef struct _bit_t {
    int bit: 1;
} bit_t;

嗯,sizeof(bit_t) 可能会因为对齐而得到 1 或 4,我不确定。

一般来说,字节应该是最小的整数类型。为此目的,您应该始终使用字节,以使您的程序可移植。如果你根本不关心可移植性,例如,你正在编写8051或PIC程序,那么你可以只使用bit类型,这与byte无关。

要声明一个字节,您可以安全地将其声明为 unsigned char,目前,我不知道有哪个 C 编译器的 char 不是 8 位。 (有什么例外吗?我想听听。)

You can declare a struct to hold a single bit:

typedef struct _bit_t {
    int bit: 1;
} bit_t;

Well, sizeof(bit_t) may get 1 or 4 because of alignment, I'm not sure.

Generally, a byte should be the smallest integer type. You should use byte always for that purpose, to make your program portable. If you don't care about portability at all, e.g., you are writing 8051 or PIC programs, then you can just use the bit type, it's nothing to do with byte.

To declare a byte, you can safely declare it as unsigned char, currently, I don't know any C compiler whose char isn't 8-bit. (Any exception? I'd like to hear about it.)

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