Q:

Using sizeof to get maximum power of two

Is there a way in C/C++ to compute the maximum power of two that is representable by a certain data type using the sizeof operator?
For example, say I have an unsigned short int. Its values can range between 0 and 65535.
Therefore the maximum power of two that an unsigned short int can contain is 32768.
I pass this unsigned short int to a function and I have (at the moment) and algorithm that looks like this:
if (ushortParam > 32768) {
    ushortParam = 32768; // Bad hardcoded literals
}

However, in the future, I may want to change the variable type to incorporate larger powers of two. Is there a type-independent formula using sizeof() that can achieve the following:
if (param > /*Some function...*/sizeof(param) )
{
    param = /*Some function...*/sizeof(param);
}

Note the parameter will never require floating-point precision - integers only.

A:

Setting most significant bit of your a variable of that parameter size will give you the highest power of 2.
1 << (8*sizeof(param)-1)

A:

What about:
const T max_power_of_two = (std::numeric_limits<T>::max() >> 1) + 1;

