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;
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Well, let’s think about this. What does XOR do?
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):
Looking at those two outputs, it’s pretty close, all we have to do is just NOR the two previous outputs (x AND y) (x NOR y) and we’d have the solution!
Now just write that out:
BINGO!
Now just write that into a function