如何得到GF(2^n)中元素的整数表示
NTL - how to get the integer representation of an element in GF(2^n)
是否有方法获得该系数向量的整数表示?也就是说,最高次系数是该整数的MSB而x^0的系数是LSB?当使用BytesFromGF2X方法时,它会导致一个奇怪的表示,既不是大端序也不是小端序。
例如,如果元素是x^23+x^20+x+1,那么我想得到的是整数:2^23+2^20+2+1。
使用这两个方法来来回转换为小端序整数表示:
FROM GF2X TO LITTLE ENDIAN INTEGER
void MyBytesFromGF2X(unsigned char* buffer, NTL::GF2X& p, int numbytes) {
int degree = NTL::deg(p);
memset(buffer,0,numbytes);
for(int i=0; i<=degree; i++) {
if(NTL::IsOne(NTL::coeff(p,i))) {
buffer[i/8] |= 1 << i%8;
}
}
}
在buffer
的末尾,p
以正常的小端方式表示数字。
如果你想得到整数,那么假设p
的最大度数是32,然后你做以下操作:
unsigned char buffer[4];
int *integer = buffer;
NTL::GF2X p;
NTL::SetCoeff(p,1,1); // set the coefficient of x^1 to 1
NTL::SetCoeff(p,30,1); // set the coefficient of x^30 to 1
MyBytesFromGF2X(buffer,p,4);
printf("%d",integer);
//result will be LE integer representation of 2^30+2^1
为了转换回GF2X
,您可以使用以下命令:
从小端整数到GF2X
void MyBytesToGF2X(const unsigned char* buffer, NTL::GF2X& p, int numbytes) {
for(int i=0; i<numbytes; i++) {
for(int j=0; j<8; j++) {
int bit = (buffer[i]>>j)&1;
NTL::SetCoeff(p, i*8+j, bit);
}
}
}
这个怎么样:
GF2X P;
SetCoeff(P, 0, 1);
SetCoeff(P, 20, 1);
SetCoeff(P, 23, 1);
ZZ number;
clear(number);
long degree = NTL::deg(P);
for(long i = 0; i <= degree; ++i)
number += conv<ZZ>(P[i]) * power2_ZZ(i);
注意:P
看起来像一个大小为24的数组,如果你打印它。但事实并非如此。它总是被打印为一个系数列表,这样最高的一个是1
。但是P
是知道每个高度数的系数都是0。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 表示"accepting anything for this template argument" C++概念的通配符
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 使用strcpy将char数组的元素复制到另一个数组
- 使用不带参数的函数访问结构元素
- 给定n个元素的m个集合.在C++中找到出现在最大集合数中的元素
- C++如何通过用户输入删除列表元素
- lower_bound()返回最后一个元素
- 基于多个条件处理地图中的所有元素
- 我应该如何表示我拥有的连续元素序列?
- 对于Boost.Propertytree,有没有办法使用JSON点表示法来引用数组元素?
- ptrdiff_t可以表示指向同一数组对象元素的指针的所有减法吗?
- C 中的动态数组(用指针表示) - 输入元素
- 整数数组元素,前导 0 表示垃圾输出
- 可以从指向子对象的指针中获取指向完整对象表示元素的指针吗
- 指向对象表示元素或指向提供的存储元素的指针
- 任何表示具有有序元素的对的 stl/boost 类型
- CodeSynthesis XSD CXX 不解析 XML 文件,表示未声明属性/元素
- 如何得到GF(2^n)中元素的整数表示
- 指向单链表中没有变量表示每个元素的内存地址的指针