1

我想一起实现异或。例如,我有两个位对,分别是 6 (110) 和 3 (011)。现在我想实现两个输入的按位异或。它可以通过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)
4

1 回答 1

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;
于 2014-07-09T15:29:39.540 回答