gpt4 book ai didi

c - 了解从 C 到 MatLab 的位移错误

转载 作者:太空宇宙 更新时间:2023-11-03 23:19:33 24 4
gpt4 key购买 nike

我正在尝试创建一个使用 bitshift 操作的函数,我正在创建的这个函数来自于正常工作的 C 代码。正在发生的问题是它仅适用于少数几个值。调试这两个函数(MatLab 和 C)我意识到在 bitshift 操作中是问题所在,在 C 中值为 27 而 MatLab 得到 378。下面是两个函数,C 代码使用 printf() 调用来查看每一行之后的值,在 MatLab 中我使用自己的调试。

C:

int main()
{
unsigned char value = 189;
printf("\nValue: %d",value);
signed char temp,temp2;
// cast to signed value
temp = (signed char) value;
// if MSB is 1, then this will signed extend and fill the temp variable with 1's
printf("\nTemp signed:%d",temp);
temp = temp >> 7;
// AND with the reduction variable
printf("\nTemp << 7: %d",temp);
temp = temp & 0x1b;
printf("\nTemp and: %d",temp);
temp2 = value<<1;
printf("\nValue << 1: %d");
// finally shift and reduce the value
printf("\nGalois Value: %d", (temp2^temp));
}

数学实验室:

value = uint16(189);
hex = uint16(hex2dec('1B'));
temp = typecast(value, 'int8');
temp = bitshift(temp,-7);
temp = bitand(typecast(temp,'uint16'),hex);
temp2 = bitshift(value,1);
galois_value = bitxor(temp2,uint16(temp));
disp(galois_value);

我用来测试它的一些输入值:

48,105,189,112,182,97,96,78,98,236,51,5,5,183,248,231,149,145,248,170,86,143,134,31,186,94,226,64,181,207,64,51,15,119,113,130

有了这个值,预期的输出是(来自 C 代码):

96,210,97,224,119,194,192,156,196,195,102,10,10,117,235,213,49,57,235,79,172,5,23,62,111,188,223,128,113,133,128,102,30,238,226,31

但是我从 matlab 代码中得到这个值:

  96,210,353,224,375,194,192,156,196,451,102,10,10,373,491,469,305,313,491,335,172,261,279,62,367,188,479,128,369,389,128,102,30,238,226,287

基本上出了什么问题:在我的 bitshift(value,1) 操作之后 temp2 应该等于 27(十六进制值) ,但此值正在获取 378。调试 C 代码我了解到 temp2 变量应该等于 27 每次 temp >> 7 运算符等于 -1 ,当 temp >> 7 运算的结果等于 0temp2 的值应该是 0 也是,对于这种情况(temp >> 7 操作等于 0)MatLab 中的函数正在工作,但是当它等于 -1bitshift 得到一个错误的值。

有人知道如何解决这个问题吗?

最佳答案

此 Matlab 代码与我接下来提供的 C 代码具有完全相同的结果。

input_array = [48,105,189,112,182,97,96,78,98,236,51,5,5];
for (i = 1:length(input_array))
value = uint8(input_array(i));
temp = typecast(value, 'int8');
temp = bitshift(temp,-7);
hex = int8(hex2dec('1B'));
temp = bitand(temp,hex);
temp2 = typecast(bitshift(value,1),'int8');
galois_value = typecast(bitxor(temp2,temp),'uint8');
disp(galois_value);
end

.

#include <stdio.h>
unsigned char input_array[] = { 48, 105, 189, 112, 182, 97, 96, 78, 98, 236, 51, 5, 5 };
int main()
{
for (int i = 0; i < 13; ++i)
{
unsigned char value = input_array[i];
//printf("\nValue: %d", value);
signed char temp, temp2;
// cast to signed value
temp = (signed char)value;
// if MSB is 1, then this will signed extend and fill the temp variable with 1's
//printf("\nTemp signed:%d", temp);
temp = temp >> 7;
// AND with the reduction variable
//printf("\nTemp << 7: %d", temp);
temp = temp & 0x1b;
//printf("\nTemp and: %d", temp);
temp2 = value << 1;
//printf("\nValue << 1: %d");
// finally shift and reduce the value
printf("\nGalois Value: %d", unsigned char(temp2^temp));
//printf("\n done");
}
printf("\n done");
}

两种情况下的输出都是: 96 210 97 224 119 194 192 156 196 195 102 10 10

关于c - 了解从 C 到 MatLab 的位移错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44501248/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com