哪种 C 数据类型可以表示 40 位二进制数? [英] Which C datatype can represent a 40-bit binary number?

查看:13
本文介绍了哪种 C 数据类型可以表示 40 位二进制数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要表示一个 40 位二进制数.应该使用哪种 C 数据类型来处理这个问题?

I need to represent a 40-bit binary number. Which C datatype should be used to handle this?

推荐答案

如果您使用的是符合 C99 或 C11 的编译器,请使用 int_least64_t 以获得最大兼容性.或者,如果你想要一个无符号类型,uint_least64_t.这些都在 <stdint.h>

If you're using a C99 or C11 compliant compiler, then use int_least64_t for maximum compatibility. Or, if you want an unsigned type, uint_least64_t. These are both defined in <stdint.h>

<stdint.h> 通常也定义 int64_t,但由于标准不要求它,因此可能不会在每个实现中都定义它.然而:

<stdint.h> usually also defines int64_t, but since it's not required by the standard, it may not be defined in every implementation. However:

  • int_least64_t - 至少 64 位,并且
  • int_fast64_t - 此实现中最快的大小,至少 64 位
  • int_least64_t - at least 64 bits, and
  • int_fast64_t - the fastest size in this implementation of at least 64 bits

两者都必须出现在 C99 和 C11 中(请参阅 C99 标准中的 § 7.18.1.2-3 和 C11 标准中的 § 7.20.1.2-3).

are both required to be present in C99 and C11 (See § 7.18.1.2-3 in the C99 standard, and § 7.20.1.2-3 in the C11 standard).

虽然 C99 指定 long long在特定的machine(§ 5.2.4.2.1),<stdint.h> 被设计为明确可移植的.

Although C99 specifies that long long is at least 64 bits on a particular machine (§ 5.2.4.2.1), the types in <stdint.h> are designed to be explicitly portable.

您可以在这里阅读更多关于不同平台上整数大小的信息.请注意,整数类型的大小是 long 数据类型的问题 - 在 64 位 Windows 上,它目前是 32 位,而在 64 位 Linux 上它是 64 位.出于这个原因,我相信您使用 <stdint.h>

You can read more about integer sizes on different platforms here. Note that the size of integer types are a problem with the long data type - on 64 bit Windows, it's currently 32 bits, whereas on 64 bit linux it's 64 bits. For this reason, I believe you're safest using the types from <stdint.h>

值得注意的是,有些人觉得long long更具可读性.就个人而言,我更喜欢 <stdint.h> 中的类型,因为它们允许你在使用它们时说出你的意思——我觉得这更易读.当然,可读性通常是一个品味问题——如果你正在使用现有的代码库,我会跟随他们所做的一切:)

It's worth noting that some feel that long long is more readable. Personally, I prefer the types from <stdint.h>, because they allow you to say what you mean when you use them - which I find more readable. Of course, readability is often a matter of taste - and if you're working with an existing codebase, I'd just follow whatever they do :)

如果你的编译器只支持 C89,那么 R.. 的解决方案 最多允许 53 位整数精度.

If your compiler only supports C89, then R..'s solution will allow you up to 53 bits of integer precision.

这篇关于哪种 C 数据类型可以表示 40 位二进制数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆