-2

Is there any tricky way to use logical xor operator ^^ in macro in C, C++, Objective-C?

I have tried applying ^^ directly in Objective-C, it does not work.

Edited: let me clarify my answer.

What I want is to use xor operator in macro. It does not mean "how to define the xor operator by a macro.

I.e, I want something like

#if defined(x) ^^ TARGET_OS_IOS ^^ __cplusplus
7
  • There is no logical XOR operator in C or C++ Commented Feb 23, 2017 at 9:04
  • 1
    how about !=? Commented Feb 23, 2017 at 9:05
  • 1
    By summoning a minimum of C programming knowledge, you can write such an operator yourself, in less than 10 seconds. #define XOR(a,b) ( ((a)!=0) ^ ((b)!=0) ). Commented Feb 23, 2017 at 9:38
  • @Lundin: my question is to use logical xor operan IN MACRO. I also want to use it though multiple file. With your solution, I have to include the header file including that macro, of course, it is not efficient Commented Feb 23, 2017 at 11:30
  • @tranvansang Macros are evaluated by the pre-processor, making them very efficient. But you don't have to implement it as a macro, you can simply use ((a)!=0) ^ ((b)!=0) anywhere in the code. It yields the very same machine code. Commented Feb 23, 2017 at 11:50

1 Answer 1

2

For seconds after posting the question, I figured out the answer my self.

!(A) != !(B) will be equivalent to xor operator

A better solution in case the number of operands is different than 2

!(A) ^ !(B) ^ !(C) ^ ...

Sign up to request clarification or add additional context in comments.

2 Comments

You can remove the ! infront of the variables A != B should also do the job
@Aeonos The ! are necessary if A and B are integer types other than bool. Consider A=1; B=2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.