FMath::SetBoolInBitField
#include "Math/UnrealMathUtility.h"
Access: public
Specifiers: staticconstexpr
Description
Sets or clears a single bit by index within a packed bitfield represented as a uint8 array. The companion to ExtractBoolFromBitfield.
Caveats & Gotchas
- • No bounds checking is performed. Passing an Index that exceeds the allocated size of the Ptr array will silently write to adjacent memory — a classic buffer-overwrite bug.
- • This function is primarily used by EngineShowFlags internals. For application-level bitfields, prefer TBitArray or a plain uint32 with bit masks, both of which are safer and more readable.
- • Thread safety is not guaranteed — concurrent reads/writes to the same byte (different bit indices in the same byte) are a data race.
Signature
static constexpr void SetBoolInBitField(uint8* Ptr, uint32 Index, bool bSet) Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| Ptr | uint8* | Pointer to the start of the bitfield byte array. | — |
| Index | uint32 | Zero-based bit index within the bitfield. | — |
| bSet | bool | True to set the bit, false to clear it. | — |
Return Type
void Example
Toggle a flag in a custom packed bitfield array C++
// Bitfield for 64 flags stored in 8 bytes
uint8 Flags[8] = {};
// Set flag at index 15
FMath::SetBoolInBitField(Flags, 15, true);
// Clear flag at index 15
FMath::SetBoolInBitField(Flags, 15, false);
// Read it back
bool bFlag = FMath::ExtractBoolFromBitfield(Flags, 15); Version History
Introduced in: 4.0
| Version | Status | Notes |
|---|---|---|
| 5.6 | stable | — |
Feedback
Was this helpful?