3
$\begingroup$

If we have:

x = 1101 and y = 101

How to XORing these numbers?

$\endgroup$
  • 1
    $\begingroup$ Why would you want to XOR these numbers? $\endgroup$ – Yuval Filmus Oct 23 '15 at 7:46
  • $\begingroup$ You already asked essentially the same question on Stack Overflow: stackoverflow.com/q/31520787/781723. It would have been better to mention the comments you already got over there when you asked it here, and link to the pre-existing question on Stack Overflow in this post. $\endgroup$ – D.W. Oct 10 '16 at 3:25
4
$\begingroup$

There are multiple ways to interpret this XOR.

The most common one is that you XOR bit-wise, and that these strings represent numbers, so adding 0 to the left doesn't change their value.

Then $5 \equiv 101 \to 0101$ and then you XOR bit-wise.

x 1101
y 0101
------- XOR
  1000 

which gives 8, if we are talking about unsigned numbers. However, the above depends on my understanding of your system. Change the assumptions, change the results.

$\endgroup$
  • $\begingroup$ What if we have two binary numbers first one 8-bits and the second one is 55-bits. Is there another method to xorring them withot add "0"? $\endgroup$ – SHdotCom Oct 23 '15 at 12:31
  • $\begingroup$ what is the aim of XOR'ing them? $\endgroup$ – Ran G. Oct 23 '15 at 13:57
0
$\begingroup$

Look first you should know is your numbers in Signage system value or not if they are in Signage system value you should do some stuff 1_ your number is positive (it means the last bit is zero such as 101 this number is -3) so in this case if you want to increase the number of bits you should add 1 to last bit . in your case the number 101 turns into 1101 and 1101 xor 101 turns into 1101 xor 1101 and the answer is 0000 2_ but if your numbers were normal (not in Signage system value mode) very easy you add zero to last bit . in your case 1101 xor 101 become 1101 xor 0101 and the answer is 1000

$\endgroup$
  • $\begingroup$ XOR is a logical operation, so it doesn't make too much sense to sign-extend numbers being XORed. $\endgroup$ – Yuval Filmus Apr 27 '18 at 19:05
0
$\begingroup$

if x and y are integer values, then

int XOR(int x,int y)
{   
    return x^y;
}

whereas, if x and y are strings, then

string XOR(string x,string y)
{
    auto temp= bitset<4>(x) ^ bitset<4>(y);
    string u;
    u=temp.to_string();
    return u;
}
$\endgroup$
  • 2
    $\begingroup$ In the notation of your 2nd code block, the question is about bitset<4>(x) ^ bitset<3>(y). $\endgroup$ – greybeard Dec 13 '19 at 3:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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