lkml.org 
[lkml]   [2002]   [Jan]   [24]   [last100]   RSS Feed
Views: [more markup]   [less markup]   [headers]   [forward]  
 
Messages in this thread
/
From"H. Peter Anvin" <>
SubjectRe: RFC: booleans and the kernel
Date24 Jan 2002 14:21:28 -0800
Followup to:  <Pine.LNX.4.44.0201241545120.2839-100000@waste.org>
By author:    Oliver Xymoron <oxymoron@waste.org>
In newsgroup: linux.dev.kernel
> > It doesn't fix  "if ( x = true)". If would
> > just make it more legit to use "if (x)".
> 
> It's been legit and idiomatic since day 1, if not sooner.
> 

The main reasons for bool is:

a) The ability to save space.  No need to waste a 32- or 64-bit word
   to hold a single bit.  If you're on an architecture that has flags
   or predicates you may be able to carry a boolean in such a value
   instead of in a full register.

b) Compatibility with other languages, including but not limited to
   C++ (there is a standard under development for inter-language linking,
   incidentally.)  C++, of course, needs bool for overloading reasons.

c) The ability to cast to bool and get an unambiguous true or false:

     b = (bool)a;

   This replaces the idiomatic but occationally confusing

     b = !!a;

d) Similarly, you can avoid doing booleanization multiple times:

   /* Highly artificial example */
   int foo(bool a)
   {
       return a ? 55 : 47;
   }
   ... could be implemented by the compiler as 47 + (a << 3), or
   depending on your ABI convention, perhaps a caller calling
   foo(x < 4) could be implemented as foo(x-4) without needing to
   convert it into an integer of exactly 1 and 0.

   Given the way C currently does it, you pretty much have do
   booleanize both in the caller and the callee to be on the safe
   side.

	-hpa


-- 
<hpa@transmeta.com> at work, <hpa@zytor.com> in private!
"Unix gives you enough rope to shoot yourself in the foot."
http://www.zytor.com/~hpa/puzzle.txt	<amsp@zytor.com>
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

\
 
 \ /
  Last update: 2005-03-22 11:15    [from the cache]
©2003-2008