Bools reloaded
Ivan Senji
ivan.senji_REMOVE_ at _THIS__gmail.com
Fri Mar 3 12:39:41 PST 2006
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 are not algebraic operators.
They are logic operators. (Ok algebraic if we are talking about Boolean
algebra).
> &,|,~,^ 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.
> && and || requires branching and are therefore much less
> efficient (even though this can be optimized away in many cases).
>
> Also, not allowing ^ for bools would mean that code that reads a^b today
> would have to be rewritten as:
>
> (a && !b) || (!a && b).
>
Oops, forgot about that one, so ^ can stay :)
>
>>>Why should &,|,^,~ not be defined
>>>and allowed for bool? It would be problematic and inconsistent not
>>>having non-short-circuit operators for booleans.
>>
>>I learned from xs0's reply that Java uses &,| for non-short-circuit
>>evaluation when arguments are of type bool. As D doesn't have a bool in
>>the Java meaning I don't think making & anything but bitwise operator
>>makes sense.
>
>
> Huh? There have been regular discussions on the D newsgroups about bool
> since atleast 2001.
Tell me about it, I think it is an neverending topic for D. Maybe even
if Walter got bools 'right' some people would be complaining.
> Almost all threads about this have people arguing for
> the introduction of a logical (as opposed to the current numerical) bool
> type similar (not necessarily identical) to the Java boolean. Isn't this
> what we are discussing here too?
I think so.
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.
>>>>>(they mean very different things for integers). "&" normally means
>>>>>"bitwise and", but a pure bool doesn't have any bits.
>>>>>Is it legal to cast from a bool to some other type?
>>>>
>>>>
>>>>I'm not against casting but implicit conversions are bad.
>>>
>>>
>>>Do you mean that all implicit conversions are bad? Are the integer
>>>promotion rules bad?
>>
>>Ofcourse not. But with bools yes.
>
>
> I read your statement as meaning that all implicit conversions are bad. I
> was merely interested in why you thought so. :)
Well, I think I would be crazy to think that ;)
More information about the Digitalmars-d-announce
mailing list