-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
Sang
  • 4,049
  • 3
  • 37
  • 47
  • There is no logical XOR operator in C or C++ – StoryTeller - Unslander Monica Feb 23 '17 at 09:04
  • 1
    how about `!=`? – Sang Feb 23 '17 at 09: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) )`. – Lundin Feb 23 '17 at 09: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 – Sang Feb 23 '17 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. – Lundin Feb 23 '17 at 11:50
  • @lundin: why not `(!(a) ^ !(b))` – Sang Feb 23 '17 at 12:34
  • @tranvansang The only difference is that `!` doesn't promote the operand and always returns an `int`. `!=` promotes the operand. I don't think it matters in your case. – Lundin Feb 23 '17 at 12:45

1 Answers1

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) ^ ...

Sang
  • 4,049
  • 3
  • 37
  • 47