Rewrite bfi in C++ for future optimization.