我已经创建了64 * 64大小的hadamard字符矩阵,现在想将矩阵的每一行的二进制值(即64位二进制数)传递给数组(查找)。执行此操作的代码是主函数底部的双循环。
当我执行一个列表时,使用此处的代码,而不是完整的二进制值。
#include <stdio.h>
#include <stdlib.h>
void create_hadamard_matrix(char** h, int N)
{
int ind,indr,indc;
for (ind=1;ind<=N;ind*=2)
{
// find the half length of H matrix for this iteration
int sideHalf=ind/2;
//for n=1 H matrix
if (ind==1)
{
h[0][0]='1';
}
else
{
//put in values in bottom left of new H matrix
for (indr=0; indr<sideHalf; indr++)
{
for (indc=0; indc<sideHalf; indc++)
{
h[indr+sideHalf][indc]=h[indr][indc];
}
}
//put in values in top right of new H matrix
for (indr=0; indr<sideHalf; indr++)
{
for (indc=0; indc<sideHalf; indc++)
{
h[indr][indc+sideHalf]=h[indr][indc];
}
}
//put in values in bottom right of new H matrix
for (indr=0; indr<sideHalf; indr++)
{
for (indc=0; indc<sideHalf; indc++)
{
//invert characters
if (h[indr][indc]=='1')
{
h[indr+sideHalf][indc+sideHalf]='0';
}
else
{
h[indr+sideHalf][indc+sideHalf]='1';
}
}
}
}
}
}
void main()
{
int i,j,N=64;
char **h = (char**) malloc(N * sizeof(char*));
for ( i = 0; i < N; i++ )
{
h[i] = (char*) malloc(N * sizeof(char));
}
create_hadamard_matrix(h, N);
unsigned long long *lookup = (unsigned long long*) malloc(N * sizeof(unsigned long long));
for (i=0; i<N; i++)
{
for (j=0; j<N; j++)
{
if (h[i][j]=='1')
{
lookup[i]=(1<<(63-j));
}
}
printf("%llu",lookup[i]);
printf("\n");
}
}
答案 0 :(得分:5)
在main()
(应该返回int
,btw)中,更改此内容:
if (h[i][j]='1');
对此:
if (h[i][j]=='1')
希望显而易见的原因。
接下来,您的查找数组在分配后没有被初始化(虽然您可能认为不需要,但在此之后下一个错误修复是必需的):
改变这个:
unsigned long long *lookup = (unsigned long long*) malloc(N * sizeof(unsigned long long));
对此:
unsigned long long *lookup = calloc(N, sizeof(*lookup));
将在初始化时填充查找数组。如果你愿意的话,你也可以memset
。选择是你的。
接下来,您将为多个位覆盖每行的查找值。即您在任何行的查找条目中写入的 last 位是仅保存的 。您需要使用|=
累积它们,而不仅仅是=
。此外,您需要确保在ulonglong上完成移位操作。如上所述,它正在int
远超int
宽度。见下文。
改变这个:
lookup[i]=(1<<(63-j));
对此:
lookup[i]|=(((unsigned long long)1)<<(63-j));
将所有这些放在一起并使用位打印机我只是猛击,这就是我得到的:
1111111111111111111111111111111111111111111111111111111111111111
1010101010101010101010101010101010101010101010101010101010101010
1100110011001100110011001100110011001100110011001100110011001100
1001100110011001100110011001100110011001100110011001100110011001
1111000011110000111100001111000011110000111100001111000011110000
1010010110100101101001011010010110100101101001011010010110100101
1100001111000011110000111100001111000011110000111100001111000011
1001011010010110100101101001011010010110100101101001011010010110
1111111100000000111111110000000011111111000000001111111100000000
1010101001010101101010100101010110101010010101011010101001010101
1100110000110011110011000011001111001100001100111100110000110011
1001100101100110100110010110011010011001011001101001100101100110
1111000000001111111100000000111111110000000011111111000000001111
1010010101011010101001010101101010100101010110101010010101011010
1100001100111100110000110011110011000011001111001100001100111100
1001011001101001100101100110100110010110011010011001011001101001
1111111111111111000000000000000011111111111111110000000000000000
1010101010101010010101010101010110101010101010100101010101010101
1100110011001100001100110011001111001100110011000011001100110011
1001100110011001011001100110011010011001100110010110011001100110
1111000011110000000011110000111111110000111100000000111100001111
1010010110100101010110100101101010100101101001010101101001011010
1100001111000011001111000011110011000011110000110011110000111100
1001011010010110011010010110100110010110100101100110100101101001
1111111100000000000000001111111111111111000000000000000011111111
1010101001010101010101011010101010101010010101010101010110101010
1100110000110011001100111100110011001100001100110011001111001100
1001100101100110011001101001100110011001011001100110011010011001
1111000000001111000011111111000011110000000011110000111111110000
1010010101011010010110101010010110100101010110100101101010100101
1100001100111100001111001100001111000011001111000011110011000011
1001011001101001011010011001011010010110011010010110100110010110
1111111111111111111111111111111100000000000000000000000000000000
1010101010101010101010101010101001010101010101010101010101010101
1100110011001100110011001100110000110011001100110011001100110011
1001100110011001100110011001100101100110011001100110011001100110
1111000011110000111100001111000000001111000011110000111100001111
1010010110100101101001011010010101011010010110100101101001011010
1100001111000011110000111100001100111100001111000011110000111100
1001011010010110100101101001011001101001011010010110100101101001
1111111100000000111111110000000000000000111111110000000011111111
1010101001010101101010100101010101010101101010100101010110101010
1100110000110011110011000011001100110011110011000011001111001100
1001100101100110100110010110011001100110100110010110011010011001
1111000000001111111100000000111100001111111100000000111111110000
1010010101011010101001010101101001011010101001010101101010100101
1100001100111100110000110011110000111100110000110011110011000011
1001011001101001100101100110100101101001100101100110100110010110
1111111111111111000000000000000000000000000000001111111111111111
1010101010101010010101010101010101010101010101011010101010101010
1100110011001100001100110011001100110011001100111100110011001100
1001100110011001011001100110011001100110011001101001100110011001
1111000011110000000011110000111100001111000011111111000011110000
1010010110100101010110100101101001011010010110101010010110100101
1100001111000011001111000011110000111100001111001100001111000011
1001011010010110011010010110100101101001011010011001011010010110
1111111100000000000000001111111100000000111111111111111100000000
1010101001010101010101011010101001010101101010101010101001010101
1100110000110011001100111100110000110011110011001100110000110011
1001100101100110011001101001100101100110100110011001100101100110
1111000000001111000011111111000000001111111100001111000000001111
1010010101011010010110101010010101011010101001011010010101011010
1100001100111100001111001100001100111100110000111100001100111100
1001011001101001011010011001011001101001100101101001011001101001
希望这应该是它的样子(老实说,我没有线索)。