Xor operation on two numbers

13 visualizaciones (últimos 30 días)
PRAVEEN GUPTA
PRAVEEN GUPTA el 11 de Mzo. de 2020
Comentada: Pravindra Kumar el 4 de Ag. de 2022
i have two numbers 23 and 47, i have to convert them into binary and then perform the Xor operation how to do that
  4 comentarios
Guillaume
Guillaume el 11 de Mzo. de 2020
If it were homework, surely the OP wouldn't be cheating by asking us to do it for him/her...
John D'Errico
John D'Errico el 11 de Mzo. de 2020
lol

Iniciar sesión para comentar.

Respuesta aceptada

Bhaskar R
Bhaskar R el 11 de Mzo. de 2020
Editada: Bhaskar R el 11 de Mzo. de 2020
Since decimal to binary conversion may not produce same length of inputs to xor, we need to append 0's before the binary value
in1 = 27;
in2 = 47;
x = dec2bin(in1);
y = dec2bin(in2);
if length(x)~=length(y)
max_len = max(length(x), length(y));
x = [repmat('0', 1, max_len-length(x)), x];
y = [repmat('0', 1, max_len-length(y)), y];
end
result = xor(x-'0', y-'0');

Más respuestas (1)

John D'Errico
John D'Errico el 11 de Mzo. de 2020
Editada: John D'Errico el 11 de Mzo. de 2020
Use the 2 argument form of dec2bin, comverting to binary. This allows you to insure the two binary equivalents are stored with the same number of bits. So one of the binary forms may now have leading zero bits. Otherwise, dec2bin may leave one result too short for the xor. By computing the number of bits necessary in advance for each number, then we can use that 2 argument form for dec2bin.
Next, you need to make the result a logical vector, not the character-form that dec2bin returns. While it looks like a binary number, it is not something that xor can use directly.
Finally, a call to xor does the final piece.
It would look like this:
x = 23;
y = 47;
nbits = floor(log2(max(x,y)))+1;
xb = dec2bin(x,nbits) == '1';
yb = dec2bin(y,nbits) == '1';
result = xor(xb,yb);
Did it work? Yes.
xb
xb =
1×6 logical array
0 1 0 1 1 1
yb
yb =
1×6 logical array
1 0 1 1 1 1
result
result =
1×6 logical array
1 1 1 0 0 0
As you can see, xb now has a leading zero bit, as is needed to make the xor happy. As well, xb and yb are now numeric (logical) vectors, the other thing that xor will need.
If you need to convert result back into a decimal integer form, then use bin2dec. Don't forget to convert the binary form from xor back into characters first, else bin2dec will fail. The idea is to add '0' to the binary form, this gives the ascii numeric form for '0' and '1'. Then the function char takes it to characters, and bin2dec gives you an integer.
bin2dec(char(result + '0'))
ans =
56
Could I have done this more compactly? Well, yes. Two lines would have been sufficient, because I never really needed to compute the number of bits in advance. The trick here is to use dec2bin only once, in a "vectorized" call.
B = dec2bin([x,y]) == '1'
B =
2×6 logical array
0 1 0 1 1 1
1 0 1 1 1 1
result = xor(B(1,:),B(2,:))
result =
1×6 logical array
1 1 1 0 0 0
You can see the vectorized form of dec2bin is smart enough to automatically left pad the smaller number as is needed. That allows us to not need to precompute the number of bits needed. And if I really wanted the result returned in an integer form, I could have done that too.
result = bin2dec(char(xor(B(1,:),B(2,:)) + '0'))
result =
56

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by