如何在matlab中将Bitxor表示为mod

时间:2014-07-09 15:19:56

标签: matlab bit-manipulation modulus bitwise-xor

我想一起实现xor bitwise。例如,我有两个位对,即6(110)和3(011)。现在我想实现两个输入的按位xor。它可以通过matlab中的bitxor函数来完成。

out=bitxor(6,3);%output is 5

但我想通过mod函数而不是bitxor来实现该方案。怎么用matlab做的?非常感谢你。这是我的代码

out=mod(6+3,2^3) %2^3 because Galois field is 8 (3 bits)

1 个答案:

答案 0 :(得分:3)

<强>代码

function out = bitxor_alt(n1,n2)

max_digits = ceil(log(max(n1,n2)+1)/log(2));%// max digits binary representation

n1c = dec2bin(n1,max_digits); %// first number as binary in char type
n2c = dec2bin(n2,max_digits); %// second number as binary in char type

n1d = n1c-'0'; %// first number as binary in double type
n2d = n2c-'0'; %// second number as binary in double type

out = bin2dec(num2str(mod(n1d+n2d,2),'%1d')); %// mod used here

return;