Another maze question, although this is more practical than the last. I'm storing both the cell data and visited cell bit flags in separate packed 64-bit integers. The cell data consists of 4 bits per cell, so I can store 16 per integer, and that is working fine. However, the visited array stores a single bit per cell and while I can set the bits just fine, retrieving them doesn't quite work and I am stumped.
The code in question is:
bool Maze::GetVisitedData(int64_t index) const
{
if (VisitedArray == nullptr || ((index < 0) || (index > (num_rows * num_columns) - 1)))
return false;
// Shifting to the right by 6 chooses the correct 64-bit integer in the array.
return VisitedArray[index >> int64_t(6)] & (index & int64_t(63));
}It simply doesn't work. Printing the returned value either gives me 0 or 1, or, for no apparent reason, the value of the bit position. I've even tried wrapping it in a ternary operator to return true or false to no avail.
That being said, if I change it to this, it works fine:
bool Maze::GetVisitedData(int64_t index) const
{
if (VisitedArray == nullptr || ((index < 0) || (index > (num_rows * num_columns) - 1)))
return false;
// Shifting to the right by 6 chooses the correct 64-bit integer in the array.
return std::bitset<64>(VisitedArray[index >> int64_t(6)])[index & int64_t(63)];
}The problem is that using bitset, especially as such, is SLOOOW! I'm clearly doing something wrong, but I cannot see it.