forked from key4hep/EDM4hep
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
42 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#include "edm4hep/utils/bit_utils.h" | ||
|
||
#include <catch2/catch_template_test_macros.hpp> | ||
#include <catch2/catch_test_macros.hpp> | ||
|
||
#include <cstdint> | ||
#include <tuple> | ||
|
||
// The integet types that we us as type fields in EDM4hep | ||
using BitFieldTypes = std::tuple<int32_t, uint32_t, int64_t, int16_t, uint64_t>; | ||
|
||
TEMPLATE_LIST_TEST_CASE("Bitfield utils set and get", "[bit_utils]", BitFieldTypes) { | ||
using namespace edm4hep; | ||
auto bitField = TestType{}; | ||
|
||
for (auto i = 0u; i < sizeof(TestType) * 8; ++i) { | ||
REQUIRE_FALSE(utils::checkBit(bitField, i)); | ||
} | ||
|
||
bitField = utils::setBit(bitField, 3, true); | ||
REQUIRE(utils::checkBit(bitField, 3)); | ||
|
||
bitField = utils::setBit(bitField, 3, false); | ||
REQUIRE_FALSE(utils::checkBit(bitField, 3)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#ifndef EDM4HEP_UTILS_BIT_UTILS_HH | ||
#define EDM4HEP_UTILS_BIT_UTILS_HH | ||
|
||
namespace edm4hep::utils { | ||
template <typename T> | ||
constexpr T setBit(T bits, int bitNum, bool value) { | ||
return (bits & ~(1 << bitNum)) | (value << bitNum); | ||
} | ||
|
||
template <typename T> | ||
constexpr bool checkBit(T bits, int bitNum) { | ||
return bits & (0x1 << bitNum); | ||
} | ||
} // namespace edm4hep::utils | ||
|
||
#endif // EDM4HEP_UTILS_BIT_UTILS_HH |