boolean xor

Delaney, Timothy tdelaney at avaya.com
Wed Jan 10 23:35:11 EST 2001


This can be done even better ...

def xor (a, b):
	return (not a)!= (not b)

getting rid of one negation ;)

Tim Delaney
Avaya Australia
+61 2 9352 9079

> -----Original Message-----
> From: Delaney, Timothy [mailto:tdelaney at avaya.com]
> Sent: Thursday, 11 January 2001 1:57 PM
> To: 'ndev42 at yahoo.com'; python-list at python.org
> Subject: RE: boolean xor
> 
> 
> Nope ... we're wanting a *logical* xor ...
> 
> def xor_wrong(a,b):
> 	return not(a==b)
> 
> def xor_right(a,b):
> 	return not((not a) == (not b))
> 
> print xor_wrong(0,1)
> print xor_wrong(1,1)
> print xor_wrong(2,1)
> 
> print xor_right(0,1)
> print xor_right(1,1)
> print xor_right(2,1)
> 
> Tim Delaney
> Avaya Australia
> +61 2 9352 9079
> 
> > -----Original Message-----
> > From: ndev42 at yahoo.com [mailto:ndev42 at yahoo.com]
> > Sent: Thursday, 11 January 2001 4:56 AM
> > To: python-list at python.org
> > Subject: Re: boolean xor
> > 
> > 
> > 
> > > I need a boolean (not bit-wise) xor function.  I've got the 
> > following
> > > function, but my logic skills are rusty enough that I'm 
> wondering if
> > > this is the "right" way to do it (I've validated that it 
> > produces the
> > > correct outputs, at least):
> > >
> > > def xor(a,b):
> > >     return not ( (a and b) or (not (a or b)) )
> > 
> > A boolean xor is a detector of differences. Knowing that, you can
> > simply write:
> > 
> > def xor(a,b):
> > 	return not(a==b)
> > 
> > Hope it helps,
> > --
> > Nicolas
> > 
> > 
> > Sent via Deja.com
> > http://www.deja.com/
> > -- 
> > http://www.python.org/mailman/listinfo/python-list
> > 
> 
> -- 
> http://www.python.org/mailman/listinfo/python-list
> 




More information about the Python-list mailing list