digitalmars.D - Boolean exclusive or?
- Ola Frid (3/3) Aug 01 2004 Any chance of adding a boolean exclusive or? Like ^^?
- Andy Friesen (5/7) Aug 01 2004 Fewer than you think!
- parabolis (2/15) Aug 01 2004 lol well put...
- teqDruid (10/21) Aug 01 2004 But that only works if you're actually comparing booleans, so something
- teqDruid (3/28) Aug 01 2004 oops! amend that to
- parabolis (7/34) Aug 01 2004 Only if you create a class and overload the opEquals operator
- teqDruid (3/40) Aug 01 2004 True... But in fact Object.opEquals returns int... But my point remains
- parabolis (2/49) Aug 01 2004 No but ^ is ^^ when comparing ints ;)
- teqDruid (3/54) Aug 01 2004 Yeah, but saying that ^^ is called != is incorrect. I'm not saying that
- parabolis (2/18) Aug 01 2004 True but I took Ola Frid's comments as suggesting that.
- Andy Friesen (4/12) Aug 01 2004 Now that you mention it, it DOES seem strange to have an operator just
- Andy Friesen (8/19) Aug 01 2004 The safest thing to do is probably
- parabolis (6/33) Aug 01 2004 I agree. There seem to be random (in that I do not understand
- Arcane Jill (7/10) Aug 01 2004 In D, as has been much discussed in the past, a boolean value is just a ...
- Lord Syl (3/15) Aug 01 2004 Wha..what!? In D a "bit" is a "int"? But...isn't it a waste to use 32 bi...
- Sean Kelly (4/7) Aug 01 2004 In D, a bit is stored in one byte and arrays of bits are packed so they
- Stephan Wienczny (6/32) Aug 01 2004 Have you ever looked at the implementations of other languages?
- Ola Frid (3/15) Aug 01 2004 But, for example, 2 ^ 1 evaluates to 3, while 2 ^^ 1 would evaluate to 0...
- parabolis (9/37) Aug 01 2004 But (cast(bit)lhs) ^ (cast(bit)rhs) == lhs ^^ rhs. That suggests
- Ola Frid (3/40) Aug 01 2004 Yes, but the point being is that it would be very nice not to have to do...
- Arcane Jill (13/15) Aug 01 2004 I can't argue with that. When you said you wanted a boolean xor I assume...
- Arcane Jill (5/7) Aug 01 2004 Actually, no they don't. They evaluate to 1, as an int. (As has been not...
- Ola Frid (7/23) Aug 01 2004 You're right, of course, but the thing I actually want is a consistent w...
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
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
Andy Friesen wrote:Ola Frid <olafrid atyay dtek.chalmers otday se> wrote:lol well put...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
On Sun, 01 Aug 2004 07:47:23 -0700, Andy Friesen wrote:Ola Frid <olafrid atyay dtek.chalmers otday se> 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?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
On Sun, 01 Aug 2004 12:55:55 -0400, teqDruid wrote:On Sun, 01 Aug 2004 07:47:23 -0700, Andy Friesen wrote:oops! amend that to (a.opEquals(b) == true) != (c.opEquals(d) == true)Ola Frid <olafrid atyay dtek.chalmers otday se> 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))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 != :) -- andywhereas since ^^ is a boolean comparison, a.opEquals(b) ^^ c.opEquals(d) should compare the same. Yes?
Aug 01 2004
teqDruid wrote:On Sun, 01 Aug 2004 07:47:23 -0700, Andy Friesen wrote: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.Ola Frid <olafrid atyay dtek.chalmers otday se> 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) == true) // <- amended whereas since ^^ is a boolean comparison, a.opEquals(b) ^^ c.opEquals(d) should compare the same.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
On Sun, 01 Aug 2004 13:28:06 -0400, parabolis wrote:teqDruid wrote:True... But in fact Object.opEquals returns int... But my point remains the same, when comparing ints, != is not ^^.On Sun, 01 Aug 2004 07:47:23 -0700, Andy Friesen wrote: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.Ola Frid <olafrid atyay dtek.chalmers otday se> 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) == true) // <- amended whereas since ^^ is a boolean comparison, a.opEquals(b) ^^ c.opEquals(d) should compare the same.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
teqDruid wrote:On Sun, 01 Aug 2004 13:28:06 -0400, parabolis wrote:No but ^ is ^^ when comparing ints ;)teqDruid wrote:True... But in fact Object.opEquals returns int... But my point remains the same, when comparing ints, != is not ^^.On Sun, 01 Aug 2004 07:47:23 -0700, Andy Friesen wrote: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.Ola Frid <olafrid atyay dtek.chalmers otday se> 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) == true) // <- amended whereas since ^^ is a boolean comparison, a.opEquals(b) ^^ c.opEquals(d) should compare the same.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
On Sun, 01 Aug 2004 13:35:52 -0400, parabolis wrote:teqDruid wrote:Yeah, but saying that ^^ is called != is incorrect. I'm not saying that the language doesn't have the capability.On Sun, 01 Aug 2004 13:28:06 -0400, parabolis wrote:No but ^ is ^^ when comparing ints ;)teqDruid wrote:True... But in fact Object.opEquals returns int... But my point remains the same, when comparing ints, != is not ^^.On Sun, 01 Aug 2004 07:47:23 -0700, Andy Friesen wrote: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.Ola Frid <olafrid atyay dtek.chalmers otday se> 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) == true) // <- amended whereas since ^^ is a boolean comparison, a.opEquals(b) ^^ c.opEquals(d) should compare the same.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
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
teqDruid wrote:Now that you mention it, it DOES seem strange to have an operator just to let us write cast(bool)(a) & cast(bool)(b) -- andyYeah, but saying that ^^ is called != is incorrect. I'm not saying that the language doesn't have the capability.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
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
Andy Friesen wrote:teqDruid wrote: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...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
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 FridIn 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. Arcane Jill
Aug 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...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)Any chance of adding a boolean exclusive or? Like ^^? It's a thing that too many programming languages lack, in my opinion. / Ola FridIn 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. Arcane Jill
Aug 01 2004
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
Lord Syl wrote:In article <cej0hj$258c$1 digitaldaemon.com>, Arcane Jill says...Have you ever looked at the implementations of other languages? 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 WiencznyIn article <cein8f$21i8$1 digitaldaemon.com>, Ola Frid <olafrid atyay dtek.chalmers otday se> says...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)Any chance of adding a boolean exclusive or? Like ^^? It's a thing that too many programming languages lack, in my opinion. / Ola FridIn 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. Arcane Jill
Aug 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...But, for example, 2 ^ 1 evaluates to 3, while 2 ^^ 1 would evaluate to 0. / Ola FridAny chance of adding a boolean exclusive or? Like ^^? It's a thing that too many programming languages lack, in my opinion. / Ola FridIn 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. Arcane Jill
Aug 01 2004
Ola Frid wrote:In article <cej0hj$258c$1 digitaldaemon.com>, Arcane Jill says...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 ================================================================In article <cein8f$21i8$1 digitaldaemon.com>, Ola Frid <olafrid atyay dtek.chalmers otday se> says...But, for example, 2 ^ 1 evaluates to 3, while 2 ^^ 1 would evaluate to 0. / Ola FridAny chance of adding a boolean exclusive or? Like ^^? It's a thing that too many programming languages lack, in my opinion. / Ola FridIn 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. Arcane Jill
Aug 01 2004
In article <ceja2k$28v5$1 digitaldaemon.com>, parabolis says...Ola Frid wrote:Yes, but the point being is that it would be very nice not to have to do that. / Ola FridIn article <cej0hj$258c$1 digitaldaemon.com>, Arcane Jill says...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 ================================================================In article <cein8f$21i8$1 digitaldaemon.com>, Ola Frid <olafrid atyay dtek.chalmers otday se> says...But, for example, 2 ^ 1 evaluates to 3, while 2 ^^ 1 would evaluate to 0. / Ola FridAny chance of adding a boolean exclusive or? Like ^^? It's a thing that too many programming languages lack, in my opinion. / Ola FridIn 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. Arcane Jill
Aug 01 2004
In article <cej9bo$28m1$1 digitaldaemon.com>, Ola Frid says...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. JillTherefore, the operator ^ will do what you want.But, for example, 2 ^ 1 evaluates to 3, while 2 ^^ 1 would evaluate to 0.
Aug 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
Aug 01 2004
In article <cejb5p$29ln$1 digitaldaemon.com>, Arcane Jill says...In article <cej9bo$28m1$1 digitaldaemon.com>, Ola Frid says...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 FridI 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. JillTherefore, the operator ^ will do what you want.But, for example, 2 ^ 1 evaluates to 3, while 2 ^^ 1 would evaluate to 0.
Aug 01 2004