数据类型中的位数

发布于 2024-08-18 03:53:25 字数 430 浏览 9 评论 0原文

我有两个任务要分配,一个任务返回任何机器上 int 类型的位数。我想我会像这样编写我的函数:

int CountIntBitsF() {
    int x = sizeof(int) / 8;
    return x;
}

看起来正确吗?

第二部分是用宏返回任意数据类型的任意位数,宏可以取自limits.h。我在我的机器上查找了limits.h,还查找了http://www .opengroup.org/onlinepubs/007908799/xsh/limits.h.html,但我认为我并不真正理解其中任何一个如何返回任何数据类型的位数。有什么想法吗?谢谢。

I have two tasks for an assignment, one return the number of bits in type int on any machine. I thought I would write my function like so:

int CountIntBitsF() {
    int x = sizeof(int) / 8;
    return x;
}

Does that look right?

The second part is to return the number of any bits of any data type with a macro, and the macro can be taken from limits.h. I looked up limits.h on my machine, and also http://www.opengroup.org/onlinepubs/007908799/xsh/limits.h.html, but I don't think I really understand how any of those would return the number of bits in any data type. Any thoughts? Thanks.

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

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

发布评论

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

评论(7

寒江雪… 2024-08-25 03:53:25

存储的基本单位是字符。它并不总是 8 位宽。 CHAR_BIT 在limits.h 中定义,具有字符中的位数。

The fundamental unit of storage is a char. It is not always 8 bits wide. CHAR_BIT is defined in limits.h and has the number of bits in a char.

初雪 2024-08-25 03:53:25

它是 *,而不是 /

至于第二部分,请参阅“数值限制”部分。

It's *, not /.

As for the second part, see the "Numerical Limits" section.

勿挽旧人 2024-08-25 03:53:25

如果您想要用于在内存中存储 int 的位数,请使用 Justin 的答案,sizeof(int)*CHAR_BIT。如果您想知道该值中使用的位数,请使用 slebetman 的答案。

尽管要获取 INT 中的位,您可能应该使用 INT_MAX 而不是 UINT_MAX。我不记得 C99 是否确实保证 intunsigned int 具有相同的宽度,或者只是保证它们具有相同的存储大小。我怀疑只有后者,因为在 6.2.6.2 中,我们有“如果有符号类型中有 M 个值位,无符号类型中有 N 个值位,则 M <= N”,而不是“M = N 或 M = N-1” ”。

实际上,在我使用过的任何实现中,整数类型都没有填充位,因此您很可能会得到相同的答案,符号位为 +/- 1。

If you want the number of bits used to store an int in memory, use Justin's answer, sizeof(int)*CHAR_BIT. If you want to know the number of bits used in the value, use slebetman's answer.

Although to get the bits in an INT, you should probably use INT_MAX rather than UINT_MAX. I can't remember whether C99 actually guarantees that int and unsigned int are the same width, or just that they're the same storage size. I suspect only the latter, since in 6.2.6.2 we have "if there are M value bits in the signed type and N in the unsigned type, then M <= N", not "M = N or M = N-1".

In practice, integral types don't have padding bits in any implementation I've used, so you most likely get the same answer for all, +/- 1 for the sign bit.

青丝拂面 2024-08-25 03:53:25

limits.h 中,UINT_MAX 是 unsigned int 类型对象的最大值。这意味着它是一个所有位都设置为 1 的 int。因此,计算 int 中的位数:

#include <limits.h>

int intBits () {
    int x = INT_MAX;
    int count = 2; /* start from 1 + 1 because we assume
                    * that sign uses a single bit, which
                    * is a fairly reasonable assumption
                    */

    /* Keep shifting bits to the right until none is left.
     * We use divide instead of >> here since I personally
     * know some compilers which does not shift in zero as
     * the topmost bit
     */
    while (x = x/2) count++;

    return count;
}

In limits.h, UINT_MAX is the maximum value for an object of type unsigned int. Which means it is an int with all bits set to 1. So, counting the number of bits in an int:

#include <limits.h>

int intBits () {
    int x = INT_MAX;
    int count = 2; /* start from 1 + 1 because we assume
                    * that sign uses a single bit, which
                    * is a fairly reasonable assumption
                    */

    /* Keep shifting bits to the right until none is left.
     * We use divide instead of >> here since I personally
     * know some compilers which does not shift in zero as
     * the topmost bit
     */
    while (x = x/2) count++;

    return count;
}
漫漫岁月 2024-08-25 03:53:25

使用 g++ -O2,此函数计算为内联常量:

#include <climits>
#include <stddef.h>
#include <stdint.h>
#include <cstdio>

template <typename T>
size_t num_bits()
{
    return sizeof (T) * (CHAR_BIT);
}

int main()
{
    printf("uint8_t : %d\n", num_bits<uint8_t>());
    printf("size_t : %d\n", num_bits<size_t>());
    printf("long long : %d\n", num_bits<long long>());
    printf("void* : %d\n", num_bits<void*>());
    printf("bool : %d\n", num_bits<bool>());
    printf("float : %d\n", num_bits<float>());
    printf("double : %d\n", num_bits<double>());
    printf("long double : %d\n", num_bits<long double>());

    return 0;
}

输出:

uint8_t : 8
size_t : 32
long long : 64
void* : 32
bool : 8
float : 32
double : 64
long double : 96

生成的 X86 32 位汇编:

---SNIP---

movl    $32, 8(%esp)      <--- const $32
movl    $.LC1, 4(%esp)
movl    $1, (%esp)
call    __printf_chk
movl    $64, 8(%esp)      <--- const $64
movl    $.LC2, 4(%esp)
movl    $1, (%esp)
call    __printf_chk

---SNIP---

With g++ -O2 this function evaluates to an inline constant:

#include <climits>
#include <stddef.h>
#include <stdint.h>
#include <cstdio>

template <typename T>
size_t num_bits()
{
    return sizeof (T) * (CHAR_BIT);
}

int main()
{
    printf("uint8_t : %d\n", num_bits<uint8_t>());
    printf("size_t : %d\n", num_bits<size_t>());
    printf("long long : %d\n", num_bits<long long>());
    printf("void* : %d\n", num_bits<void*>());
    printf("bool : %d\n", num_bits<bool>());
    printf("float : %d\n", num_bits<float>());
    printf("double : %d\n", num_bits<double>());
    printf("long double : %d\n", num_bits<long double>());

    return 0;
}

outputs:

uint8_t : 8
size_t : 32
long long : 64
void* : 32
bool : 8
float : 32
double : 64
long double : 96

Generated X86 32-bit assember:

---SNIP---

movl    $32, 8(%esp)      <--- const $32
movl    $.LC1, 4(%esp)
movl    $1, (%esp)
call    __printf_chk
movl    $64, 8(%esp)      <--- const $64
movl    $.LC2, 4(%esp)
movl    $1, (%esp)
call    __printf_chk

---SNIP---

撩起发的微风 2024-08-25 03:53:25

您确定需要位数,而不是字节数吗?在 C 中,对于给定类型 T,您可以使用 sizeof 运算符找到它所占用的字节数。一个字节中的位数为 CHAR_BIT,通常为 8,但也可以不同。

因此,给定类型 T,类型 T 的对象中的位数为:

#include <limits.h>
size_t nbits = sizeof(T) * CHAR_BIT

请注意,除了 unsigned char 类型外,所有上述 nbits 位的可能组合可能不代表 T 类型的有效值。

对于第二部分,请注意,您可以将 sizeof 运算符应用于对象和类型。换句话说,给定一个类型 T 和一个该类型的对象 x

T x;

您可以通过 sizeof(T) 找到 T 的大小,以及 x 的大小乘以 sizeof x。如果 sizeof 用于对象,则括号是可选的。

根据上述信息,您应该能够回答第二个问题。如果仍有问题,请再次询问。

Are you sure you want number of bits, not number of bytes? In C, for a given type T, you can find the number of bytes it takes by using the sizeof operator. The number of bits in a byte is CHAR_BIT, which usually is 8, but can be different.

So, given a type T, the number of bits in an object of type T is:

#include <limits.h>
size_t nbits = sizeof(T) * CHAR_BIT

Note that, except for unsigned char type, all possible combinations of nbits bits above may not represent a valid value of type T.

For the second part, note that you can apply sizeof operator to an object as well as a type. In other words, given a type T and an object x of such type:

T x;

You can find the size of T by sizeof(T), and the size of x by sizeof x. The parentheses are optional if sizeof is used for an object.

Given the information above, you should be able to answer your second question. Ask again if you still have issues.

冰火雁神 2024-08-25 03:53:25

您的公式不正确:您不应将 sizeof(int) 除以 8,而是应乘以 int 中的字节数 (sizeof(int)) 乘以字节中的位数,它实际上在 中定义为宏 CHAR_BIT 的值。

这是更正后的函数:

#include <limits.h>

int CountIntBitsF(void) {
    return sizeof(int) * CHAR_BIT;
}

Your formula is incorrect: instead of dividing sizeof(int) by 8, you should multiply the number of bytes in an int (sizeof(int)) by the number of bits in a byte, which is indeed defined in <limits.h> as the value of macro CHAR_BIT.

Here is the corrected function:

#include <limits.h>

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