开发者

Windows: How big is a BOOL?

How big (in bits) is a Windows BOOL data type?

Microsoft defines the BOOL data type as:

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 int data type?


Edit: In before K&R.


Edit 2: Something to think about

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

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 int is. But that's a C/C++ int, not the Integer data type in our pretend language.

So i need to either find, or create, a data-type that is the same size as an 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.


int is 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.

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).

Sizes of Fundamental Types

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


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

sizeof(int)*8


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 semantic point 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:

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:

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.

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


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.

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


They are both 32 bits big (4 bytes).

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


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

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


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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜