-1

I want to write a chess engine and I've decided to use the bitboard representation of the board. I've done some reading and I found out that the most efficient way to do it is to use 64-bit long variables and bit manipulation. However I also know that there is a thing called std::bitset which, if my understanding is correct, does exactly the thing I need it to.

My question is: Is it better to use the std::bitset in order to have code that is a little slower but easier to read and write or is it better to focus solely on the performance?

Ðаn
  • 10,934
  • 11
  • 59
  • 95
Placeholder
  • 93
  • 1
  • 9
  • 3
    IMHO, code for readability and maintainability first. Then, if you find performance lacking, profile and find where you can start trimming the fat. – NathanOliver Apr 18 '17 at 14:51
  • 3
    What makes you think `std::bitset` is slower than your home-made bit operations? – François Andrieux Apr 18 '17 at 14:51
  • For a simple 8x8 board of chess, you won't get any significant game performance benefits of using one over the other. Use the data structure that makes for the most maintainable code and easier to work with. In other words, which ever one you like better. I'll let the experts give you the formal answer on bitmask vs bitset. – selbie Apr 18 '17 at 14:55
  • If your compiler's any good it'll turn the `std::bitset` function calls into highly optimal assembly anyway (so long as you do turn on the optimiser) – Alnitak Apr 18 '17 at 14:58
  • Since you seem unsure, I'll confirm that your understanding of [`std::bitset`](http://en.cppreference.com/w/cpp/utility/bitset) is correct. *"The class template bitset represents a fixed-size sequence of N bits. Bitsets can be manipulated by standard logic operators and converted to and from strings and integers."* – François Andrieux Apr 18 '17 at 14:59
  • 2
    Also, the answer could very well be "neither". I would think `ChessBoardSpace[8][8]` would be better. Where ChessboardSpace a struct defining color, piece type, and availability of a square on the board. – selbie Apr 18 '17 at 14:59
  • 1
    I'm curious what information you want to store in your bit set. Rather a square is free or occupied? It seems to me like 1 bit is not enough to fully represent a square on a chess board. – François Andrieux Apr 18 '17 at 15:01
  • _"I've done some reading and I found out that the most efficient way to do it is to use 64-bit long variables"_ - citation needed – Alnitak Apr 18 '17 at 15:02
  • You know that a 64 bit integer will compile to single machine instructions on a machine with 64 bit registers, whilst an std::bitset with 64 bits may or may not. It's standard in high end chess programs to use bitmasks to represent allowed positions for each piece. However to actually get the program up to the level where this makes the difference is a very tall order. – Malcolm McLean Apr 18 '17 at 15:08

1 Answers1

2

Since your question is about performance and readability, I suggest you listen to Herb Sutter's speech @ CppCon 2014 here.

In a nutshell:

  • Write for clarity and correctness first.
  • Avoid premature optimization (prefer clear code over optimized code).

I don't think memory optimization is a concern for a chess program IMHO.

AlexG
  • 1,091
  • 7
  • 15
  • 1
    Bit representation of chessboard is not memory optimization, but performance optimization. Some of the "situation queries", like for example whether any pawn can move, can be done with constant (few) amount of `and+or+shift` instructions, instead of jumping over 64 byte array and calculating field indices. It's very specific to chess/checkers game, as those have only 64/32 field board, so it's reasonable to convert it into separate bit masks for particular kind of information about board. – Ped7g Apr 18 '17 at 15:22
  • 1
    @Ped7g my call still stands. unless you want to create a bot that predicts 20+ turns, I would assume a regular chess game runs at user speed, so unless it's a real dealbreaker I wouldn't worry much about that in the early stage of development. – AlexG Apr 18 '17 at 15:25