Search code examples
matlabxor

Why does Matlab's `bitxor(1000,10)` gives 994?


I know that bitxor is bit by bit addition modulo 2, so isn't bitxor(1000,10) supposed to give 1010? why is it giving me 994?


Solution

  • The problem is 1000 and 10 are NOT binary numbers. In decimal 0b1000 would be 8 and 0b10 would be 2. Try this:

    bitxor(8,2)
    ans = 10
    

    Now you might think its wrong but ans is also NOT binary, its decimal 10 which equals 1010 binary.

    EDIT: to make it work the way you want try this:

    dec2bin(bitxor(bin2dec('1000'), bin2dec('10'))
    ans = 1010