Hello all :)
I'm looking for a fast way to setup a method of returning a bitmask based on a number. Basically, 4 one bits need to be emitted pre number input. Here's a good idea of what I mean:
foo(1); // returns 0x000F foo(2); // returns 0x00FF foo(3); // returns 0x0FFF foo(4); // returns 0xFFFF
I could just use a big switch statement, but I don't know how wide the input type is in advance. (This is a template function)
Here was the first thing I tried:
template <typename T> T foo(unsigned short length)
{
T result = 0xF;
for (unsigned short idx = length; idx > 0; idx--)
{
result = (result << 4 | 0xF);
}
return result;
}
but it spends a lot of time doing maintenence on the for loop. Any clever ways of doing this I've not thought of?
Billy3