August 01, 2004
On Sun, 01 Aug 2004 13:28:06 -0400, parabolis wrote:

> teqDruid wrote:
> 
>> On Sun, 01 Aug 2004 07:47:23 -0700, Andy Friesen wrote:
>> 
>> 
>>>Ola Frid <olafrid atyay dtek.chalmers otday se> wrote:
>>>
>>>>Any chance of adding a boolean exclusive or? Like ^^?
>>>>It's a thing that too many programming languages lack, in my opinion.
>>>
>>>Fewer than you think!
>>>
>>>We usually call it !=
>>>
>>>:)
>>>
>>>  -- andy
>> 
>> 
>> But that only works if you're actually comparing booleans, so something like:
>> 
>> a.opEquals(b) != c.opEquals(d)
>> isn't necessarily the same as
>> (a.opEquals(b) == true) != (c.opEquals(d) == true) // <- amended
>> whereas since ^^ is a boolean comparison,
>> a.opEquals(b) ^^ c.opEquals(d)
>> should compare the same.
>> 
> 
> Only if you create a class and overload the opEquals operator and return a byte/ubyte/int/uint/long/ulong instead of type bit. Of course you can also overload the opEquals with return type Object...
> 
> Sadlly the compiler does not seem to be consistent about not being able to convert an int to a bit.

True... But in fact Object.opEquals returns int... But my point remains the same, when comparing ints, != is not ^^.
August 01, 2004
In article <cej0hj$258c$1@digitaldaemon.com>, Arcane Jill says...
>
>In article <cein8f$21i8$1@digitaldaemon.com>, Ola Frid <olafrid atyay dtek.chalmers otday se> says...
>>
>>Any chance of adding a boolean exclusive or? Like ^^?
>>It's a thing that too many programming languages lack, in my opinion.
>>
>>/ Ola Frid
>
>In D, as has been much discussed in the past, a boolean value is just a int (bah!), with the type "bool" aliased to "bit".
>
>Therefore, the operator ^ will do what you want.
>
># bool b = (x == y) ^ (z > 0);
>
>Arcane Jill
>
>
>

But, for example, 2 ^ 1 evaluates to 3, while 2 ^^ 1 would evaluate to 0.

/ Ola Frid


August 01, 2004
teqDruid wrote:

> On Sun, 01 Aug 2004 13:28:06 -0400, parabolis wrote:
> 
> 
>>teqDruid wrote:
>>
>>
>>>On Sun, 01 Aug 2004 07:47:23 -0700, Andy Friesen wrote:
>>>
>>>
>>>
>>>>Ola Frid <olafrid atyay dtek.chalmers otday se> wrote:
>>>>
>>>>
>>>>>Any chance of adding a boolean exclusive or? Like ^^?
>>>>>It's a thing that too many programming languages lack, in my opinion.
>>>>
>>>>Fewer than you think!
>>>>
>>>>We usually call it !=
>>>>
>>>>:)
>>>>
>>>> -- andy
>>>
>>>
>>>But that only works if you're actually comparing booleans, so something
>>>like:
>>>
>>>a.opEquals(b) != c.opEquals(d)
>>>isn't necessarily the same as
>>>(a.opEquals(b) == true) != (c.opEquals(d) == true) // <- amended
>>>whereas since ^^ is a boolean comparison,
>>>a.opEquals(b) ^^ c.opEquals(d)
>>>should compare the same.
>>>
>>
>>Only if you create a class and overload the opEquals operator and return a byte/ubyte/int/uint/long/ulong instead of type bit.
>>Of course you can also overload the opEquals with return type Object...
>>
>>Sadlly the compiler does not seem to be consistent about not being able to convert an int to a bit.
> 
> 
> True... But in fact Object.opEquals returns int... But my point remains
> the same, when comparing ints, != is not ^^.

No but ^ is ^^ when comparing ints ;)
August 01, 2004
Ola Frid wrote:
> In article <cej0hj$258c$1@digitaldaemon.com>, Arcane Jill says...
> 
>>In article <cein8f$21i8$1@digitaldaemon.com>, Ola Frid <olafrid atyay
>>dtek.chalmers otday se> says...
>>
>>>Any chance of adding a boolean exclusive or? Like ^^?
>>>It's a thing that too many programming languages lack, in my opinion.
>>>
>>>/ Ola Frid
>>
>>In D, as has been much discussed in the past, a boolean value is just a int
>>(bah!), with the type "bool" aliased to "bit".
>>
>>Therefore, the operator ^ will do what you want.
>>
>># bool b = (x == y) ^ (z > 0);
>>
>>Arcane Jill
>>
>>
>>
> 
> 
> But, for example, 2 ^ 1 evaluates to 3, while 2 ^^ 1 would evaluate to 0.
> 
> / Ola Frid
> 
> 

But (cast(bit)lhs) ^ (cast(bit)rhs) == lhs ^^ rhs. That suggests that if xor is not working the way you expect it to then you have caused DMD to implictly convert promote a bit to an byte/int/other. You should downcast any int that is not a bit.

================================================================
    printf( "%d\n", (cast(bit)2) ^ (cast(bit)1) );
Output: 0
================================================================
August 01, 2004
In article <ceja2k$28v5$1@digitaldaemon.com>, parabolis says...
>
>Ola Frid wrote:
>> In article <cej0hj$258c$1@digitaldaemon.com>, Arcane Jill says...
>> 
>>>In article <cein8f$21i8$1@digitaldaemon.com>, Ola Frid <olafrid atyay dtek.chalmers otday se> says...
>>>
>>>>Any chance of adding a boolean exclusive or? Like ^^?
>>>>It's a thing that too many programming languages lack, in my opinion.
>>>>
>>>>/ Ola Frid
>>>
>>>In D, as has been much discussed in the past, a boolean value is just a int (bah!), with the type "bool" aliased to "bit".
>>>
>>>Therefore, the operator ^ will do what you want.
>>>
>>># bool b = (x == y) ^ (z > 0);
>>>
>>>Arcane Jill
>>>
>>>
>>>
>> 
>> 
>> But, for example, 2 ^ 1 evaluates to 3, while 2 ^^ 1 would evaluate to 0.
>> 
>> / Ola Frid
>> 
>> 
>
>But (cast(bit)lhs) ^ (cast(bit)rhs) == lhs ^^ rhs. That suggests that if xor is not working the way you expect it to then you have caused DMD to implictly convert promote a bit to an byte/int/other. You should downcast any int that is not a bit.
>
>================================================================
>     printf( "%d\n", (cast(bit)2) ^ (cast(bit)1) );
>Output: 0 ================================================================

Yes, but the point being is that it would be very nice not to have to do that.

/ Ola Frid
August 01, 2004
In article <cej9bo$28m1$1@digitaldaemon.com>, Ola Frid says...

>>Therefore, the operator ^ will do what you want.
>
>But, for example, 2 ^ 1 evaluates to 3, while 2 ^^ 1 would evaluate to 0.

I can't argue with that. When you said you wanted a boolean xor I assumed you meant that the inputs would be boolean (which in D terms means 1 or 0). Okay, so ^ doesn't suit your purpose, but != has also been suggested, and (2 != 1) evaluates to 0, as you require.

To clarify something I said earlier, bool is aliased to bit, and a bit is implemented by DMD as a byte, constrained to values 0 and 1. (There were some bugs a while back whereby it was possible to get other values in a bit, but hopefully they're fixed now).

So != should do everything you want.

The expressions (a.opEquals(b))and (a.opEquals(b) == true) both evaluate to
(cast(bit)1), so your other examples should work too.

Jill


August 01, 2004
In article <cejb5p$29ln$1@digitaldaemon.com>, Arcane Jill says...
>
>The expressions (a.opEquals(b))and (a.opEquals(b) == true) both evaluate to
>(cast(bit)1), so your other examples should work too.

Actually, no they don't. They evaluate to 1, as an int. (As has been noted
before, opEquals() returns int, not bool or bit).

Anyway, != still does the job.

Jill


August 01, 2004
In article <cejb5p$29ln$1@digitaldaemon.com>, Arcane Jill says...
>
>In article <cej9bo$28m1$1@digitaldaemon.com>, Ola Frid says...
>
>>>Therefore, the operator ^ will do what you want.
>>
>>But, for example, 2 ^ 1 evaluates to 3, while 2 ^^ 1 would evaluate to 0.
>
>I can't argue with that. When you said you wanted a boolean xor I assumed you meant that the inputs would be boolean (which in D terms means 1 or 0). Okay, so ^ doesn't suit your purpose, but != has also been suggested, and (2 != 1) evaluates to 0, as you require.
>
>To clarify something I said earlier, bool is aliased to bit, and a bit is implemented by DMD as a byte, constrained to values 0 and 1. (There were some bugs a while back whereby it was possible to get other values in a bit, but hopefully they're fixed now).
>
>So != should do everything you want.
>
>The expressions (a.opEquals(b))and (a.opEquals(b) == true) both evaluate to
>(cast(bit)1), so your other examples should work too.
>
>Jill
>

You're right, of course, but the thing I actually want is a consistent way to do an xor like there is a way of doing an && or ||, no matter of the original type, be it an int, an object reference or a bit. ^^ would, like && or ||, convert it's operands to a bit. It's a thing that I think would be nice to have, even thouhg it is, as you and others have pointed out, not needed.

/ Ola Frid
August 01, 2004
teqDruid wrote:
> But that only works if you're actually comparing booleans, so something
> like:
> 
> a.opEquals(b) != c.opEquals(d)
> isn't necessarily the same as
> (a.opEquals(b) == true) != (c.opEquals(d))
> whereas since ^^ is a boolean comparison,
> a.opEquals(b) ^^ c.opEquals(d)
> should compare the same.
> 
> Yes?

The safest thing to do is probably

    if (cast(bool)(a==b) != cast(bool)(c==d)) { ... }

Which, incidently, should be equivalent to

    if (cast(bool)(a==b) ^ cast(bool)(c==d)) { ... }

(if we had a more anally retentive boolean type, this would not be a problem.  alas...)

 -- andy
August 01, 2004
Andy Friesen wrote:

> teqDruid wrote:
> 
>> But that only works if you're actually comparing booleans, so something
>> like:
>>
>> a.opEquals(b) != c.opEquals(d)
>> isn't necessarily the same as
>> (a.opEquals(b) == true) != (c.opEquals(d))
>> whereas since ^^ is a boolean comparison,
>> a.opEquals(b) ^^ c.opEquals(d)
>> should compare the same.
>>
>> Yes?
> 
> 
> The safest thing to do is probably
> 
>     if (cast(bool)(a==b) != cast(bool)(c==d)) { ... }
> 
> Which, incidently, should be equivalent to
> 
>     if (cast(bool)(a==b) ^ cast(bool)(c==d)) { ... }
> 
> (if we had a more anally retentive boolean type, this would not be a problem.  alas...)
> 
>  -- andy

I agree. There seem to be random (in that I do not understand them) circumstances in which you either can or cannot implictly cast to a bit type. That means you should randomly cast(bit) if you do not know... Which of course leads to the <sarcasm> terse </sarcasm> code in your example...