Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 761
Ian Boyd Avatar asked Dec 03 '09 21:12

Ian Boyd


People also ask

What is the size of a bool?

bool The bool type takes one byte and stores a value of true (1) or false(0). The size of a bool is 1 true 1 1 1 false 0 0 0 Press any key to continue . . . int is the integer data type.

Why is a bool 4 bytes?

Because it's fast. A 32-bit processor typically works with 32-bit values. Working with smaller values involves longer instructions, or extra logic.

How many bytes is a Boolean?

Internally, a Boolean variable is a 2-byte value holding –1 (for TRUE) or 0 (for FALSE). Any type of data can be assigned to Boolean variables. When assigning, non-0 values are converted to TRUE , and 0 values are converted to FALSE.


1 Answers

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
like image 95
Reed Copsey Avatar answered Sep 30 '22 01:09

Reed Copsey