Questions about bool variable


I’ve been trying to learn C++ and one of the things I do is to read the C++ templates’ codes, where I found that you can use uint32 to declare a bool. Then I googled and found this: Using uint32:1 to Create a Boolean - UE4 AnswerHub and this: (I thought this would be a common C++ technique but the google results are all UE4 related…it seems like Epic Games‘ secret art…)

The reason for doing this is quite clear in those two posts. I still have some questions that I didn’t find answers to though.

  1. Most importantly, how to use it… The MSDN link in the AnswerHub post has 2 examples, both of which are data structs. Naturally I would assume one has to declare a bunch of bools in a struct using uint32 to benefit from it. But in the template (twinstick shooter template to be exact) they just declared a lonely bool “bCanFire”.
    Say I have an actor class where a bool is stored this way, and I have 32 instances of this actor placed in my level, are these 32 instances’ bool variables stored in a single uint32 or 32 different uint32s? What if I have 32 classes and each of them has a bool stored this way and one instance in the level?
    Generally, if I keep all my bools in all classes declared this way, will I be saving memory? Or do I have to declare like more than 4 bools in a single struct to save memory?

  2. Being declared this way, I assumed it’ll be used like this: if (bCanFire == 1), however in the code they just wrote if (bCanFire == true). Does this mean that those 1 bit bools can be used exactly the same as a normal 1 byte bool? If so, why not jut if (bCanFire)?

Sorry it might be too long for a rather simple question. Thanks for helping!

bump, someone help…

I think Epic uses such bools everywhere because in general they are more memory efficient.

  1. Every instance of a class object will have exactly the same memory layout. So if your class have only 1 bool variable it will occupy an uint32 of memory (in every instance). If you have more than 1, but less then 32, they will also occupy the same uint32.

  2. This comes from the way computer stores things in memory. In terms of low-level C/C++ stuff 0 value considered ‘false’ and any other value (1, 2, 324234, -8 and so on) considered true. Because we use only 1 bit of bCanFire, those expressions become almost equivalent. As of why they not used if (bCanFire) I may say that it is a bit more understandable to use direct comparison with ‘true’ value, than just if.

Hope this helps.

AFAIK it’s just bit-packing. All you’re doing is packing multiple booleans into a single variable. It can in some cases help with Replication for multiplayer, but it’s only really useful for that. There isn’t any real performance benefit to be gained from doing so in the real world.

It’s also worth noting that when you declare these bools, they all have to be sequentially declared in the header file. For example, these two will be packed:

uint8 MyBool : 1;
uint8 MyBool2 : 1;

whereas these will not, and will actually create new uint8’s for each:

uint8 MyBool : 1;
float MyFloat;
uint8 MyBool2 : 1;

EDIT: Also the reason you’re only getting UE4 results when you google it is because uint32 is a UE4 type.

Thank you both for helping. This really helps me understanding this technique a great deal. Another question is, what if I have 32 different classes (each of which has 1 bool declared this way of course) and each of them have 1 instance, would they occupy the same uint32?

Also, @TheJamsh I think uint32 is a generic data type since I could find a page in MSDN under .Net Framework 4.5 and 4.6 describing uint32 structure. Just nothing on using uint32 for bools. I guess not many people need 32 bools.

I could be wrong but I’m not sure that Unreal uses the .NET framework (which uint32 etc are a part of) - .NET is super old and deprecated now. They would still create new instances of those bools per-instance of that class. The reality is though, you’re not really saving any performance or memory by doing this (well you are for memory, but it’s so minuscule it probably doesn’t make a difference in the grand scheme of things).

Bit packing is still hella-useful though. So it’s all good practice nonetheless! It does improve replication / bandwidth performance I’ve found.

Here’s a quick bit of reading on the subject (this is C though, but still applies):

I can try to explain an example of where I (am planning) to use bitfields, though this may be easier for me to understand since I’m working in the project. I have an ActorComponent with a bunch of weapon ‘Hardpoints’ - basically an ‘Inventory’ system. These Hardpoints are then split into ‘Firing Groups’, so that a player can fire multiple weapons at the same time (think of having two guns mounted on the same turret or something, but acting as a single weapon).

In order to determine which firing group is active, I currently replicate a uint8 which just represents which firing group (usually between 1-4) is active. What I want to add is support for being able to fire multiple groups at a time, and since I already have that uint8 I can re-use it for the same purpose, and pay no extra cost for bandwidth or replication. I can pack a bool for each of the firing groups (uint8 has enough room for 8 bools, plenty for my purposes), and each one will represent one of the groups.

It does add a bit of processing overhead to pack all those flags in, but in Multiplayer bandwidth is your nemesis, so you almost always want to trade performance for bandwidth where you can.

If you get super-cocky bit bit-packing, you can even pack multiple data types into single variables, since you can reserve a continuous section of ‘bits’ as one data type. I’m currently using a uint32 to replicate four quantized floats, so I can send player input from a gamepad / joystick to the server and back again. I should probably post this on the Wiki actually…

Thanks for all the info. I actually thought it wouldn’t be very practical too. After all even iPhone has 2GB memory now, who cares about a few bits… I felt like it’s more like a fancy way to code, like dunk compared to lay-up. But as you’ve said this could be really useful in multiplayer. I’ll remember that. Thanks.