Windows:BOOL 有多大?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1843134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:32:11  来源:igfitidea点击:

Windows: How big is a BOOL?

cwindowstypesboolean

提问by Ian Boyd

How big (in bits) is a Windows BOOLdata type?

Windows BOOL数据类型有多大(以单位)?

Microsoft defines the BOOLdata typeas:

微软定义布尔数据类型为:

BOOL  Boolean variable (should be TRUE or FALSE).
      This type is declared in WinDef.h as follows:

      typedef int BOOL;

Which converts my question into:

这将我的问题转换为:

How big (in bits) is an intdata type?

int数据类型有多大(以位为单位)?



Edit: In before K&R.

编辑:在 K&R 之前。



Edit 2: Something to think about

编辑2:需要考虑的事情

Pretendwe're creating a typed programming language, and compiler. You have a type that represents something logically being Trueor False. If your compiler can also link to Windows DLLs, and you want to call an API that requires a BOOLdata type, what data type from your language would you pass/return?

假设我们正在创建一种类型化的编程语言和编译器。您有一个类型,它代表某种逻辑上是TrueFalse 的东西。如果您的编译器还可以链接到 Windows DLL,并且您想要调用需要BOOL数据类型的 API ,那么您将传递/返回您的语言中的哪种数据类型?

In order to interop with the Windows BOOL data type, you have to know how large a BOOL is. The question gets converted to how big an intis. But that's a C/C++ int, not the Integerdata type in our pretend language.

为了与 Windows BOOL 数据类型互操作,您必须知道 BOOL 有多大。问题被转换为int有多大。但这是一个 C/C++ int,而不是我们假装语言中的Integer数据类型。

So i need to either find, or create, a data-type that is the same size as an int.

所以我需要找到或创建一个与 int 大小相同的数据类型。

Note:In my original question i'm not creating a compiler. i'm calling Windows from a language that isn't C/C++, so i need to find a data type that is the same size as what Windows expects.

注意:在我最初的问题中,我没有创建编译器。我从不是 C/C++ 的语言调用 Windows,所以我需要找到一个与 Windows 期望的大小相同的数据类型。

回答by Reed Copsey

intis officially "an integral type that is larger than or equal to the size of type short int, and shorter than or equal to the size of type long." It can be any size, and is implementation specific.

int正式是“大于或等于类型 short int 的大小且小于或等于类型 long 的大小的整数类型”。它可以是任何大小,并且是特定于实现的。

It is 4 bytes (32 bits), on Microsoft's current compiler implementation (this is compiler specific, not platform specific). You can see this on the Fundamental Types (C++)page of MSDN (near the bottom).

它是 4 个字节(32 位),在 Microsoft 当前的编译器实现上(这是特定于编译器的,而不是特定于平台的)。您可以在MSDN的基本类型 (C++)页面上看到这一点(靠近底部)。

Sizes of Fundamental Types

Type                    Size
======================= =========
int, unsigned int       4 bytes

基本类型的大小

Type                    Size
======================= =========
int, unsigned int       4 bytes

回答by catwalk

it is platform-dependent, but easy to find out:

它依赖于平台,但很容易找到:

sizeof(int)*8

回答by Paul Hsieh

In terms of code, you can always work out the size in bits of any type via:

在代码方面,您始终可以通过以下方式计算出任何类型的位大小:

#include <windows.h>
#include <limits.h>

sizeof (BOOL) * CHAR_BIT

However, from a semanticpoint of view, the number of bits in a BOOL is supposed to be one. That is to say, all non-zero values of BOOL should be treated equally, including the value of TRUE. FALSE(which is 0) is the only other value that should have a distinguished meaning. To follow this rule strictly actually requires a bit of thought. For example, to cast a BOOL down to a char you have do the following:

但是,从语义的角度来看, BOOL 中的位数应该是1。也就是说,所有 BOOL 的非零值都应该一视同仁,包括TRUE的值。FALSE(即 0)是唯一应该具有特殊含义的其他值。严格遵守这条规则实际上需要一些思考。例如,要将 BOOL 转换为字符,您可以执行以下操作:

char a_CHAR_variable = (char) (0 != b_A_BOOL_variable);

(If you were to just cast directly, then values like (1 << 8) would be interpreted as FALSE instead of TRUE.) Or if you just want to avoid the multi-value issues altogether via:

(如果您只是直接强制转换,那么 (1 << 8) 之类的值将被解释为 FALSE 而不是 TRUE。)或者如果您只想通过以下方式完全避免多值问题:

char a_CHAR_variable = !!b_A_BOOL_variable;

If you are trying to use the various different values of BOOL for some other purpose, chances are what you are doing is either wrong or at the very least will lead to something unmaintainable.

如果您尝试将 BOOL 的各种不同值用于其他目的,那么您所做的很可能是错误的,或者至少会导致无法维护的事情。

Its because of these complications that the C++ language actually added a bonafide bool type.

由于这些复杂性,C++ 语言实际上添加了一个真正的 bool 类型。

回答by Nate C-K

It depends on the implementation. (Even on the same OS.) Use sizeof(int)to find the size of int on the implementation that you're currently using. You should never hard-code this into your C program.

这取决于实现。(即使在同一操作系统上。)用于sizeof(int)在您当前使用的实现上查找 int 的大小。你永远不应该把它硬编码到你的 C 程序中。

Better yet, use sizeof(BOOL)so you don't have to worry if MS ever changes their definition of BOOL.

更好的是,使用它,sizeof(BOOL)这样您就不必担心 MS 是否更改了他们对 BOOL 的定义。

回答by Alex Black

They are both 32 bits big (4 bytes).

它们都是 32 位大(4 个字节)。

In languages that have a native boolean type, booleans are usually 8 bits big (1 byte), not 1 bit as I once thought.

在具有原生布尔类型的语言中,布尔值通常为 8 位(1 字节),而不是我曾经认为的 1 位。

回答by John

It's as big as sizeof(int) says it is?

它和 sizeof(int) 说的一样大?

(That's in bytes so multiply by 8.)

(以字节为单位,因此乘以 8。)

回答by Moby

On windows 7 x64 and C# 2010 sizeof(bool) gives an answer of 1 , whereas sizeof(int) gives an answer of 4.

在 windows 7 x64 和 C# 2010 sizeof(bool) 给出的答案是 1 ,而 sizeof(int) 给出的答案是 4。

Therefore the answer to "how big in bits is a bool" is 8, and it is not the same as an int.

因此,“bool 的位数有多大”的答案是 8,它与 int 不同。