位操作给错输出

时间:2014-12-28 23:42:51

标签: python c bit-manipulation

我试图使用C在Python中创建一个数独的数独解算器。

我使用的是与Python相同的tecnhinc,但它会产生不同的结果。也许Python正在削减一些我不知道的边缘。

首先我采用81个字符长字符串并将其转换为int [9] [9],如下所示:

char puzzle[] = "800000000003600000070090200050007000000045700000100030001000068008500010090000400";
int PUZZLE[9][9];

    for (int i = 0; i < 81; i++ ){
        int cur_char = puzzle[i];
        PUZZLE[row][column] = cur_char;
        column ++;
        if ((i+1)%9==0){
            column = 0;
            row ++;
        }

然后我对int ROW[9]进行操作:

ROW[row] |= 1<<cur_char;

在完成所有行/列后,我可以通过执行以下操作检查行/列中是否有数字:

if(ROW[i] & 1 << j) printf("ROW: %d has number: %d\n",i,j);

但即使是未填充的未填充网格,9x9也只有0。它给了我这个:

ROW: 0 has number: 1
ROW: 0 has number: 2
ROW: 0 has number: 3
ROW: 0 has number: 4
ROW: 0 has number: 6
ROW: 3 has number: 1
ROW: 3 has number: 3
ROW: 3 has number: 4
ROW: 3 has number: 5
ROW: 5 has number: 5
ROW: 5 has number: 6
ROW: 6 has number: 2
ROW: 6 has number: 6
ROW: 7 has number: 1
ROW: 7 has number: 7
ROW: 7 has number: 8
ROW: 7 has number: 9

这是我的C代码:

#include <stdio.h>

int main(){
    char puzzle[] = "800000000003600000070090200050007000000045700000100030001000068008500010090000400";

    int PUZZLE[9][9];

    char ROW[9];

    int row = 0;
    int column = 0;
    int i;

    for ( i = 0; i < 81; i++ ){
        int cur_char = puzzle[i];
        PUZZLE[row][column] = cur_char;
        ROW[row] |= 1 << cur_char;
        column ++;
        if ((i+1)%9==0){
            column = 0;
            row ++;
        }
    }

    int j;
    for ( i = 0; i < 9; i ++) {
        for ( j = 1; j < 10; j ++) {
            if(ROW[i] & 1 << j) printf("ROW: %d has number: %d\n",i,j);
        }
    }

    return 0;
}

这是我的Python代码:

puzzle = "800000000003600000070090200050007000000045700000100030001000068008500010090000400"
PUZZLE = [[0]*9]*9
ROW = [0]*9
row = 0
column = 0
for i in xrange(81): # not the pythonic way, but the equivalent in C
    cur_char = int(puzzle[i])
    PUZZLE[row][column] = cur_char
    ROW[row] |= 1 << cur_char
    column += 1
    if (i+1)%9==0:
        row += 1
        column = 0

for i in xrange(9):
    for j in xrange(1,10):
        if ROW[i] & 1 << j:
            print "ROW: %d has number: %d"%(i,j)

我的问题是:为什么C代码没有给出与Python相同的结果;虽然它是相同的算法吗?

编辑:

正如所指出的,我现在正在初始化char ROW[9] = {0,0,0,0,0,0,0,0,0};并从cur_char int cur_char = puzzle[i] - 48;

中删除额外的48

C产量:

ROW: 0 has number: 7 #wrong
ROW: 0 has number: 8 #right
ROW: 0 has number: 9 #wrong
ROW: 1 has number: 2 #wrong
ROW: 1 has number: 5 #wrong
ROW: 2 has number: 1 #wrong
ROW: 2 has number: 6 #wrong
ROW: 3 has number: 4 #wrong
ROW: 3 has number: 6 #wrong
ROW: 4 has number: 3 #wrong
ROW: 4 has number: 4 #right
ROW: 4 has number: 6 #wrong
ROW: 5 has number: 2 #wrong
ROW: 6 has number: 5 #wrong
ROW: 6 has number: 7 #wrong
ROW: 6 has number: 8 #right
ROW: 6 has number: 9 #wrong
ROW: 7 has number: 4 #wrong
ROW: 7 has number: 7 #wrong
ROW: 7 has number: 8 #right
ROW: 7 has number: 9 #wrong
ROW: 8 has number: 3 #wrong

Python产生:

ROW: 0 has number: 8
ROW: 1 has number: 3
ROW: 1 has number: 6
ROW: 2 has number: 2
ROW: 2 has number: 7
ROW: 2 has number: 9
ROW: 3 has number: 5
ROW: 3 has number: 7
ROW: 4 has number: 4
ROW: 4 has number: 5
ROW: 4 has number: 7
ROW: 5 has number: 1
ROW: 5 has number: 3
ROW: 6 has number: 1
ROW: 6 has number: 6
ROW: 6 has number: 8
ROW: 7 has number: 1
ROW: 7 has number: 5
ROW: 7 has number: 8
ROW: 8 has number: 4
ROW: 8 has number: 9

正如你所看到的那样,它产生的数字很少,但却错过了最多,而且还增加了其他数字。

2 个答案:

答案 0 :(得分:2)

看行

ROW[row] |= 1 << cur_char;

我看到两个错误。首先,ROW[]尚未初始化,因此您正在使用未初始化的值。其次,C字符串puzzle[]的第一个字符是'8',它是ascii 56.所以移位1 << 56毫无意义,无论如何都是UB,尽管我不知道Python如何处理这个问题。 / p>

答案 1 :(得分:0)

哦,设法使它成功:感谢Weather Vane和Dunes。这是两个错误。

1)我没有初始化ROW和2)我使用的是char数组,而不是int。

这解决了两个错误:

int ROW[9] = {0,0,0,0,0,0,0,0,0};

结果:

ROW 0 has numbers: 8
ROW 1 has numbers: 3, 6
ROW 2 has numbers: 2, 7, 9
ROW 3 has numbers: 5, 7
ROW 4 has numbers: 4, 5, 7
ROW 5 has numbers: 1, 3
ROW 6 has numbers: 1, 6, 8
ROW 7 has numbers: 1, 5, 8
ROW 8 has numbers: 4, 9