Search code examples
matlabbinaryxor

stuck in doing xor two binary numbers in matlab


i'm trying to do the XOR for two binary numbers for example :

10100010 xor 01000101 = 11100111

code for matlab :

y =  bitxor (10100010,01000101 )

but when i use this , the matlab sees these 2 numbers as a decimal numbers and it gives a decimal answer

= 9789327

how can i xor two binary numbers and have a binary answer ?


Solution

  • The issue seems to be that MATLAB is expecting the input in double representation:

    Not the most elegant, but one method:

    y = dec2bin(bitxor(bin2dec(num2str(10100010)), bin2dec(num2str(01000101))));
    

    Or, representing the bits as elements in an vector:

    y =  bitxor([1 0 1 0 0 0 1 0],[0 1 0 0 0 1 0 1]);