![]() |
|
|||
File indexing completed on 2025-06-05 08:56:54
0001 #ifndef EDM4HEP_UTILS_BIT_UTILS_HH 0002 #define EDM4HEP_UTILS_BIT_UTILS_HH 0003 0004 #include <type_traits> 0005 0006 namespace edm4hep::utils { 0007 0008 /// Set a bit in the passed bitfield to the desired value 0009 /// 0010 /// @tparam T any integer type that can be used as a bitfield, typically an 0011 /// unsigned integer type 0012 /// 0013 /// @param bitfield The bitfield for which the bit should be set 0014 /// @param bit The bit (number) that should be set 0015 /// @param value The value to which the bit should be set 0016 /// 0017 /// @returns The new value of the bitfield after setting bits 0018 template <typename T> 0019 constexpr T setBit(T bitfield, int bit, bool value) { 0020 return (bitfield & ~(0x1 << bit)) | (value << bit); 0021 } 0022 0023 /// Set multiple bits to one desired value in the passed bitfield 0024 /// 0025 /// @tparam T any integer type that can be used as a bitfield, typically an 0026 /// unsigned integer type 0027 /// @tparam Bits A variable number of bits (numbers) that should be set 0028 /// 0029 /// @param bitfield The bitfield for which the bit should be set 0030 /// @param value The value to which the bit should be set 0031 /// @param bits The bits that should be set 0032 template <typename T, typename... Bits> 0033 constexpr T setBits(T bitfield, bool value, Bits... bits) { 0034 static_assert((std::is_same_v<Bits, int> && ...), "All bit numbers to set must be integers"); 0035 static_assert(sizeof...(bits) > 0, "Need at least one bit to set"); 0036 0037 for (auto n : {bits...}) { 0038 bitfield = setBit(bitfield, n, value); 0039 } 0040 return bitfield; 0041 } 0042 0043 /// Check if a bit is set in the bitfield 0044 /// 0045 /// @tparam T any integer type that can be used as a bitfield, typically an 0046 /// unsigned integer type 0047 /// 0048 /// @param bitfield The bitfield that should be checked 0049 /// @param bit The bit (number) that should be checked 0050 /// 0051 /// @returns true if the passed bit is set in the bitfield and false otherwise 0052 template <typename T> 0053 constexpr bool checkBit(T bitfield, int bit) { 0054 return bitfield & (0x1 << bit); 0055 } 0056 0057 /// Check if all the passed bits are set in the bitfield 0058 /// 0059 /// @tparam T any integer type that can be used as a bitfield, typically an 0060 /// unsigned integer type 0061 /// @tparam Bits A variable number of bits (numbers) that should be checked 0062 /// 0063 /// @param bitfield The bitfield that should be checked 0064 /// @param bits The bits that should be checked 0065 /// 0066 /// @returns true if all the passed bits are set in the bitfield and false 0067 /// otherwise 0068 template <typename T, typename... Bits> 0069 constexpr bool checkAllBits(T bitfield, Bits... bits) { 0070 static_assert((std::is_same_v<Bits, int> && ...), "All bit numbers to set must be integers"); 0071 static_assert(sizeof...(bits) > 0, "Need at least one bit to check"); 0072 return (true && ... && checkBit(bitfield, bits)); 0073 } 0074 0075 /// Check if any of the passed bits is set in the bitfield 0076 /// 0077 /// @tparam T any integer type that can be used as a bitfield, typically an 0078 /// unsigned integer type 0079 /// @tparam Bits A variable number of bits (numbers) that should be checked 0080 /// 0081 /// @param bitfield The bitfield that should be checked 0082 /// @param bits The bits that should be checked 0083 /// 0084 /// @returns true if any of the passed bits is set in the bitfield and false 0085 /// otherwise 0086 template <typename T, typename... Bits> 0087 constexpr bool checkAnyBits(T bitfield, Bits... bits) { 0088 static_assert((std::is_same_v<Bits, int> && ...), "All bit numbers to set must be integers"); 0089 static_assert(sizeof...(bits) > 0, "Need at least one bit to check"); 0090 return (false || ... || checkBit(bitfield, bits)); 0091 } 0092 0093 } // namespace edm4hep::utils 0094 0095 #endif // EDM4HEP_UTILS_BIT_UTILS_HH
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |