x char ? 255 ?
const unsigned char x = static_cast<unsigned char>(A - B);
, , , , :
const unsigned int x = static_cast<unsigned int>(A - B) & 0x1f;
EDIT:
:
template<unsigned int A, unsigned int B>
int foo() {
int v = 1;
const int x = A - B;
if (x > 0) {
v = v << (static_cast<unsigned int>(x) & 0x1f);
}
bar(v);
}
: 0x1f - : (CHAR_BIT * sizeof (T) - 1)
EDIT: , : g++ -W -Wall -ansi -pedantic test.cc -o test
#include <iostream>
template<unsigned int A, unsigned int B>
int foo() {
int v = 1;
const int x = A - B;
if (x > 0) {
v = v << (static_cast<unsigned int>(x) & 0x1f);
}
return v;
}
int main() {
std::cout << foo<1, 3>() << std::endl;
std::cout << foo<3, 1>() << std::endl;
std::cout << foo<300, 1>() << std::endl;
std::cout << foo<25, 31>() << std::endl;
}