Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
bitxor for n elements

Subject: bitxor for n elements

From: suzi freunden

Date: 29 Dec, 2008 12:24:02

Message: 1 of 6

hi,
I want to calculate xor for more than two elements, for exemple n elements
I found the function bitxor, but it works only for two?

Subject: bitxor for n elements

From: David

Date: 29 Dec, 2008 12:35:03

Message: 2 of 6

"suzi freunden" <ghada_zaibi@yahoo.fr> wrote in message <gjafh2$6kq$1@fred.mathworks.com>...
> hi,
> I want to calculate xor for more than two elements, for exemple n elements
> I found the function bitxor, but it works only for two?

the inputs to bitxor can be arrays if that is what you are looking to do... but if you want to do more than 2 scalars or arrays then you will probably have to do some looping.

Subject: bitxor for n elements

From: ImageAnalyst

Date: 29 Dec, 2008 14:52:30

Message: 3 of 6

On Dec 29, 7:35=A0am, "David" <d...@bigcompany.com> wrote:
> "suzi freunden" <ghada_za...@yahoo.fr> wrote in message <gjafh2$6k...@fre=
d.mathworks.com>...
> > hi,
> > I want to calculate xor for more than two elements, for exemple n eleme=
nts
> > I found the function bitxor, but it works only for two?
>
> the inputs to bitxor can be arrays if that is what you are looking to do.=
.. but if you want to do more than 2 scalars or arrays then you will probab=
ly have to do some looping.

---------------------------------------------------
On the odd chance that you're looking to xor three numbers....
Since xor is normally "this or that but not both" then you might
define one for three numbers as
"number1 or number 2 or number 2 but not any two nor all three"
In that case, you can just sum the numbers (if they're binary 0/1) and
check to see if the sum is exactly 1. If the total of all three
numbers is 1 then it's true, else it's false.
Regards,
ImageAnalyst

Subject: bitxor for n elements

From: Roger Stafford

Date: 29 Dec, 2008 16:40:18

Message: 4 of 6

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <f791474c-8fa9-4300-a86e-7163970ab1e4@z6g2000pre.googlegroups.com>...
> On the odd chance that you're looking to xor three numbers....
> Since xor is normally "this or that but not both" then you might
> define one for three numbers as
> "number1 or number 2 or number 2 but not any two nor all three"
> In that case, you can just sum the numbers (if they're binary 0/1) and
> check to see if the sum is exactly 1. If the total of all three
> numbers is 1 then it's true, else it's false.
> Regards,
> ImageAnalyst

  That's not quite right, ImageAnalyst. For three booleans it should be either any one of them or else all three. If summing any number of 0/1 booleans, the sum should be an odd number for a true answer to 'xor'.

  However, Suzi mentioned 'bitxor' and such summation wouldn't work in a bitwise manner. As David states looping is undoubtedly the best way.

Roger Stafford

Subject: bitxor for n elements

From: Matt

Date: 29 Dec, 2008 18:09:02

Message: 5 of 6

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gjauhi$4j2$1@fred.mathworks.com>...
> ImageAnalyst <imageanalyst@mailinator.com> wrote in message <f791474c-8fa9-4300-a86e-7163970ab1e4@z6g2000pre.googlegroups.com>...
> > On the odd chance that you're looking to xor three numbers....
> > Since xor is normally "this or that but not both" then you might
> > define one for three numbers as
> > "number1 or number 2 or number 2 but not any two nor all three"
> > In that case, you can just sum the numbers (if they're binary 0/1) and
> > check to see if the sum is exactly 1. If the total of all three
> > numbers is 1 then it's true, else it's false.
> > Regards,
> > ImageAnalyst
>
> That's not quite right, ImageAnalyst. For three booleans it should be either any one of them or else all three. If summing any number of 0/1 booleans, the sum should be an odd number for a true answer to 'xor'.
>
> However, Suzi mentioned 'bitxor' and such summation wouldn't work in a bitwise manner. As David states looping is undoubtedly the best way.

Why is looping best? Couldn't you just do

all(X) | ( sum(logical(X)) ==1 );

Subject: bitxor for n elements

From: Roger Stafford

Date: 29 Dec, 2008 21:12:01

Message: 6 of 6

"Matt " <mjacobson.removethis@xorantech.com> wrote in message <gjb3nu$qns$1@fred.mathworks.com>...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gjauhi$4j2$1@fred.mathworks.com>...
> > That's not quite right, ImageAnalyst. For three booleans it should be either any one of them or else all three. If summing any number of 0/1 booleans, the sum should be an odd number for a true answer to 'xor'.
> >
> > However, Suzi mentioned 'bitxor' and such summation wouldn't work in a bitwise manner. As David states looping is undoubtedly the best way.
>
> Why is looping best? Couldn't you just do
>
> all(X) | ( sum(logical(X)) ==1 );

  Matt, I said looping was probably necessary for bitwise XOR-ing, as is done by 'bitxor' but where more than two doubles (or uint64's or whatever) are to be XOR-ed.

  What you have is XOR for logical variables, which is quite another thing, but your example only works if X possesses three boolean (logical) elements. The general method would (or could) be

 mod(sum(logical(X)),2)==1

where X is a vector. That is, an odd number of elements should be true for the xor to be true.

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us