Bitboard: hexadecimal to binary conversion

user5482467

I am initializing bitboards for chess programming. However, when I initialize the black bitboards, the binary output is incorrect for some reason.

The following is the code:

#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
}

The output is as follows:

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 

When calling printAll(), all of the white bitboards printout fine, but the black bitboards all printout as the bpawn did with the first half all one's. Anyone have any advice on why this is occurring?

Jason Ball

change unsigned x;

to uint64_t x;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related