我正在为我的 comsci 类(class)做作业。但是,我对作业的要求没有任何疑问。这个想法是通过选择特定位位置(抽头)、将种子向左移动 1,然后使用 XOR 逻辑将 0 或 1 附加到种子的右端,基于种子随机生成数字。基本情况是种子 6 (0110),然后抽头 3 和 2。 Tap3 = 0, Tap2 = 1, 0 XOR 1 = 1。然后 6 (0110) 左移并变为 12 (1100),最后 1 是添加后,值变为 13 (1101)。水龙头保持在原位,直到打印出所需数量的值。 Tap3 = 1 和 Tap2 = 0 之后的含义。
int tapShift(int tempSeed, int tap){//shifts seed over to the desired tap and returns that tap value. 0 or 1
int bitVal = tempSeed >> tap;
bitVal == bitVal & 1;
return bitVal;}
int xorTaps(int taps[], int length, int tempSeed){
if(length == 0){
return tapShift(tempSeed, 31);//return a-1 tap after all input taps.
}else{//returns the binary value of selected tap to recursively determine XOR of taps.
return tapShift(tempSeed, taps[length-1])^xorTaps(taps,--length, tempSeed);} }
我遇到的问题是 xorTaps 返回诸如 6,7,5,3 之类的值,而不是我认为可以返回的值。我已经研究了几个小时了,但没有任何进展,因此我决定寻求帮助。我在代码的更下方调用 xorTaps。
int feedback = xorTaps(taps, i , tempSeed);
n-1 位的抽头必须始终被视为分配的一部分。这意味着 32 位种子的槽 31 的抽头始终在 XOR 算法中进行评估。
最佳答案
嗯。我问我的兄弟谁更擅长调试。 20分钟后我输错了
bitVal == tempSeed & 1。
正确的代码是 bitVal = tempSeed & 1。
关于c - XOR 递归函数返回除零和一之外的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36764076/