Wrong answer of xor operation

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 ?

Réponses (2)

Image Analyst
Image Analyst le 23 Fév 2014

2 votes

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 commentaires

Raza Ali
Raza Ali le 23 Fév 2014
can u explain why subtraction of 0 is required?
Sagar Damle
Sagar Damle le 23 Fév 2014
You can try these also -
a=(dec2bin(13))
b=(dec2bin(14))
% Since 'EX-OR'ing is same as modulo-2 addition
d1 = rem(a+b,2)
d2 = mod(a+b,2)
For more information,read this.
There are many functions in MATLAB to convert base 10 to base 2 of a number –
1. dec2base()
2. dec2bin()
3. de2bi()
4. dec2binvec()
However,outputs of these are are not in same format. Run this code on your computer for understaning purpose. See help about these functions in MATLAB.
Note : '0' represents ASCII value of zero(0) which is 48. So,
ans2 = dec2base(4,2) - '0'
is same as
ans2 = dec2base(4,2) – 48
Play with these functions! ENJOY!
% Program -
temp1 = dec2base(4,2)
ans1 = str2num(dec2base(4,2))
ans2 = dec2base(4,2) - '0'
temp2 = dec2bin(4)
ans3 = dec2bin(4) - '0'
temp3 = de2bi(4)
temp4 = de2bi(4,'right-msb')
ans4 = de2bi(4,'left-msb')
temp5 = dec2binvec(4)
ans5 = seqreverse(dec2binvec(4))
whos
Raza Ali
Raza Ali le 23 Fév 2014
can you refer me any book or other material related to bits manipulation. like the above
Raza Ali
Raza Ali le 23 Fév 2014
Thankx man
Image Analyst
Image Analyst le 23 Fév 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.

Connectez-vous pour commenter.

Azzi Abdelmalek
Azzi Abdelmalek le 23 Fév 2014

1 vote

a and b are char, there is a difference betwenn
xor('1','0')
and
xor(1,0)

5 commentaires

John D'Errico
John D'Errico le 23 Fév 2014
Modifié(e) : John D'Errico le 23 Fév 2014
To be honest, it does seem that xor should have a test for improper (non-numeric/non-logical) input, returning an error here, or at least a warning.
Raza Ali
Raza Ali le 23 Fév 2014
so what should i do to solve my above question?
Jhon, it seems that any character of a string is considered as true
str='I am true'
not(str)
This explains the result given by xor.
Raza Ali
Raza Ali le 23 Fév 2014
thanks man
John D'Errico
John D'Errico le 23 Fév 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.

Connectez-vous pour commenter.

Catégories

Question posée :

le 23 Fév 2014

Commenté :

le 23 Fév 2014

Community Treasure Hunt

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

Start Hunting!

Translated by