UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
BitTwiddling.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "HAL/PlatformMath.h"
8#include "Traits/IntType.h"
9
10namespace UE::Net
11{
12
32{
33 typedef typename TUnsignedIntType<sizeof(T)>::Type SmallUnsignedType;
34
35 /* The algorithm for signed integers works as described below.
36 *
37 * 1. Replicate the sign bit to the right, by assuming right - shift on signed integers propagates the sign - bit.
38 * This will create a bit pattern consisting of all ones for negative numbersand all zeros for positive numbers.
39 * 2. Exclusive - or the bit - pattern with the original value.This will do nothing for positive numbers, but for
40 * negative this will clear out all the top bits that were set to 1 and then set the most significant zero bit to 1.
41 * 3. The resulting value from step 2 makes it possible to find the most significant bit set apart from sign - bits that
42 * can easily be derived from the bit above.The number of bits needed can now be calculated as 1 for the sign
43 * plus the number of bits in the type minus how many zero bits in the value after the most significant set bit.
44 */
45 const uint32 MassagedValue = uint32(SmallUnsignedType(T(Value ^ (Value >> (sizeof(T)*8 - 1U)))));
46 return 33U - static_cast<uint32>(FPlatformMath::CountLeadingZeros(MassagedValue));
47}
48
51{
52 return 32U - static_cast<uint32>(FPlatformMath::CountLeadingZeros(uint32(Value)));
53}
54
57{
58 const uint64 MassagedValue = uint64(Value ^ (Value >> 63U));
59 return 65U - static_cast<uint32>(FPlatformMath::CountLeadingZeros64(MassagedValue));
60}
61
64{
65 return 64U - static_cast<uint32>(FPlatformMath::CountLeadingZeros64(Value));
66}
67
78uint32 GetBitsNeededForRange(const T LowerBound, const T UpperBound)
79{
80 using UnsignedType = typename TUnsignedIntType<sizeof(T)>::Type;
81 const uint32 Range = uint32(UnsignedType(UnsignedType(UpperBound) - UnsignedType(LowerBound)));
82 return 32U - static_cast<uint32>(FPlatformMath::CountLeadingZeros(Range));
83}
84
86uint32 GetBitsNeededForRange(const T LowerBound, const T UpperBound)
87{
88 const uint32 Range = uint32(UpperBound) - uint32(LowerBound);
89 return 32U - static_cast<uint32>(FPlatformMath::CountLeadingZeros(Range));
90}
91
92template<typename T, typename TEnableIf<sizeof(T) == 8U, uint64>::Type X = 1ULL>
93uint32 GetBitsNeededForRange(const T LowerBound, const T UpperBound)
94{
95 const uint64 Range = uint64(UpperBound) - uint64(LowerBound);
96 return 64U - static_cast<uint32>(FPlatformMath::CountLeadingZeros64(Range));
97}
98
102{
103 using SignedT = typename TSignedIntType<sizeof(T)>::Type;
104 const T LeastSignificantBit = Value & T(-SignedT(Value));
105 return LeastSignificantBit;
106}
107
108}
FPlatformTypes::int64 int64
A 64-bit signed integer.
Definition Platform.h:1127
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition Platform.h:1117
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define X(Name, Desc)
Definition FormatStringSan.h:47
uint32_t uint32
Definition binka_ue_file_header.h:6
@ Range
Definition EnvQueryTypes.h:81
uint32 GetBitsNeeded(const int32 Value)
Definition QuantizedVectorSerialization.cpp:13
Definition NetworkVersion.cpp:28
Definition IntType.h:13
Definition IntType.h:34