You can compute the difficulty in a much simpler manner, at least in C and C++.
Code:
#include <cmath>
double difficulty(const unsigned bits) {
const unsigned exponent_diff = 8 * (0x1D - ((bits >> 24) & 0xFF));
const double significand = bits & 0xFFFFFF;
return std::ldexp(0x00FFFF / significand, exponent_diff);
}
In C, just change cmath to math.h and std::ldexp to ldexp.
The ldexp() function lets you scale by a power of two almost for free, by directly adjusting the exponent on the floating point number. Thus, the difficulty calculation gets reduced to a couple integer arithmetic steps, single floating point divide, and a single scale-by-power-of-2.
This should be quite a bit faster than what's currently on the Wiki, and it's far more direct. No need for multiple floating point multiplies or an expensive floating point exponentiation.