M
MikeP
I need help (but nevermind that!) templates and the C++ integer rules are
confusing me! (No occifer, I have not been drinking, I am just dazed and
confused by C++ integer rules!).
// CheckBit
// Checks the status of individual bits of any unsigned integer type.
// Indexing of bits starts at 1, not 0.
// mask can be any unsigned integer type.
// W is the width of the type T.
//
template <typename T, unsigned int W>
bool CheckBit(T& mask, T n)
{ // The assertion should be an exception, I know.
Assert((n >= 1) && (n <= W), "Bit index out of range.")
return ((mask & ((T)1) << (n-((T)1))) != 0);
}
I wanted to use the appropriate integer type suffix as a template
parameter to use on the 1's (don't know if I needed to though), but C++
templates won't allow that so I have the casts-to-T all over the place
but I'm not sure if they are necessary. I made arg n a type T just to
avoid fld and n being different unsigned integer types.
Is CheckBit correct? If not, can it be corrected? Can it be improved?
confusing me! (No occifer, I have not been drinking, I am just dazed and
confused by C++ integer rules!).
// CheckBit
// Checks the status of individual bits of any unsigned integer type.
// Indexing of bits starts at 1, not 0.
// mask can be any unsigned integer type.
// W is the width of the type T.
//
template <typename T, unsigned int W>
bool CheckBit(T& mask, T n)
{ // The assertion should be an exception, I know.
Assert((n >= 1) && (n <= W), "Bit index out of range.")
return ((mask & ((T)1) << (n-((T)1))) != 0);
}
I wanted to use the appropriate integer type suffix as a template
parameter to use on the 1's (don't know if I needed to though), but C++
templates won't allow that so I have the casts-to-T all over the place
but I'm not sure if they are necessary. I made arg n a type T just to
avoid fld and n being different unsigned integer types.
Is CheckBit correct? If not, can it be corrected? Can it be improved?