views:

533

answers:

6

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.

+2  A: 

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.

Alex Black
They are 32bits on Microsoft's implementation, but not by standard.
Reed Copsey
Is there another company that produces a product called Windows that I haven't heard of? ;)
Alex Black
@Alex: They're 32 bits on Microsoft's CURRENT platforms, but even MSDN says that "int" is not 32bits by definition. I quoted that (and got voted down for it).
Reed Copsey
Wrong. C++ has a native boolean type, and BOOL is still normally 4 bytes there (but as Reed noted, that's not guaranteed).
Peter
@Peter: what is wrong? You're saying most languages that have a native boolean type use 4 bytes for them?
Alex Black
@Reed: He simply asked how big is int and BOOL. They answer is they are 32bits. Try it. I see your point that they are not 32bits on say a 16bit windows platform, and who knows what they will be on a 128bit Windows platform, but that doesn't change the answer that he's looking for :)
Alex Black
Alex: On my machine, sizeof(BOOL) returns 4 in C++, which does have a native boolean type. So yes, it still uses 4 bytes for them. sizeof(bool) returns 1, but bool and BOOL are quite different animals.
Peter
@Peter: by "native boolean type" I meant bool. BOOL is obviously not a native boolean type, its just a typedef to INT.
Alex Black
Oh I see... sorry, I read it as "BOOL and int are both 32 bits. In languages with a native boolean type, BOOL and int are usually 8 bits". The repeated "they" was a little confusing IMO...
Peter
@Peter: ah, sorry for the confusion.
Alex Black
+22  A: 

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
Reed Copsey
Why the -1? I'm quoting strait from MSDN here, which is the premier refernece source for Windows. int on Microsoft's platform, by their own words, is not guaranteed to be 32 bits on Windows, but Visual C++ specifics it as 32 bits.
Reed Copsey
Thank you. i never found Microsoft's declaration of sizes.
Ian Boyd
I voted -1 because I didn't think "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." was helpful.
Alex Black
He quoted the answer as given by Microsoft. Just because you don't like or can't understand the answer doesn't mean it's not the answer. Voted up because you voted down.
rotard
@Alex Black: just because you don't like the official definition doesn't mean it's wrong. and that isn't MS specific, that's part of C++.
rmeador
@rotard: thanks for the insult that I don't understand it.
Alex Black
@rmeador: I didn't say it was wrong, nor did I say I didn't like it. I just said I don't think it is helpful given the question at hand.
Alex Black
@Alex Black: I disagree, fairly strongly. I feel that understanding that, although BOOL is 32 bits for now, int (and hence BOOL) != 32 bits officially means this isn't something you can rely on never changing. It's a critical piece of information for understanding the data type, and how it should be used.
Reed Copsey
@Reed: I do see your point of view. My viewpoint is that its not going to change anytime soon so knowing it might change will have no impact on someone writing code using int. I am sure others will disagree, thats fine.
Alex Black
Well, that's why I included the current size, too - not just the fact that it's platform/implementation specific.
Reed Copsey
+7  A: 

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

sizeof(int)*8
catwalk
This is not true for an "int" in Windows. If you need to pass an int to a function, you better pass the number of bits they're expecting.
Ian Boyd
Windows primary platform is x86 : since when could you address at a bit level on x86?
Pod
@Ian: what isn't true? Is the size of an int on windows not sizeof(int)? Or is the size of int on windows not platform (windows) dependent?
Alex Black
The size of an int is not platform dependant. It is 32-bits on 32-bit Windows and on 64-bit Windows.
Ian Boyd
@Ian, but this is what I think IS called platform-dependency (such as 32bit vs 64 bit)
catwalk
@Ian: are you sure? what was it on 16bit windows? :) What will it be on 128bit windows?
Alex Black
@Alex: Of course you are right about 16 bit *EXE*s (which includes old Windows and old-DOS). However, the *de facto* standard for applications moving forward is for int to be 32 bits. The reason is that too much functioning software has been written with that assumption. Very few surviving 64 bit machines even bothered to make their int defined as 64 bits (though indeed, some did).The really right answer, however, is to use int32_t or DWORD as being 32 bits, and to always write your code so that it doesn't matter what the size of int is.
Paul Hsieh
@Paul: its funny, I'm having two discussions on this page, one where I am taking the viewpoint that int will be 4 bytes for long enough that you can think of it as forever, and one where I am taking the side that it didn't used to be 4 bytes and it might not be 4 bytes in the future :)
Alex Black
+2  A: 

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

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

John at CashCommons
+2  A: 

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.

Nate C-K
The size of an int can never change on Windows, without breaking all API calls.
Ian Boyd
You still shouldn't hard-code the size into your program, regardless.
Nate C-K
+4  A: 

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.

Paul Hsieh