【问题标题】:How to concatenate bit by bit in c?如何在c中逐位连接?
【发布时间】: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


【解决方案1】:

可以使用strtoull() 将整数的文本表示形式转换为其整数值。

char buf[sizeof(matrix)+1];
memcpy(buf, matrix, sizeof(matrix));
buf[sizeof(matrix)] = '\0';
result = strtoull(buf, NULL, 2);

【讨论】:

  • @PeterJ_01:我更喜欢向别人展示解决技术问题的更好方法,而不是因为有人让我做作业而感到恶心。
  • 如果不是'0' 将是' ' 而不是'1' 'x'
  • @PeterJ_01:毫无疑问,理解基础知识是有价值的,但输入的替代表示并不是不使用已经测试和正确实现的 C 库函数的借口。因此,对于' ''x',将输入转换为函数所期望的,然后调用函数。
【解决方案2】:

试试这个

const int mx_size = 8;


int main() {
    unsigned long long result = 0;

    bool  matrix[8][8]; // lets that the matrix is already filled by '1' and '0'
    for (int i =0; i < mx_size; ++i)
        matrix[i][i] = 1;

    for (int i = 0; i < mx_size; i++) {
        for (int j = 0; j < mx_size; j++) {         
            result |= (unsigned long long)matrix[i][j] << (i*mx_size + j);
        }
    }

    return 0;
}

【讨论】:

  • @chux 你在这里
【解决方案3】:

这里有代码(多一点

#include <stdio.h>
#include <stdint.h>

uint64_t convert(char matrix[8][8], int order, char zero)
{
    uint8_t byte;
    uint64_t result = 0;
    for(size_t row = 0; row < 8; row++)
    {
        byte = 0;
        for(size_t column = 0; column < 8; column++)
        {
            byte <<= 1;
            byte |= matrix[row][column] != zero ? 1 : 0; //anything != defined zero char is 1
        }
        if (order)
        {
            result |= (uint64_t)byte << (8 * row);
        }
        else
        {
            result |= (uint64_t)byte << (56 - 8 * row);
        }
    }
    return result;
}

int main(void) {
    char matrix[8][8] = 
    {
        {'1','0','1','0','1','0','1','0'},
        {'0','1','0','1','0','1','0','1'},
        {'1','1','1','0','0','0','1','1'},
        {'0','0','0','1','1','1','0','0'},
        {'1','1','1','1','1','0','0','0'},
        {'0','0','0','0','1','1','1','1'},
        {'1','1','0','0','1','1','0','0'},
        {'0','0','1','1','0','0','1','1'},
    };

    unsigned long long result = convert(matrix, 0, '0');

    for(size_t index = 0; index < 64; index ++)
        printf("%1d", !!(result & (1ULL << index)));
    printf("\n");
    result = convert(matrix,1, '0');
    for(size_t index = 0; index < 64; index ++)
        printf("%1d", !!(result & (1ULL << index)));
    printf("\n");

    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-29
    • 2015-03-05
    • 1970-01-01
    • 2016-11-24
    相关资源
    最近更新 更多