Maik Klein
Maik Klein

Reputation: 16148

Why should I use 1bit 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 use uint32 one bit bitfields instead of bools. Why are they doing this?

Upvotes: 4

Views: 3532

Answers (1)

nvoigt
nvoigt

Reputation: 77285

A standalone bool is at least one byte long. A processor cannot process smaller units. However, we all know that a byte could hold 8 bits/bools, so if you have a data structure featuring multiple bools, you don't need a byte for each. They can share a byte. If you have many of those structures, it may save some memory.

Upvotes: 9

Related Questions