【发布时间】:2018-05-30 19:24:35
【问题描述】:
我有尺寸为 8x8 的“1”和“0”矩阵。我需要将整个矩阵逐位存储在一个 unsigned long long 变量中。我该怎么做?
例如,让我们以 2x2 的 '1' 和 '0' 矩阵为例: 矩阵 2x2:
1 0
0 1
变量必须包含:1001 位。 相同的示例,但在矩阵 8x8 和 unsigned long long 变量上。
这就是我试图做的:
#include <stdio.h>
int main()
{
unsigned long long result = 0;
char matrix[8][8]; // lets that the matrix is already filled by '1' and '0'
for (i=0; i<SIZE; i++)
{
for (j=0; j<SIZE; j++)
{
result = result | ((unsigned long long)(matrix[i][j] - '0'));
result <<= 1;
}
}
return 0;
}
对吗?我在我的算法中实现了这个嵌套循环,但它不能正常工作。
【问题讨论】:
-
定义“没有正常工作”。
-
'temp = 0;'无论你上移多少次 0,它仍然是 0。
-
prog.c:7:8: error: ‘i’ undeclared (first use in this function)prog.c:7:15: error: ‘SIZE’ undeclared (first use in this function)prog.c:9:11: error: ‘j’ undeclared (first use in this function)prog.c:5:22: warning: variable ‘result’ set but not used -
您想先移位然后放置位。也只是在字符串中做一些比较而不是减法。
-
感谢发布演示问题的可编译代码。不编译的代码缺乏清晰性并且更加困难。 “没有正常工作。”不提供信息。发布看到和预期的输出以及minimal reproducible example
标签: c string matrix bit-manipulation bits