如何在代码字中进行所有可能的位更改? [英] How to make all possible changes of bits in a codeword?
本文介绍了如何在代码字中进行所有可能的位更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有代码字u,我需要制作一个程序,允许:
为1< = i< = k,对你的k个位中的l进行所有可能的更改。
示例1:
I have codeword u and I need to make a program that allows :
for 1<=i<=k ,make all possible changes of l of the k bits in u.
Example 1:
if u = [1 1 0 1] ; k = 4 and l = 1
我们获得:
we obtain :
u1 = [0 1 0 1] ; u2 = [1 0 0 1] ; u3 = [1 1 1 1] and u4 = [1 1 0 0].
例2:
Example 2 :
if u = [1 0 0 ] ; k = 3 and l = 1
我们获得:
we obtain :
u1 = [0 0 0] ; u2 = [1 1 0] and u3 = [1 0 1].
我试图编程,但我获得:
I tried to program, but i obtain :
if k = 3 and l = 1
u1 = [1 00] ; u2 = [010] and u3 = [001]
我尝试过:
我的代码:
What I have tried:
My code :
l = 1;
ind = nchoosek(1:k, l);
t = size(ind,1);
u1 = zeros(t,k);
u1(bsxfun(@plus, (ind-1)*t, (1:t).')) = 1;
推荐答案
我不知道MATLAB,你的要求不是很清楚但是下面的C代码实现了示例的输出
I do not know MATLAB and your requirement is not very clear but the following C code achieves the output of the examples
int u = 0x0d; // 1101
int k = 4;
int l, result;
for(l = 1 << (k - 1); l != 0; l >>= 1)
{
result = u ^ l; // 0101 = 1101 XOR 1000, 1001 = 1101 XOR 100, 1111 = 1101 XOR 10, 1100 = 1101 XOR 1
}
// it just remains to express 'result' in the form [b1 b2 b3 b4] - where b1 .. b4 are the bit values
上述代码也适用于第二个示例,其中包含3位,当然也有适当的更改u和k的值。
the above code also works for second example with 3 bits with of course appropriate changes in value of u and k.
这篇关于如何在代码字中进行所有可能的位更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文