Bools reloaded

Oskar Linde olREM at OVEnada.kth.se
Sat Mar 4 01:15:31 PST 2006


Ivan Senji wrote:

> Oskar Linde wrote:
>> && and || controls the program flow.
> 
> If's and while's containing them control program flow (or I didn't get
> something right?)

They control program flow by themselves too:

my_if() && (my_then(),1) || my_else(); 

Is equivalent to:

if (my_if())
        my_then();
else
        my_else();


>> &,|,~,^ are real operators that also maps directly onto machine
>> instructions on most cpu architectures.
> 
> Sure they are, and I use them all the time on ints.
> 
>> Their meaning would be well defined
>> for bools.
> 
> Not really:
> bool a = //somehow 2
> bool b = //somehow 4
> a && b == true && true == true
> a & b == 0x0010 & 0x0100 == 0x0000 == false,
> 
> I mean they would work but only if true == always 1 and false == always 0.

We all seem to be talking about different kinds of bool :). 

[snip]

> In the paragraf you replyed to I was just trying to say that having &,|
> and ^ for bools only has meaning f they are strict logic bools and not
> numeric.

I see. I was talking about logical bools. Btw, the current bool (and C99
_Bool, C++ bool) is defined to be either 1 or 0. Without sidestepping the
type system there is no way to make a bool take any other value. This means
that both numeric operators and bitwise operator are well defined. A bool
can never somehow be 2 or somehow be 4. Assigning x to a bool makes an
implicit (x!=0). Another numeric boolean representation where the least
significant bit defines the truth state would also work with bit
operations.

The bool you seem to be talking about is one where any numeric value != 0
would represent true?

/Oskar



More information about the Digitalmars-d-announce mailing list