fast inverse square root in csharp

Here is an implementation of the fast inverse square root algorithm in C#:

main.cs
public static float FastInverseSqrt(float x)
{
    float xhalf = 0.5f * x;
    int i = BitConverter.ToInt32(BitConverter.GetBytes(x), 0);
    i = 0x5f3759df - (i >> 1);
    x = BitConverter.ToSingle(BitConverter.GetBytes(i), 0);
    x = x * (1.5f - xhalf * x * x); // Newton's method
    return x;
}
300 chars
10 lines

The algorithm uses bit manipulation to find an initial approximation of the inverse square root, and then refines it using Newton's method. This algorithm is faster than the standard inverse square root function in most cases, but may not always be as accurate. It is commonly used in game development and other performance-critical applications.

gistlibby LogSnag