How to make bit wise XOR in C
问题 I'm trying to get into C programming, and I'm having trouble writing a bitwise XOR function with only ~ and & operators. Example: bitXor(4, 5) = 1 . How can I achieve this? So far I have this: int bitXor(int x, int y) { return z; } 回答1: Well, let's think about this. What does XOR do? x y XOR ------------ 0 0 0 1 0 1 0 1 1 1 1 0 So how do we turn that into a function? Let's think about AND, and the inverse order of AND (~x&~y) (this happens to be NOR): (~x&~y) x y AND NOR ---------------------