1
$\begingroup$

I want to take two numbers, say 200 and 101, and transform them into their binary form. Then XOR them together and transform the result back into the decimal form. The result in this case should be 173.

I tried to play with the BaseForm[200, 2] and BaseForm[101, 2]. However, I cannot work with the result. I also tried the operator Xor, for example, Xor[1, 1], but it returns False instead of 0.

Any idea?

$\endgroup$
1
  • 1
    $\begingroup$ BitXor is intended for this. $\endgroup$ Oct 29, 2023 at 21:56

1 Answer 1

2
$\begingroup$
a = 200;
b = 101;
ceiling = Max@(Ceiling@Log2@# & /@ {a, b})
abin = IntegerDigits[a, 2, ceiling]
bbin = IntegerDigits[b, 2, ceiling]

FromDigits[BitXor[abin, bbin], 2]

173

$\endgroup$
2
  • 1
    $\begingroup$ BitXor[200, 101] would also do it but sometimes things get more involved with the masking going on. $\endgroup$
    – Syed
    Oct 29, 2023 at 15:20
  • 1
    $\begingroup$ Thank you I've got it. $\endgroup$ Oct 29, 2023 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.