Reputation: 33
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 a third binary number but the operator ^ doesn't work on them. How can I do it?
Upvotes: 3
Views: 1352
Reputation: 1136
This is possible by first XOR'ing the two numbers and then converting it to a string
representation.
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);
Upvotes: 1
Reputation: 726489
Don't XOR binary numbers represented as strings, XOR then as int
s:
var xored = 339 ^ 45;
Once operator ^
has done its work, convert the result to string
:
var binXored = Convert.ToString(xored, 2);
Upvotes: 1