Pio*_*ioz 0 c chess bit-manipulation bit
我在C中的这段代码有问题.
#include <stdio.h>
#include <stdint.h>
typedef uint64_t bboard;
// Accessing a square of the bitboard
int
get (bboard b, int square)
{
return (b & (1ULL << square));
}
void
print_board (bboard b)
{
int i, j, square;
for (i = 7; i >= 0; i--) // rank => top to bottom
{
for (j = 0; j < 8; j++) // file => left to right
printf ("%d ", get (b, j+8*i) ? 1 : 0);
printf ("\n");
}
}
int
main ()
{
bboard b = 0xffffffffffffffff;
print_board (b);
}
// result that I have
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
Run Code Online (Sandbox Code Playgroud)
好的,为什么位板没有设置为1位?
如有任何问题,请添加评论.泰:D
get
返回一个int
,但是(b & (1ULL << square))
是uint64_t
.当(b & (1ULL << square))
大于时INT_MAX
,结果是不确定的; 在这种情况下,它会截断并返回0
.
如果get
返回a bboard
,则按预期工作(在此验证:http://codepad.org/zEZiJKeR).
归档时间: |
|
查看次数: |
1026 次 |
最近记录: |