1

Problem

Hello, this is my first stack overflow question. I'm using Bit-boards to represent board states in my chess engine. Currently I have a bit-board class like so:

class Bitboard {
    public:
        uint64_t Board = 0ULL;

        void SetSquareValue(int Square) {

            Board |= (1ULL << Square);            

        }

        void PrintBitbaord() {
            for (int rank = 0; rank < 8; rank++) {
                for (int file = 0; file < 8; file++) {
                    // formula for converting row and colum to index 
                    // rank * 8 + file
                    int square = rank * 8 + file;

                    if (file == 0) {
                        cout << 8 - rank << " " << "|" << " ";
                    }


                    cout << (Board & (1ULL << square)) ? 1 : 0;
                    cout << " ";
                }
                cout << endl;
            }
            cout << "    ---------------" << endl;
            cout << "    a b b d e f g h" << endl;

            //cout << "current bitboard: " << self << endl;
        }
};

I also have this enum (positioned above class in actual file):

enum BoardSquare {
  a8, b8, c8, d8, e8, f8, g8, h8,
  a7, b7, c7, d7, e7, f7, g7, h7,
  a6, b6, c6, d6, e6, f6, g6, h6,
  a5, b5, c5, d5, e5, f5, g5, h5,
  a4, b4, c4, d4, e4, f4, g4, h4,
  a3, b3, c3, d3, e3, f3, g3, h3,
  a2, b2, c2, d2, e2, f2, g2, h2,
  a1, b1, c1, d1, e1, f1, g1, h1
};


My problem is in the set square value method calling that:

TestBitboard.SetSquareValue(e2);

Followed by:

TestBitboard.PrintBitboard();

Gives a strange output:


8 | 0 0 0 0 0 0 0 0 
7 | 0 0 0 0 0 0 0 0 
6 | 0 0 0 0 0 0 0 0 
5 | 0 0 0 0 0 0 0 0 
4 | 0 0 0 0 0 0 0 0 
3 | 0 0 0 0 0 0 0 0 
2 | 0 0 0 0 4503599627370496 0 0 0 
1 | 0 0 0 0 0 0 0 0 
    ---------------
    a b c d e f g h

Compared to my desired output:


8 | 0 0 0 0 0 0 0 0 
7 | 0 0 0 0 0 0 0 0 
6 | 0 0 0 0 0 0 0 0 
5 | 0 0 0 0 0 0 0 0 
4 | 0 0 0 0 0 0 0 0 
3 | 0 0 0 0 0 0 0 0 
2 | 0 0 0 0 1 0 0 0 
1 | 0 0 0 0 0 0 0 0 
    ---------------
    a b c d e f g h

I'm hoping someone with more experience in cpp and bit manipulation could explain why im getting this output. While im not new to programming, i am new to cpp and I've never had to mess with bits before.

What I've tried

I've watched the following videos. i have tried to use their implementations of a similar function to no avail.

I've also messed around with the code swapping values, trying different enum values, etc.

The most change I've been able to get is the value 4503599627370496 to change to something different but still not my desired output.

3
  • 2
    Sometimes it really helps to look at a number in hex: 4503599627370496 ->0x10000000000000. I don't know if that means anything to you, but it does look interesting. Commented Jun 23, 2021 at 0:43
  • unrelated, but why is the columns a b b d instead of a b c d? Commented Jun 23, 2021 at 0:49
  • Create a minimal reproducible example Commented Jun 23, 2021 at 0:50

1 Answer 1

4

You’re running afoul of operator precedence. << binds more strongly than ?:, so the bitwise expression is printed directly, and the conditional expression is performed on the resultant state of the stream and has no effect.

Add parentheses around your intended conditional expression: presumably, cout << ((Board & (1ULL << square)) ? 1 : 0);.

Sign up to request clarification or add additional context in comments.

1 Comment

Compiler warning about this, too; godbolt.org/z/jjdcbG1Ea

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.