Why should I use 1 bit bitfields instead of bools?
UCLASS(abstract, config=Game, BlueprintType, hidecategories=("Pawn|Character|InternalEvents"))
class ENGINE_API ACharacter : public APawn
{
GENERATED_UCLASS_BODY()
...
UPROPERTY(Transient)
uint32 bClientWasFalling:1;
/** If server disagrees with root motion track position, client has to resimulate root motion from last AckedMove. */
UPROPERTY(Transient)
uint32 bClientResimulateRootMotion:1;
/** Disable simulated gravity (set when character encroaches geometry on client, to keep him from falling through floors) */
UPROPERTY()
uint32 bSimGravityDisabled:1;
/**
* Jump key Held Time.
* This is the time that the player has held the jump key, in seconds.
*/
UPROPERTY(Transient, BlueprintReadOnly, VisibleInstanceOnly, Category=Character)
float JumpKeyHoldTime;
The code above is from UE4. They seem to be using uint32 single bit bitfields instead of bools. Why are they doing this?
+3
source to share
1 answer
Standalone bool
is at least one byte long. The processor cannot handle smaller units. However, we all know that a byte can hold 8 bits / bools, so if you have a data structure with multiple bools, you don't need a byte for each. They can share a byte. If you have a lot of such structures, it can save some memory.
+6
source to share