【问题标题】:How to represent Bitxor to mod in matlab如何在matlab中将Bitxor表示为mod
【发布时间】:2014-08-30 17:00:30
【问题描述】:

我想一起实现异或。例如,我有两个位对,分别是 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)

【问题讨论】:

  • 想知道这里提供的答案是否适合您?如果是,请考虑接受它,以便在 Stackoverflow 中将其视为“已关闭”。

标签: matlab bit-manipulation modulus bitwise-xor


【解决方案1】:

代码

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;

【讨论】:

  • 达尼特!我正要写答案。你打败了我......再次:P。这与我的方法基本相同。 +1
  • 可能的改进;第一行:max_digits = ceil(log2(max(num1,num2)));,最后一行:out = sum(pow2(mod(num1d+num2d,2), max_digits-1:-1:0))
  • @Amro 谢谢,ceil 部分确实是一个错误!我想我会保留out 部分,因为它很简洁:)
  • @Amro 错别字! :) 好吧,我也在努力让 cmets 在不引入水平滚动条的情况下挤进去;)
  • 我也讨厌那些滚动条!你应该保留ab 作为变量名:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-30
  • 1970-01-01
  • 2015-07-20
  • 2016-04-05
  • 2011-08-23
  • 2016-10-22
相关资源
最近更新 更多