www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Boolean exclusive or?

reply Ola Frid <olafrid atyay dtek.chalmers otday se> <Ola_member pathlink.com> writes:
Any chance of adding a boolean exclusive or? Like ^^?
It's a thing that too many programming languages lack, in my opinion.

/ Ola Frid
Aug 01 2004
next sibling parent reply Andy Friesen <andy ikagames.com> writes:
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
Aug 01 2004
next sibling parent parabolis <parabolis softhome.net> writes:
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

lol well put...
Aug 01 2004
prev sibling parent reply teqDruid <me teqdruid.com> writes:
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)) whereas since ^^ is a boolean comparison, a.opEquals(b) ^^ c.opEquals(d) should compare the same. Yes?
Aug 01 2004
next sibling parent teqDruid <me teqdruid.com> writes:
On Sun, 01 Aug 2004 12:55:55 -0400, 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))

(a.opEquals(b) == true) != (c.opEquals(d) == true)
 whereas since ^^ is a boolean comparison,
 a.opEquals(b) ^^ c.opEquals(d)
 should compare the same.
 
 Yes?

Aug 01 2004
prev sibling next sibling parent reply parabolis <parabolis softhome.net> writes:
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.
Aug 01 2004
parent reply teqDruid <me teqdruid.com> writes:
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 ^^.
Aug 01 2004
parent reply parabolis <parabolis softhome.net> writes:
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 ;)
Aug 01 2004
parent reply teqDruid <me teqdruid.com> writes:
On Sun, 01 Aug 2004 13:35:52 -0400, parabolis wrote:

 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 ;)

Yeah, but saying that ^^ is called != is incorrect. I'm not saying that the language doesn't have the capability.
Aug 01 2004
next sibling parent parabolis <parabolis softhome.net> writes:
teqDruid wrote:

 On Sun, 01 Aug 2004 13:35:52 -0400, parabolis wrote:
 
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.







 Yeah, but saying that ^^ is called != is incorrect.  I'm not saying that
 the language doesn't have the capability.

True but I took Ola Frid's comments as suggesting that.
Aug 01 2004
prev sibling parent Andy Friesen <andy ikagames.com> writes:
teqDruid wrote:
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 ;)

Yeah, but saying that ^^ is called != is incorrect. I'm not saying that the language doesn't have the capability.

Now that you mention it, it DOES seem strange to have an operator just to let us write cast(bool)(a) & cast(bool)(b) -- andy
Aug 01 2004
prev sibling parent reply Andy Friesen <andy ikagames.com> writes:
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
Aug 01 2004
parent parabolis <parabolis softhome.net> writes:
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...
Aug 01 2004
prev sibling parent reply Arcane Jill <Arcane_member pathlink.com> writes:
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
Aug 01 2004
next sibling parent reply Lord Syl <Lord_member pathlink.com> writes:
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

Wha..what!? In D a "bit" is a "int"? But...isn't it a waste to use 32 bits when there could be used only 8? (that is, using a (0, !0) "byte" for boolean values)
Aug 01 2004
next sibling parent Sean Kelly <sean f4.ca> writes:
Lord Syl wrote:
 
 Wha..what!? In D a "bit" is a "int"? But...isn't it a waste to use 32 bits when
 there could be used only 8? (that is, using a (0, !0) "byte" for boolean
values)

In D, a bit is stored in one byte and arrays of bits are packed so they actually are one bit in size. Sean
Aug 01 2004
prev sibling parent Stephan Wienczny <Stephan Wienczny.de> writes:
Lord Syl 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

Wha..what!? In D a "bit" is a "int"? But...isn't it a waste to use 32 bits when there could be used only 8? (that is, using a (0, !0) "byte" for boolean values)

On the other side. You can't easily pack some bits together. Then you would have to do some more asm operations to access it. We live on 32bit machines today... Stephan Wienczny
Aug 01 2004
prev sibling parent reply Ola Frid <Ola_member pathlink.com> writes:
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
Aug 01 2004
next sibling parent reply parabolis <parabolis softhome.net> writes:
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 ================================================================
Aug 01 2004
parent Ola Frid <Ola_member pathlink.com> writes:
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
Aug 01 2004
prev sibling parent reply Arcane Jill <Arcane_member pathlink.com> writes:
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
Aug 01 2004
next sibling parent Arcane Jill <Arcane_member pathlink.com> writes:
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
Aug 01 2004
prev sibling parent Ola Frid <Ola_member pathlink.com> writes:
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
Aug 01 2004