Bitset vs FBitSet

I’m trying to do some bitwise operations and I have a few questions / problems:

  1. 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?

  2. 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);

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);

You can use string to initialize:

constexpr std::bitset<4> N("0001");

Integer literals started with 0 (like 0110) are treated as octal numbers

1 Like

Right, also worth mentioning are binary literals:

constexpr std::bitset<4> N(0b0001);

Ah, thanks both of you. I think the 0b prefix was what I needed.

I believe TStaticBitArray is closer to std::bitset functionality than FBitSet.
TStaticBitArray | Unreal Engine 5.4 Documentation | Epic Developer Community (