Author: Tony Werten
Date: 09:30:09 11/19/02
Go up one level in this thread
On November 19, 2002 at 08:58:16, Gerd Isenberg wrote: ><snip> > >>>Btw. the build in C++ type "bool" is often implemented with byte size. >>>Due to performace reasons, it might be advantageous to use an own typedef like >> >>Yes, that's it, it's a performance warning. > >solvable by including an != 0 without any code size effects... > >int i = 1234; >bool b = i; // the value range of bool is [0,1] resp [false,true] > // so the (ms)compiler warns, but implicitly assignes (i != 0) > > >> >>>typedef int BOOL; >>> >>>#ifndef TRUE >>>#define FALSE 0 >>>#define TRUE 1 >>>#endif >> >>I don't think I get it, you suggest to cast to int instead, what is the rest >>for? > >Confusion :) > >You find the BOOL typedef it a lot in ms-header files, maybe also for historical >reasons, because in older days there was no bool type in C. > >But even today, "bool" may have some performance problems due to byte-size. > >Look in MFC-functions you find mostly BOOL instead of bool, because sizeof(BOOL) >is 4, which is the natural register size of x86 processors. Another point is >pushing bool/char parameters may result in some misalignment on the stack. > >The rest, i guess you mean the defines? > >MS simply use the old fashioned preprocessor to define TRUE,FALSE literals, if >not already defined. > >Of course this BOOL type, behaves exactly like an int. > > >> >>Anyway, I hardly ever do cast to bool, it's working fine without it so it's >>probably just wasted clocks? >> > > >It's a good idea to assign only other boolean expressions to variables of type >bool incuding the literals "true" or "false". If you wan't to assign other types >to bool you should always use explicite relational operators, most likely != 0. > >Assigning bool to int is another thing, because bool is a kind of pragmatical >subset of int, due to "true" maps to "one" and "false" to "zero". >Therefore expressions like that are possible: > >int a,b,c; >... >bool bExpression = (b>c); >int nTrueConditions = (a>b) + (a>c) + bExpression; > >with a value range of [0...3] > >In strict and for didactical purposes more cleaner languages like Java or Pascal >you can't simply do such performance relevant "dirty" tricks like in the >"pragmatic" C-language. Actually, since in Pascal a bool is garanteed to be 0 or 1 there are some nice trics. So you can replace: if (a>5) then inc(b) with: inc(b,a>5) ( Could be inc(b,integer(a>5)), I'm not sure) Tony > >Gerd
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.