I’m trying to do some bitwise operations and I have a few questions / problems:
-
I see Unreal has its own bitset class called FBitSet (FBitSet | Unreal Engine Documentation), but the documentation and functionality appears a bit minimalist. Should I be using it and is there some better documentation / additional functionality?
-
If I use std::bitset I get a few problems
Here is some sample code
constexpr std::bitset<4> N{ 0001 };
constexpr std::bitset<4> S{ 0010 };
constexpr std::bitset<4> E{ 0100 };
constexpr std::bitset<4> W{ 1000 };
std::bitset<4> blockedDirections;
//blockedDirections.set(2); //linker problem if used
blockedDirections |= N;
blockedDirections |= S;
blockedDirections |= E;
blockedDirections |= W;
UE_LOG(LogTemp, Warning, TEXT("blockedDirections size: %d"), blockedDirections.size());
UE_LOG(LogTemp, Warning, TEXT("blockedDirections count: %d"), blockedDirections.count());
int out = static_cast<int>(blockedDirections.to_ulong());
UE_LOG(LogTemp, Warning, TEXT("%d"), out);
OUTPUT
LogTemp: Warning: blockedDirections size: 4
LogTemp: Warning: blockedDirections count: 2
LogTemp: Warning: 9
test and set appear to cause linker problems. size is correct (4), but count suggests only 2 are set when it should be 4. The int value of 9 suggests a binary value of 1001, which ties up with the count == 2, but does not tie up with all 4 bits being set in the block above it.
Any help much appreciated.
Edit: OK I understand part of my problem, if I switch my NSEW variables to decimal integer equivalent values, then I get all the right numbers out… Still interested to get more thoughts on this.
constexpr std::bitset<4> N(1);
constexpr std::bitset<4> S(2);
constexpr std::bitset<4> E(4);
constexpr std::bitset<4> W(8);