伽罗华域算法从C到Matlab
Galois field algorithm from C to Matlab
我正在为 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
MatLab:
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);
我正在为 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
MatLab:
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);