Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 609
Maik Klein Avatar asked Jan 09 '23 14:01

Maik Klein


1 Answers

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.

like image 153
nvoigt Avatar answered Jan 18 '23 12:01

nvoigt