I would make a table - this is probably the fastest way.
You could, of course, do this:
int doublebits(int x) { int y = 0; int bit = 0; while(x) { if (x & 1) y |= 3 << bit; bit += 2; x >>= 1; } return y; }
For an 8-bit number, you will make no more than 8 shifts, and 8 shifts to the right to make a new number.
Mats petersson
source share