I'm trying to toggle the most significant bit of an unsigned int
based on a bool
flag. This is my code for an hypothetical K = unit64_t
:
This is the Item
class:
template<typename K>
class Item {
public:
K first;
Item() = default;
explicit Item(const K &elem, const bool flag = false) {
first = elem & 0x3FFFFFFFFFFFFFFF;
first |= (flag * 0x8000000000000000);
}
};
Is there a way to do this fully generical? That it works for all kind of numeric K
?
I tried with the 8 * sizeof(K)
but it doesn't work.