XOR binary numbers C #

I have 2 vars with binary numbers:

var bin1 = Convert.ToString(339, 2);
var bin2 = Convert.ToString(45, 2);

      

and I want to XOR them and get the third binary number, but the ^ operator doesn't work on them. How can i do this?

+3


source to share


2 answers


Don't write binary numbers XOR as strings, then XOR, then int

s:

var xored = 339 ^ 45;

      



Once the operator has ^

done its job, convert the result to string

:

var binXored = Convert.ToString(xored, 2);

      

+1


source


This is possible by first XOR'ing two numbers and then converting it to a representation string

.



int n1 = 339;
int n2 = 45;
int n3 = n1 ^ n2;

string b1 = Convert.ToString(n1, 2);
string b2 = Convert.ToString(n2, 2);
string b3 = Convert.ToString(n3, 2);

      

+1


source







All Articles