Optimized bitCount function