Wrong answer of xor operation

16 visualizaciones (últimos 30 días)
Raza Ali
Raza Ali el 23 de Feb. de 2014
Comentada: John D'Errico el 23 de Feb. de 2014
a=(dec2bin(13))
a=1101
b=(dec2bin(14))
b=1110
c=xor(a,b)
the answer i get is c= 0 0 0 0
which is wrong. how should i solve this problem ?
  1 comentario
Raza Ali
Raza Ali el 23 de Feb. de 2014
the answer should be
c= 0011

Iniciar sesión para comentar.

Respuestas (2)

Image Analyst
Image Analyst el 23 de Feb. de 2014
Try this:
% Subtract '0' to get numerical array.
a=(dec2bin(13))-'0'
b=(dec2bin(14))-'0'
% Two ways to do xor:
d1 = (a | b) & ~(a & b)
d2=xor(a,b)
  5 comentarios
Raza Ali
Raza Ali el 23 de Feb. de 2014
Thankx man
Image Analyst
Image Analyst el 23 de Feb. de 2014
Raza, subtraction of '0' turns it from a character string into an array of individual numbers to that we can use xor like you want to.

Iniciar sesión para comentar.


Azzi Abdelmalek
Azzi Abdelmalek el 23 de Feb. de 2014
a and b are char, there is a difference betwenn
xor('1','0')
and
xor(1,0)
  5 comentarios
Raza Ali
Raza Ali el 23 de Feb. de 2014
thanks man
John D'Errico
John D'Errico el 23 de Feb. de 2014
Yes, I recognize that. And thus my point. Since it is impossible to return a meaningful result, and it is likely that users, especially novices, can have problems, then an error should result. There can be no case where returning an error can be a problem for backwards compatibility, since xor never has returned anything meaningful for character input.

Iniciar sesión para comentar.

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by