gpt4 book ai didi

将二进制无符号 vector 转换为 dec 列表

转载 作者:太空宇宙 更新时间:2023-11-04 04:53:27 26 4
gpt4 key购买 nike

此代码将 unsigned long vector 变量 cR1 转换为 NB_ERRORS 数字(在 a 变量中我打印了这些数)。

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

int EXT_DEGREE = 8;
int NB_ERRORS = 8;
int BIT_SIZE_OF_LONG = 32;
typedef unsigned short gf_t;
int main(void){

int j, k, l;
gf_t a;
unsigned long cR1 [] = {418636844,2037720909}; //example
for (l = 0; l < NB_ERRORS; ++l) {
k = (l * EXT_DEGREE) / BIT_SIZE_OF_LONG;
j = (l * EXT_DEGREE) % BIT_SIZE_OF_LONG;
a = cR1[k] >> j;
if(j + EXT_DEGREE > BIT_SIZE_OF_LONG)
a ^= cR1[k + 1] << (BIT_SIZE_OF_LONG - j);
a &= ((1 << EXT_DEGREE) - 1);
printf("a = %d\n", a);
}

例如,我有一个 cR1 数组,其中包含两个元素:

0,0,1,1,0,1,0,0,0,0,1,0,0,1,1,1,1,1,0,0,1,1, 1,1,0,0,0,1,1,0,0,01,0,1,1,0,0,1,0,1,1,1,0,0,1,0,0,1,0,1,0,1,1,1,0 ,1,0,0,1,1,1,1,0

执行我得到的代码:

    a = 44
a = 228
a = 243
a = 24
a = 77
a = 39
a = 117
a = 121

这段代码是从右到左转换的,我想修改成从左到右转换。我在哪里可以修改它?

最佳答案

尝试修改j计算行。替换为:

j = ( (NB_ERRORS - 1 - l) * EXT_DEGREE) % BIT_SIZE_OF_LONG;

cR1 中包含的数字将从左边开始转换。输出将是:

a = 24
a = 243
a = 228
a = 44
a = 121
a = 117
a = 39
a = 77

关于将二进制无符号 vector 转换为 dec 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12345048/

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