I am asking this question for two different languages: C and C++.
What is best practice when calling functions that have an opposite integer sign expectation to what we require in our code?
uint32 _depth; // uint32 = DWORD
_BitScanForward(&_depth, (uint32)input); // DWORD, DWORD
depth = (int)_depth;
It is very important to write an explicit cast when going from any integer type that is narrower than
int to any integer type that is the same width or wider than
int. If you don't do this, the compiler will first convert the value to
int, because of the "integer promotion" rules, and then to the destination type. This is almost always wrong, and we wouldn't design the language this way if we were starting from scratch today, but we're stuck with it for compatibility's sake.
System-provided typedefs like
DWORD might be narrower, wider, or the same size as
int; in C++ you can use templates to figure it out, but in C you can't. Therefore, you may want to write explicit casts for any conversion involving these.