Advertisement

is there a difference between bool and BOOL?

Started by April 25, 2000 10:34 PM
14 comments, last by Possibility 24 years, 7 months ago
I was just wondering if there is a difference between bool and BOOL. Because in VC++, bool turns a bright blue color, where as BOOL does not, it just stays the normal black font color. I know they do the exact same thing and you can use them interchangeably, but is there any difference behind the scenes? Possibility
bool is a standard C++ keyword. BOOL was something invented by Microsoft to serve the same purpose.

Try to use bool if possible. Only use BOOL for compatibility with Win32.

(my byline from the Gamedev Collection series, which I co-edited) John Hattan has been working steadily in the casual game-space since the TRS-80 days and professionally since 1990. After seeing his small-format games turned down for what turned out to be Tandy's last PC release, he took them independent, eventually releasing them as several discount game-packs through a couple of publishers. The packs are actually still available on store-shelves, although you'll need a keen eye to find them nowadays. He continues to work in the casual game-space as an independent developer, largely working on games in Flash for his website, The Code Zone (www.thecodezone.com). His current scheme is to distribute his games virally on various web-portals and widget platforms. In addition, John writes weekly product reviews and blogs (over ten years old) for www.gamedev.net from his home office where he lives with his wife and daughter in their home in the woods near Lake Grapevine in Texas.

Advertisement
bool is 1 byte..

BOOL is 4 bytes - same as int on win32 platform
quote:
Try to use bool if possible. Only use BOOL for compatibility with Win32.


Why use bool, when it´s much slower on 32/64 bit cpu´s.
With BOOL you don´t have to think of the memory-alignment which should be multible by 8 ((memAdress % 8) == 0).

This will probably be more important in the furture, where the 64 bit alpha can´t handle odd memory-adresses.
This must be done in software!!!! Which is VERY SLOW...
(The 32 bit cpu can handle this exception in hardware, which is much faster, but you are still loosing a lot of speed!)

So I won´t advise the use of bool, unless you make sure to align your data!
what''s this blind obsession with performance anyway .

Pack 4 bool''s into a struct and POOF - no alignment problems.
Oh well.

I personally prefer a bit-bool myself, saving space.
BYTE bitBool can contain 8 boolean values, and it''s fun to program.


#pragma DWIM // Do What I Mean!
~ Mad Keith ~
**I use Software Mode**
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
quote: Original post by Anonymous Poster

This will probably be more important in the furture, where the 64 bit alpha can´t handle odd memory-adresses.


It''s hardly a big deal. If the bool is byte 3 of a 4-byte word, I -imagine- (I''m no assembly expert, so flame away) that at worst, the compiled code would just read in the whole word (a fast operation, as you say), mask off the unwanted bits with an AND, and shift it right to get the correct 8 bits. That doesn''t seem like a lot of operations to me. 99% of tests involving a bool could possibly be optimised further anyway, avoiding the shift or the mask or both. And let''s not forget caching issues - probably the best performance increase you can get is to fit your current ''working set'' into the cache. If you go around using 4x as much memory as needed, this is less likely to happen.
Advertisement
I agree with MadKeith. Why use 8 or 32 or even 64 bits to represent 1?


Lack

Christianity, Creation, metric, Dvorak, and BeOS for all!
Lack
Christianity, Creation, metric, Dvorak, and BeOS for all!
quote:
I agree with MadKeith. Why use 8 or 32 or even 64 bits to represent 1?

Because a single bit isn''t addressable. Inside a tight loop the masking operations necessary to address the individual bits can start to add up to a measurable performance hit. And remember the processor acts on byte or larger arguments, so in a sense by using bit bools you''re working against the processors (get a 32 bit value thunk it down to one bit, thunk it back up to 8 bits and repeat). A byte is at least addressable, so represents a reasonable compromise between space and speed in a computation bound process.

Of course, If you''re going to save it to disk or send it over the network, by all means, compress your bools to bit values.
Well the alignment on the alpha CPU is VERY important,
in fact you can read in "Advanced Windows programming" (now called: Programming applications for Microsoft Windows) by Jeffrey Richter that this error cost upto 100 times speed (10000 %)!!!

So this could be a very important aspect of Windows programming in the furture!
bool is a built-in type in C++ and BOOL isn''t. Before VC++5, they don''t support bool type, so MS invented themself a BOOL, which is simply
typedef int BOOL;
.... then they screw it up and make things more confused. Anyone bother to read the GetMessage() help? It returns a BOOL type, but with more than 2 different kind of values instead of true/false. Why don''t they call it a TRILEAN anyway? ;p

For other platform, I''m not sure about the memory alignment/size for a bool. I believe the compiler should know how to ''optimize'' the bool.

Stick with the bool. Use BOOL when checking returns from Windows API.
"after many years of singularity, i'm still searching on the event horizon"

This topic is closed to new replies.

Advertisement