我正在为国际象棋编程初始化位板。但是,当我初始化黑色位板时,二进制输出由于某种原因是不正确的。
以下是代码:
#include <stdint.h>
#include <inttypes.h>
#include <sstream>
#include <bitset>
#include <string>
uint64_t wpawn, wknight, wbishop, wrook, wqueen, wking, bpawn, bknight, bbishop, brook, bqueen, bking;
using namespace std;
void printBoard(uint64_t board)//print binary form of hexadecimal
{
printf("\n");
stringstream ss;
ss<<board;
unsigned x;
ss>>x;
bitset<64>b(x);
string tmp = b.to_string();
int count =0;
for (int i =0; i < 8; i ++)
{
printf("\n");
for (int j=0;j <8;j++)
{
printf("%c ",tmp[count]);
count ++;
}
}
}
void printAll()//print all boards
{
printf("\nwhite\n");
printBoard(wpawn);
printBoard(wknight);
printBoard(wbishop);
printBoard(wrook);
printBoard(wqueen);
printBoard(wking);
printf("\nblack\n");
printBoard(bpawn);
printBoard(bknight);
printBoard(bbishop);
printBoard(brook);
printBoard(bqueen);
printBoard(bking);
}
int main(int argc, char *argv[])
{
wpawn = 0x000000000000FF00;//initialize boards
wknight=0x0000000000000042;
wbishop=0x0000000000000024;
wrook = 0x0000000000000081;
wqueen =0x0000000000000010;
wking = 0x0000000000000008;
bpawn = 0x00FF000000000000;
bknight=0x4200000000000000;
bbishop=0x2400000000000000;
brook = 0x8100000000000000;
bqueen =0x1000000000000000;
bking = 0x0800000000000000;
printBoard(wpawn);//white pawn
printBoard(bpawn);//black pawn
}
输出如下:
white
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
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0
Black
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
当调用printAll()时,所有的白色位板打印输出都很好,但黑色位板的所有打印输出都像bpawn一样,前半部分全部打印出来。任何人都对为什么会这样做有任何建议?
答案 0 :(得分:1)
变化
unsigned x;
要
uint64_t x;