gpt4 book ai didi

c - 伽罗华域算法从C到Matlab

转载 作者:太空宇宙 更新时间:2023-11-04 02:30:10 33 4
gpt4 key购买 nike

我正在为 Matlab 转录 Gallois 域中乘以 2 的 C 代码,问题是我的 matlab 代码没有显示与 C 代码相同的值。显然一切正常,我在 matlab 中注释了代码以识别代码下方的 C 代码的改编。

C:

#include <stdio.h>
#include <stdlib.h>

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

输出:

-86
-1
27
335

数学实验室:

hex = uint8(hex2dec('1B'));                     % define 0x1b
temp = uint8(hex2dec('AA')); % temp = (signed char) value;
disp(temp);
value = uint8(hex2dec('AA')); % unsigned char value = 0xaa
temp = bitsra(temp,7); % temp = temp >> 7;
disp(temp);
temp = bitand(temp,hex); % temp = temp and 0x1b
disp(temp);
galois_value = bitxor(bitsll(value,1),temp); % ((value << 1)^temp)
disp(galois_value); % printf ("\n%u",...)

输出:

 170

1

1

85

C 代码工作正常,我在 C 代码中打印 %d 以显示变量的整数值,因为在代码的开头进行了转换。

有人知道发生了什么

最佳答案

试试这个:

hex = uint8(hex2dec('1B')); 

temp = typecast(uint8(hex2dec('AA')), 'int8');
disp(temp);

temp = bitshift(temp,-7);
disp(temp);

temp = bitand(typecast(temp,'uint8'),hex);
disp(temp);

galois_value = bitxor(bitshift(uint16(hex2dec('AA')),1),uint16(temp));
disp(galois_value);

关于c - 伽罗华域算法从C到Matlab,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44372944/

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