我们将字节中的第一个位称为位 1 还是 0?

发布于 2024-12-28 02:30:18 字数 185 浏览 3 评论 0原文

对于本示例,我创建一个整数。

int example = 0;

现在假设我想知道这个整数的第一位是什么。我知道它位于位位置 0。但是我会称之为位 1 还是位 0。我问的原因是因为我看过文档,其中整数的第一位标记为位 0,然后标记为位 1。我知道这是他们的错误,只是好奇我应该将其称为什么。

For this example, I create an integer.

int example = 0;

Now let's say I want to know what the first bit of this integer is. I know it would be at bit position 0. But would I call it bit 1 or 0. The reason I ask is because I have seen documentation where the first bit of an integer is labeled as bit 0 and then later labeled as bit 1. I know it's a mistake on their end, just curious as to what I should be referring to it as.

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

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

发布评论

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

评论(2

绝不放开 2025-01-04 02:30:18

大多数时候,最低位被称为位 0。

但是,这实际上取决于您询问它的上下文。我曾在两个不同的(互连的)系统上工作过,其中一个的文档称为它是位 1,另一个称为位 0。谈论令人困惑!重要的是,如果您记录某些内容,请始终对其进行限定。

通常,这称为“索引”。因此,如果最低位被称为“位零”,则该位域是“零索引”的。

就我个人而言,我总是将最低位称为零位。根据此约定,您可以移动 1 n 位,以打开第 n 位:

x = 1<<0;    00000001b  (bit 0 is on)
x = 1<<4;    00010000b  (bit 4 is on)

Most of the time, the lowest-order bit is called bit 0.

However, it really depends on the context you ask it in. I have worked on two different (interconnected) systems, when one's documentation called it bit 1, and the other's called it bit 0. Talk about confusing! The important thing is to always qualify something if you document it.

Typically, this is called "-indexed". So if the lowest-order bit is called "bit zero" then the bitfield is "zero-indexed."

Personally, I always refer to the lowest-order bit as bit zero. With this convention, you can shift a 1 n places, to turn on the nth bit:

x = 1<<0;    00000001b  (bit 0 is on)
x = 1<<4;    00010000b  (bit 4 is on)
小苏打饼 2025-01-04 02:30:18

如果简单地计算 2 的幂,2**0 就是 1。从 0 开始对位进行编号更有意义。

If you go simply by powers of two, 2**0 is 1. It makes more sense to number the bits starting at 0.

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